What are the 4 string functions in java with examples

Vernon 130 Published: 07/29/2024

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!

What are the 5 functions of string in Java?

I'd be happy to help! Here's a detailed explanation of the 5 main functions of Strings in Java:

1. Immutable Objects: In Java, strings are immutable objects, which means that once a string is created, its value cannot be changed. This is different from other programming languages like C++, where strings can be modified after they're created. Because strings are immutable, this ensures that the original string remains unchanged, even if you try to modify it.

2. Concatenation: The + operator in Java is overloaded to perform concatenation on strings. This means that when you combine two strings using the + operator, a new string is created that contains the characters from both strings. For example: String str1 = "Hello "; String str2 = "World!"; String result = str1 + str2; System.out.println(result); // Output: "Hello World!".

3. Indexing: Strings in Java have indexing capabilities, which allows you to access a specific character or substring within the string. You can use the charAt() method to get the character at a specified index or the substring() method to extract a part of the string.

For example:

String str = "Hello";

char c = str.charAt(0); // c would be 'H'

String substr = str.substring(1, 3); // substr would be "el"

4. Comparing Strings: Java provides several methods to compare strings, including equals(), equalsIgnoreCase(), and compareTo().

The equals() method checks whether two strings are exactly the same, including case sensitivity.

The equalsIgnoreCase() method ignores case while comparing the strings.

The compareTo() method compares two strings lexicographically. It returns a negative integer if the first string is less than the second, zero if they're equal, and positive if the first string is greater than the second.

For example:

String str1 = "Hello";

String str2 = "hello";

System.out.println(str1.equals(str2)); // false (case sensitivity)

System.out.println(str1.equalsIgnoreCase(str2)); // true (ignoring case)

5. Pattern Matching: Java's Pattern class and its related classes allow you to perform pattern matching on strings. This enables you to search for specific patterns within a string, such as searching for all occurrences of a particular word or phrase.

For example:

String str = "The quick brown fox jumps over the lazy dog";

Pattern p = Pattern.compile("quick|lazy");

Matcher m = p.matcher(str);

while (m.find()) {

System.out.println(m.group()); // prints "quick" and then "lazy"

}

These 5 functions provide a comprehensive set of operations for working with strings in Java. They allow you to manipulate, compare, and search strings efficiently and accurately, making them essential tools for any Java developer.