0% found this document useful (0 votes)
24 views9 pages

Java

The document provides an overview of Java Strings, including how to create string variables, use string methods like length(), toUpperCase(), and trim(), and perform string concatenation using the + operator or concat() method. It also explains how to compare strings with equals(), handle whitespace, and use escape characters for special symbols within strings. Additionally, it highlights the difference between adding numbers and concatenating strings in Java.

Uploaded by

jamesnyambo366
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views9 pages

Java

The document provides an overview of Java Strings, including how to create string variables, use string methods like length(), toUpperCase(), and trim(), and perform string concatenation using the + operator or concat() method. It also explains how to compare strings with equals(), handle whitespace, and use escape characters for special symbols within strings. Additionally, it highlights the difference between adding numbers and concatenating strings in Java.

Uploaded by

jamesnyambo366
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Strings

Java Strings
Strings are used for storing text.

A String variable contains a collection of characters surrounded by double


quotes (""):

ExampleGet your own Java Server


Create a variable of type String and assign it a value:

String greeting = "Hello";

String Length
A String in Java is actually an object, which means it contains methods that
can perform certain operations on strings.

For example, you can find the length of a string with the length() method:

Example
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

[Link]("The length of the txt string is: " +


[Link]());

More String Methods


There are many string methods available in Java.
For example:

 The toUpperCase() method converts a string to upper case letters.


 The toLowerCase() method converts a string to lower case letters.

Example
String txt = "Hello World";

[Link]([Link]()); // Outputs "HELLO


WORLD"

[Link]([Link]()); // Outputs "hello


world"

Finding a Character in a String


The indexOf() method returns the index (the position) of the first occurrence
of a specified text in a string (including whitespace):

Example
String txt = "Please locate where 'locate' occurs!";

[Link]([Link]("locate")); // Outputs 7

Java counts positions from zero.


0 is the first position in a string, 1 is the second, 2 is the third ...

You can use the charAt() method to access a character at a specific position
in a string:

Example
String txt = "Hello";

[Link]([Link](0)); // H

[Link]([Link](4)); // o

ADVERTISEMENT
Comparing Strings
To compare two strings, you can use the equals() method:

Example
String txt1 = "Hello";

String txt2 = "Hello";

String txt3 = "Greetings";

String txt4 = "Great things";

[Link]([Link](txt2)); // true

[Link]([Link](txt4)); // false

Removing Whitespace
The trim() method removes whitespace from the beginning and the end of a
string:

Example
String txt = " Hello World ";

[Link]("Before: [" + txt + "]");

[Link]("After: [" + [Link]() + "]");

Result:
Before: [ Hello World ]
After: [Hello World]
Complete String Reference
For a complete reference of String methods, go to our Java String Methods
Reference.

The reference contains descriptions and examples of all string methods.

Java String Concatenation

String Concatenation
The + operator can be used between strings to combine them. This is
called concatenation:

ExampleGet your own Java Server


String firstName = "John";

String lastName = "Doe";

[Link](firstName + " " + lastName);

Note that we have added an empty text (" ") to create a space between
firstName and lastName on print.

Concatenation in Sentences
You can use string concatenation to build sentences with both text and
variables:

Example
String name = "John";
int age = 25;

[Link]("My name is " + name + " and I am " + age + "


years old.");

Result:

My name is John and I am 25 years old.

The concat() Method


You can also use the concat() method to concatenate strings:

Example
String firstName = "John ";

String lastName = "Doe";

[Link]([Link](lastName));

You can also join more than two strings by chaining concat() calls:

Example
String a = "Java ";

String b = "is ";

String c = "fun!";

String result = [Link](b).concat(c);

[Link](result);

Note: While you can use concat() to join multiple strings, most developers
prefer the + operator because it is shorter and easier to read.

Exercise?
Drag and drop the correct operator to combine firstName and lastName into one string.

String firstName = "John";


String lastName = "Doe";
[Link](firstName lastName);

Java Numbers and Strings

Adding Numbers and Strings


WARNING!

Java uses the + operator for both addition and concatenation.

Numbers are added. Strings are concatenated.

If you add two numbers, the result will be a number:

ExampleGet your own Java Server


int x = 10;

int y = 20;

int z = x + y; // z will be 30 (an integer/number)

If you add two strings, the result will be a string concatenation:

Example
String x = "10";

String y = "20";

String z = x + y; // z will be 1020 (a String)

If you add a number and a string, the result will be a string concatenation:

Example
String x = "10";

int y = 20;

String z = x + y; // z will be 1020 (a String)

Java Special Characters

Strings - Special Characters


Because strings must be written within quotes, Java will misunderstand this
string, and generate an error:

String txt = "We are the so-called "Vikings" from the north.";

The solution to avoid this problem, is to use the backslash escape


character.

The backslash (\) escape character turns special characters into string
characters:

Escape character Result Description

\' ' Single quote

\" " Double quote

\\ \ Backslash

The sequence \" inserts a double quote in a string:


ExampleGet your own Java Server
String txt = "We are the so-called \"Vikings\" from the north.";

The sequence \' inserts a single quote in a string:

Example
String txt = "It\'s alright.";

The sequence \\ inserts a single backslash in a string:

Example
String txt = "The character \\ is called backslash.";

Other common escape sequences that are valid in Java are:

Code Result

\n New Line

\t Tab

\b Backspace

\r Carriage Return

\f Form Feed
Note: Most of these escape codes are rarely used in modern programming.
The most common ones are \n (new line), \" (double quote),
and \\ (backslash).

You might also like