Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Java String Methods in Java

Java String Methods in Java

In Java, the String class is part of the java.lang package and provides many useful methods for manipulating and working with strings. Here’s a comprehensive list of common String methods in Java, along with examples of how to use them.

1. length()

Returns the length of the string (i.e., the number of characters it contains).

String str = "Hello, World!";int length = str.length();System.out.println("Length: " + length);  // Output: 13

2. charAt(int index)

Returns the character at the specified index.

String str = "Hello";char c = str.charAt(1);System.out.println("Character at index 1: " + c);  // Output: e

3. equals(Object obj)

Compares two strings for equality. Returns true if the strings are equal, false otherwise.

String str1 = "hello";String str2 = "hello";boolean isEqual = str1.equals(str2);System.out.println("Strings are equal: " + isEqual);  // Output: true

4. equalsIgnoreCase(String anotherString)

Compares two strings, ignoring case differences.

String str1 = "hello";String str2 = "HELLO";boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);System.out.println("Strings are equal ignoring case: " + isEqualIgnoreCase);  // Output: true

5. toLowerCase()

Converts all characters of the string to lowercase.

String str = "HELLO";String lowerCase = str.toLowerCase();System.out.println("Lowercase: " + lowerCase);  // Output: hello

6. toUpperCase()

Converts all characters of the string to uppercase.

String str = "hello";String upperCase = str.toUpperCase();System.out.println("Uppercase: " + upperCase);  // Output: HELLO

7. trim()

Removes leading and trailing whitespace from the string.

String str = "   Hello, World!   ";String trimmed = str.trim();System.out.println("Trimmed: '" + trimmed + "'");  // Output: 'Hello, World!'

8. substring(int beginIndex)

Returns a substring starting from the specified index to the end of the string.

String str = "Hello, World!";String subStr = str.substring(7);System.out.println("Substring: " + subStr);  // Output: World!

9. substring(int beginIndex, int endIndex)

Returns a substring from the specified beginIndex to endIndex (endIndex is not included).

String str = "Hello, World!";String subStr = str.substring(0, 5);System.out.println("Substring: " + subStr);  // Output: Hello

10. indexOf(String str)

Returns the index of the first occurrence of the specified substring.

String str = "Hello, World!";int index = str.indexOf("World");System.out.println("Index of 'World': " + index);  // Output: 7

11. lastIndexOf(String str)

Returns the index of the last occurrence of the specified substring.

String str = "Hello, World, World!";int lastIndex = str.lastIndexOf("World");System.out.println("Last index of 'World': " + lastIndex);  // Output: 14

12. replace(char oldChar, char newChar)

Replaces all occurrences of oldChar with newChar in the string.

String str = "Hello, World!";String replaced = str.replace('o', 'a');System.out.println("Replaced: " + replaced);  // Output: Hella, Warld!

13. replaceAll(String regex, String replacement)

Replaces all substrings that match the given regular expression with the specified replacement.

String str = "abc123abc";String replaced = str.replaceAll("\\d", "#");System.out.println("Replaced: " + replaced);  // Output: abc###abc

14. split(String regex)

Splits the string into an array of substrings based on the given regular expression.

String str = "apple,banana,cherry";String[] fruits = str.split(",");for (String fruit : fruits) {    System.out.println(fruit);}// Output:// apple// banana// cherry

15. contains(CharSequence sequence)

Returns true if the string contains the specified sequence of characters.

String str = "Hello, World!";boolean contains = str.contains("World");System.out.println("Contains 'World': " + contains);  // Output: true

16. startsWith(String prefix)

Returns true if the string starts with the specified prefix.

String str = "Hello, World!";boolean startsWith = str.startsWith("Hello");System.out.println("Starts with 'Hello': " + startsWith);  // Output: true

17. endsWith(String suffix)

Returns true if the string ends with the specified suffix.

String str = "Hello, World!";boolean endsWith = str.endsWith("World!");System.out.println("Ends with 'World!': " + endsWith);  // Output: true

18. toCharArray()

Converts the string to an array of characters.

String str = "Hello";char[] charArray = str.toCharArray();for (char c : charArray) {    System.out.print(c + " ");}// Output: H e l l o

19. valueOf(Object obj)

Returns the string representation of the specified object.

int number = 123;String str = String.valueOf(number);System.out.println("String value: " + str);  // Output: 123

20. format(String format, Object... args)

Returns a formatted string using the specified format string and arguments.

String str = String.format("Hello, %s! You have %d new messages.", "Alice", 5);System.out.println(str);  // Output: Hello, Alice! You have 5 new messages.

21. compareTo(String anotherString)

Compares two strings lexicographically. Returns 0 if the strings are equal, a negative integer if the string is lexicographically less than the other string, and a positive integer if it is greater.

String str1 = "apple";String str2 = "banana";int result = str1.compareTo(str2);System.out.println("Comparison result: " + result);  // Output: Negative value (apple < banana)

22. compareToIgnoreCase(String anotherString)

Compares two strings lexicographically, ignoring case considerations.

String str1 = "hello";String str2 = "HELLO";int result = str1.compareToIgnoreCase(str2);System.out.println("Comparison result (ignore case): " + result);  // Output: 0 (equal ignoring case)

Summary of Common String Methods

MethodDescription
length()Returns the length of the string.
charAt(int index)Returns the character at the specified index.
equals(String other)Compares two strings for equality.
equalsIgnoreCase(String other)Compares two strings ignoring case.
toLowerCase()Converts all characters to lowercase.
toUpperCase()Converts all characters to uppercase.
trim()Removes leading and trailing whitespaces.
substring(int beginIndex)Returns the substring from beginIndex to end.
substring(int beginIndex, int endIndex)Returns the substring between the given indexes.
indexOf(String str)Returns the index of the first occurrence of a substring.
replace(char oldChar, char newChar)Replaces characters in the string.
split(String regex)Splits the string into an array of substrings.
contains(String sequence)Checks if the string contains a sequence.
startsWith(String prefix)Checks if the string starts with the specified prefix.
endsWith(String suffix)Checks if the string ends with the specified suffix.
toCharArray()Converts the string to a character array.
valueOf(Object obj)Returns the string representation of an object.
format(String format, Object... args)Returns a formatted string.
compareTo(String other)Compares two strings lexicographically.
compareToIgnoreCase(String other)Compares two strings lexicographically, ignoring case.

This should give you a solid overview of the String methods in Java. Let me know if you'd like more details or examples on any specific method!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql