0% found this document useful (0 votes)
2 views

Java String Methods

The document provides an overview of Java Strings, describing them as immutable sequences of characters. It covers how to create strings, concatenate them, and utilize various built-in string methods such as .length(), .charAt(), .equals(), and .replace(). Additionally, it includes examples demonstrating the functionality of these methods.

Uploaded by

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

Java String Methods

The document provides an overview of Java Strings, describing them as immutable sequences of characters. It covers how to create strings, concatenate them, and utilize various built-in string methods such as .length(), .charAt(), .equals(), and .replace(). Additionally, it includes examples demonstrating the functionality of these methods.

Uploaded by

LeX GAMING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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 = firstName.concat(“ “).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";
System.out.println(course.length()); //output: 16

The .charAt() method returns the character at the specified index.


ex.
String course = "Computer Science";
System.out.println(course.charAt(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”

System.out.println(string1.equals(string2)); //Output: false


System.out.println(string1.equals(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”

System.out.println(string1.equalsIgnoreCase(string2));
//Output: true
String Methods

The .startsWith() method checks the string starts with the specified
prefix.
ex.
String string1 = "Hello";

System.out.println(string1.startsWith(“He”)); //Output: true


System.out.println(string1.startsWith(“he”)); //Output: false
String Methods

The .endsWith() method checks the string ends with the specified
prefix.
ex.
String string1 = "Hello";

System.out.println(string1.endsWith(“lo”)); //Output: true


System.out.println(string1.endsWith(“llo”)); //Output: true
System.out.println(string1.endsWith(“lO”)); //Output: false
String Methods

The .toUpperCase() method converts all characters in a string to


uppercase.
ex.
String string1 = "heLLo World!";

System.out.println(string1.toUpperCase());
//Output: HELLO WORLD!
String Methods

The .toLowerCase() method converts all characters in a string to


lowercase.
ex.
String string1 = "heLLo World!";

System.out.println(string1.toLowerCase());
//Output: hello world!
String Methods

The .trim() method removes whitespace from the beginning and


end.
ex.
String string1 = " hello world! ";

System.out.println(string1.trim());
//Output: hello world!
String Methods

The .indexOf() method searches for the first occurence of a


character or substring.
ex.
String string1 = "hello world!”;

System.out.println(string1.indexOf(“o”));
//Output: 4
System.out.println(string1.indexOf(“llo”));
//Output: 2
System.out.println(string1.indexOf(“abc”));
//Output: -1
String Methods

The .contains() method checks if a specified characters exists


within a given string.
ex.
String string1 = "hello world!”;

System.out.println(string1.contains(“llo”));
//Output: true
System.out.println(string1.indexOf(“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”

System.out.println(string1.concat(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”

System.out.println(string1.concat(“kitty”));
//Output: hellokitty
String Methods

The .isEmpty method check if the string is empty.


ex.
String string1 = "”;
String string2;

System.out.println(string1.isEmpty());
//Output: true

System.out.println(string2.isEmpty());
//Output: Error
String Methods

The .substring() method returns a portion of the string from a given


index.
ex.
String string1 = "hello world”;

System.out.println(string1.substring(2));
//Output: llo world

System.out.println(string1.substring(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”;

System.out.println(string1.replace(‘l’, ‘*’));
//Output: he**o wor*d

System.out.println(string1.replace(“ll”, ‘*’));
//Output: he*o world

You might also like