Java Strings
Creating Strings
String Methods
Java Strings
Storing Text in String Variables
Using Special Characters in String
Display Strings
Concatenate Strings
Using Other Variables with Strings
Strings are sequences of characters beginning
and ending with double-quotes.
Unlike other programming languages, Java does
not use an array of characters to represent a
string.
Use a String by typing some text surrounded
by double quotes. This is called a String literal.
“This is a Java String”
A character is the basic element of a string. It is
a single letter, number, punctuation mark, or
other symbol.
In Java programs, a character is enclosed by
single quotes.
Character variables are created with the char
type in a statement.
char message;
When you create character variables, you can set them up
with an initial value.
char message = ‘A’;
A string is a collection of characters.
Using the String literal and the name of the variable, you can
set up a variable to hold a string value.
String fullName = “Juan dela Cruz”;
You can also assign a String literal to a String variable to
change its content.
fullName = “Jose Rizal”;
If you want to include double quotation marks as
part of a String, Java has created a special code
sequence for this: \”.
Every time this code sequence is encountered in
a String literal, it is replaced with the double
quotation marks.
String str = “Juan watched the
movie \ “Spiderman\” in 2007.”;
The System.out.println() statement is used to
display a string in a Java program.
System.out.println(“Hello World!”);
When you say display a line of text on the screen, this
often refers to printing.
Java allows the use of the + sign to concatenate or connect
two strings together.
String a = “Hello”;
String b = “World”;
String message = a + b;
When you concatenate a string with a value that is not a
string, the latter is converted to a string.
int age = 18;
String rating = “R” + age;
Example:
int length = 120;
char rating = ‘R’;
System.out.println(“Running time: ” +
length + “ minutes”);
System.out.println(“Rated ” + rating);
Output:
Running time: 120 minutes
Rated R
String findWords = “”;
findWords = findWords + “comedy”;
findWords = findWords + “action”;
findWords = findWords + “Philippines”;
The += operator combines the functions of the = and +
operators. With strings, it is used to add something to the
end of an existing string.
String findWords = “”;
findWords += “comedy”;
findWords += “action”;
findWords += “Philippines”;
String strGreeting = “Hello World”;
String “Hello World”
Charact
‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘ ’ ‘W’ ‘o’ ‘r’ ‘l’ ‘d’
er
Index 0 1 2 3 4 5 6 7 8 9 10
strGreeting.charAt(0); // returns char H
strGreeting.charAt(8); // returns char r
Directly assigning string literal to a String object:
String str1 = “Hello World”;
Using the new keyword and String constructor:
String str1 = new String(“Hello World”);
String str1 = “Computer”;
String str2 = “Computer”;
String str3 = new String(“Computer”);
String str4 = new String(“Computer”);
Memory
str1
“Computer”
str2
str3
“Computer”
“Computer”
str4
Assume: String str = “Java”;
Method Example Return value
charAt(index) str.charAt(2) ‘v’
compareTo(string) str.compareTo(“Java’s”) -2
concat(string) str.concat(“ program”) “Java program”
equals(string) str.equals(“Java”) true
equalsIgnorCase str.equalsIgnoreCase
true
(string) (“java”)
indexOf(string) str.indexOf(“a”) 1
lastIndexOf
str.lastIndexOf(“a”) 3
(string)
Assume: String str = “Java”, strEx = “ Java Java ”;
Method Example Return value
length() str.length() 4
toLowerCase() str.toLowerCase() “java”
toLowerCase() Str.toUpperCase() “JAVA”
replace(oldChar,
str.replace(‘a’, ‘o’) “Jovo”
newChar)
substring(start) str.substring(2) “va”
substring(start,
str.substring(1, 3) “av”
end)
trim() strEx.trim() “Java Java”
String strName = “Jess Diaz”;
String strConverted = strName.toUpperCase();
System.out.println(strConverted); //JESS DIAZ
System.out.println(strName); //Jess Diaz
Problem
1. Assume: String sentence = “The quick brown fox
jumps over the lazy dog.”;
Write Java statements that will count and display the total
number of character ‘o’ in the string.
2. Assume:
String str1 = “Class”, str2 = “ class ”;
Write Java statements that will display true when both
variables are compared.
3. Assume: String sentence = “The quick brown fox
jumps over the lazy dog.”;
Write Java statements that will display the string replacing all
the whitespaces to character ‘%’.
Problem
4. Assume: String sentence = “The quick brown fox
jumps over the lazy dog.”;
Write Java statements that will display a message specifying
what is the index position of character ‘a’ in the string.
5. Write Java statements that will ask the user to enter a string
then print each character of this character using a loop
statement.