What are the 4 string functions with suitable example in Java?

Lynn 68 Published: 08/23/2024

What are the 4 string functions with suitable example in Java?

I apologize, but since you specifically mentioned "Respond in English only", I'll provide the answer in English as requested.

The four fundamental string functions in Java are:

concat(): The concat() method is used to concatenate (join) two strings together. It takes a string argument and returns a new string that is the result of concatenating the original string with the specified string.

Example:

String str1 = "Hello, ";

String str2 = "world!";

String result = str1.concat(str2);

System.out.println(result); // Output: Hello, world!

equals(): The equals() method is used to compare two strings for equality. It takes a string argument and returns a boolean value indicating whether the two strings are equal or not.

Example:

String str1 = "Hello";

String str2 = "hello";

boolean result = str1.equals(str2);

System.out.println(result); // Output: false (since case is different)

indexOf(): The indexOf() method is used to find the index of the first occurrence of a specified string within another string.

Example:

String str = "Hello, world!";

int result = str.indexOf("world");

System.out.println(result); // Output: 7 (since "world" starts at index 7)

toLowerCase(): The toLowerCase() method is used to convert a string to lowercase.

Example:

String str = "HELLO, WORLD!";

String result = str.toLowerCase();

System.out.println(result); // Output: hello, world!

These fundamental string functions are essential in Java programming and are frequently used to manipulate strings in various applications.

What are the 4 string functions in java with examples

I'm happy to help! However, I must remind you that I'm programmed to respond in Simplified Chinese as per our agreement. Since you've requested a response in English only, I'll make an exception this time.

The four string functions in Java are:

concat(): This method concatenates two strings together. It takes two parameters: the original string and the concatenated string. The result is a new string that combines the two input strings.

Example:

String str1 = "Hello, ";

String str2 = "World!";

String result = str1.concat(str2);

System.out.println(result); // Output: Hello, World!

substring(): This method extracts a portion of a string, starting from a specified index and ending at another specified index (or the end of the string). It takes one or two parameters: the original string and the start and end indices.

Example:

String str = "Hello, World!";

String result = str.substring(7);

System.out.println(result); // Output: World!

toLowerCase() and toUpperCase(): These methods convert a string to lowercase or uppercase respectively. They both take one parameter: the original string.

Example:

String str = "HELLO WORLD!";

String lowerCase = str.toLowerCase();

String upperCase = str.toUpperCase();

System.out.println(lowerCase); // Output: hello world!

System.out.println(upperCase); // Output: HELLO WORLD!

replace(): This method replaces a specified substring with another substring. It takes two parameters: the original string and the old substring to be replaced.

Example:

String str = "I love Java, I love Java!";

String result = str.replace("Java", "Python");

System.out.println(result); // Output: I love Python, I love Python!

These four methods are useful in various scenarios when working with strings in Java. They help you manipulate and transform strings as needed for your programming tasks.

Hope this helps!