What are the different Java string methods?

Virginia 188 Published: 08/05/2024

What are the different Java string methods?

Java provides a wide range of string manipulation methods that can be used to process and manipulate strings. Here are some of the most commonly used Java string methods:

equals(): This method is used to compare two strings for equality. It returns true if the two strings are identical, and false otherwise.

Example: String str1 = "Hello"; String str2 = "Hello"; boolean result = str1.equals(str2);

equalsIgnoreCase(): This method is similar to the equals() method but it ignores case sensitivity. It returns true if the two strings are identical regardless of case, and false otherwise.

Example: String str1 = "hello"; String str2 = "HELLO"; boolean result = str1.equalsIgnoreCase(str2);

startsWith(): This method is used to check if a string starts with a specified prefix. It returns true if the string starts with the prefix, and false otherwise.

Example: String str1 = "Hello World"; boolean result = str1.startsWith("Hello");

endsWith(): This method is similar to the startsWith() method but it checks if a string ends with a specified suffix.

Example: String str1 = "Hello World"; boolean result = str1.endsWith("World");

charAt(): This method returns the character at a specific index in a string.

Example: String str1 = "Hello"; char result = str1.charAt(0);

indexOf(): This method is used to find the index of the first occurrence of a specified substring or character within a string. It returns -1 if the substring or character is not found.

Example: String str1 = "Hello World"; int result = str1.indexOf("World");

lastIndexOf(): This method is similar to the indexOf() method but it finds the index of the last occurrence of a specified substring or character within a string.

Example: String str1 = "Hello World"; int result = str1.lastIndexOf("o");

subString(): This method returns a portion of a string, starting at a specified index and ending at another specified index.

Example: String str1 = "Hello World"; String result = str1.substring(6);

replace(): This method replaces all occurrences of a specified substring with another substring.

Example: String str1 = "Hello World"; String result = str1.replace("World", "Universe");

split(): This method splits a string into multiple substrings based on a specified delimiter.

Example: String str1 = "Hello,World,Java"; String[] result = str1.split(",");

trim(): This method removes leading and trailing whitespace from a string.

Example: String str1 = " Hello World "; String result = str1.trim();

length(): This method returns the length of a string, i.e., the number of characters in the string.

Example: String str1 = "Hello"; int result = str1.length();

concat(): This method concatenates two strings into one.

Example: String str1 = "Hello"; String str2 = "World"; String result = str1.concat(str2);

toUpperCase() and toLowerCase(): These methods convert a string to uppercase or lowercase, respectively.

Example: String str1 = "hello"; String upperCaseResult = str1.toUpperCase(); String lowerCaseResult = str1.toLowerCase();

format(): This method formats a string using placeholders and arguments.

Example: String str1 = "Hello {0}"; String result = str1.format("World");

join(): This method joins multiple strings into one using a specified delimiter.

Example: String[] strArray = {"Hello", "World", "Java"}; String result = String.join(",", strArray);

These are just a few of the many string methods available in Java. By using these methods, you can perform various string manipulation tasks and create more sophisticated programs.

What are the 4 string functions in java with examples w3schools

I'm happy to help you with that!

In Java, there are four string manipulation methods: concat(), substring(), toUpperCase(), and toLowerCase(). Let's dive into each of them and explore some examples using W3Schools.

1. Concat()

The concat() method concatenates two strings together. It returns a new string that is the result of joining the original string with another string.

Example:

String str = "Hello, ";

String str2 = "World!";

String result = str.concat(str2);

In this example, we have two strings: str and str2. We concatenate them using concat(), resulting in a new string result.

2. Substring()

The substring() method extracts a portion of the original string.

Example:

String str = "Hello, World!";

String result = str.substring(7);

In this example, we want to extract the substring starting from index 7 (inclusive) until the end of the original string. The resulting substring would be "World!".

3. ToUpper()

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

Example:

String str = "hello, world!";

String result = str.toUpperCase();

In this example, we convert the original string from lowercase to uppercase using toUpperCase(). The resulting string would be "HELLO, WORLD!".

4. ToLower()

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

Example:

String str = "HELLO, WORLD!";

String result = str.toLowerCase();

In this example, we convert the original string from uppercase to lowercase using toLowerCase(). The resulting string would be "hello, world!".

These four methods are essential for working with strings in Java. By mastering these techniques, you'll be well-equipped to handle various string manipulation tasks.

Additional resources:

W3Schools: Java String Methods Oracle Java Tutorials: String Methods

Hope this helps!