Java String
Java String
• String is a sequence of character treated as single unit such as
“Hello World”.
• May include letters, digits, etc.
• String immutable, once a string is created it cannot be changed.
None of its methods changes the string.
Creating a String
• The common way to create a string by using string literals or
using quotes and assign it in a variable;
• Syntax:
• String variableName = “String”;
• String firstName = “Pedro”;
• String lastName = “Penduko”;
• String course = ”Computer Science”;
• String age = “22”;
String concatenation
String firstName = “Pedro”;
String lastName = “Penduko”;
String course = ”Computer Science”;
String BSCS = “Bachelor of science in “ + course
String fullName = firstName + “ “ + lastName;
String newString = [Link](“ “).concat(lastName);
String Methods
String methods in Java are built-in functions that make it easier to
manipulate and work with strings.
Commonly used String Method:
.length() .toLowerCase()
.charAt() .concat()
.equals() .replace()
.equalsIgnoreCase() .trim()
.startWith() .indexOf()
.endWith()
.toUpperCase()
String Methods
The .length() method returns the length of the string.
ex.
String course = "Computer Science";
[Link]([Link]()); //output: 16
The .charAt() method returns the character at the specified index.
ex.
String course = "Computer Science";
[Link]([Link](5)); //output: t
String Methods
The .equals() method it compares the strings, it return true if
arguments are the same sequence of character (case-sensitive)
ex.
String string1 = "Hello";
String string2 = "hello";
String string3 = “Hello”
[Link]([Link](string2)); //Output: false
[Link]([Link](string3)); //Output: true
String Methods
The .equalsIgnoreCase() method it compares the strings, it return
true if arguments are the same sequence of character but ignoring
case differences.
ex.
String string1 = "Hello";
String string2 = "hello";
String string3 = “Hello”
[Link]([Link](string2));
//Output: true
String Methods
The .startsWith() method checks the string starts with the specified
prefix.
ex.
String string1 = "Hello";
[Link]([Link](“He”)); //Output: true
[Link]([Link](“he”)); //Output: false
String Methods
The .endsWith() method checks the string ends with the specified
prefix.
ex.
String string1 = "Hello";
[Link]([Link](“lo”)); //Output: true
[Link]([Link](“llo”)); //Output: true
[Link]([Link](“lO”)); //Output: false
String Methods
The .toUpperCase() method converts all characters in a string to
uppercase.
ex.
String string1 = "heLLo World!";
[Link]([Link]());
//Output: HELLO WORLD!
String Methods
The .toLowerCase() method converts all characters in a string to
lowercase.
ex.
String string1 = "heLLo World!";
[Link]([Link]());
//Output: hello world!
String Methods
The .trim() method removes whitespace from the beginning and
end.
ex.
String string1 = " hello world! ";
[Link]([Link]());
//Output: hello world!
String Methods
The .indexOf() method searches for the first occurence of a
character or substring.
ex.
String string1 = "hello world!”;
[Link]([Link](“o”));
//Output: 4
[Link]([Link](“llo”));
//Output: 2
[Link]([Link](“abc”));
//Output: -1
String Methods
The .contains() method checks if a specified characters exists
within a given string.
ex.
String string1 = "hello world!”;
[Link]([Link](“llo”));
//Output: true
[Link]([Link](“hi”));
//Output: false
String Methods
The .concat() method combine the specified string to the end of the
first string.
ex.
String string1 = "hello”;
String string2 = “world”
[Link]([Link](string2));
//Output: helloworld
String Methods
The .concat() method combine the specified string to the end of the
first string.
ex.
String string1 = "hello”;
String string2 = “world”
[Link]([Link](“kitty”));
//Output: hellokitty
String Methods
The .isEmpty method check if the string is empty.
ex.
String string1 = "”;
String string2;
[Link]([Link]());
//Output: true
[Link]([Link]());
//Output: Error
String Methods
The .substring() method returns a portion of the string from a given
index.
ex.
String string1 = "hello world”;
[Link]([Link](2));
//Output: llo world
[Link]([Link](2,8));
//Output: llo wo
String Methods
The .replace() method replace all occurences of a characters in the
string with new characters.
ex.
String string1 = "hello world”;
[Link]([Link](‘l’, ‘*’));
//Output: he**o wor*d
[Link]([Link](“ll”, ‘*’));
//Output: he*o world