The String class in Java

The String class in Java is used to represent strings of characters. It is one of the most commonly used classes in the Java API. In this post, I will explain the various methods of the String class with examples.

  1. length(): Returns the length of the string in terms of the number of characters.

Example:

String str = "Hello World";
int length = str.length(); // length is 11
  1. charAt(int index): Returns the character at the specified index.

Example:

String str = "Hello World";
char ch = str.charAt(0); // ch is 'H'
  1. substring(int beginIndex, int endIndex): Returns a new string that is a substring of the original string. The substring starts at the specified beginIndex and extends to the character at the endIndex – 1 index.

Example:

String str = "Hello World";
String substr = str.substring(0, 5); // substr is "Hello"
  1. concat(String str): Concatenates the specified string to the end of the current string.

Example:

String str = "Hello";
String newStr = str.concat(" World"); // newStr is "Hello World"
  1. equals(Object obj): Compares the current string to the specified object for equality.

Example:

String str1 = "Hello World";
String str2 = "Hello World";
boolean result = str1.equals(str2); // result is true
  1. compareTo(String anotherString): Compares the current string to the specified string lexicographically.

Example:

String str1 = "Hello World";
String str2 = "Hello Java";
int result = str1.compareTo(str2); // result is -9
  1. indexOf(int ch): Returns the index of the first occurrence of the specified character in the string.

Example:

String str = "Hello World";
int index = str.indexOf('o'); // index is 4
  1. toUpperCase(): Returns a new string in all uppercase.

Example:

String str = "Hello World";
String upper = str.toUpperCase(); // upper is "HELLO WORLD"
  1. toLowerCase(): Returns a new string in all lowercase.

Example:

String str = "Hello World";
String lower = str.toLowerCase(); // lower is "hello world"
  1. replace(char oldChar, char newChar): Returns a new string with all occurrences of the oldChar replaced with the newChar.

Example:

String str = "Hello World";
String newStr = str.replace('o', 'a'); // newStr is "Hella Warld"
  1. trim(): Returns a new string with all leading and trailing whitespaces removed.

Example:

String str = "   Hello World   ";
String newStr = str.trim(); // newStr is "Hello World"
  1. startsWith(String prefix): Returns true if the string starts with the specified prefix.

Example:

String str = "Hello World";
boolean result = str.startsWith("He"); // result is true
  1. endsWith(String suffix): Returns true if the string ends with the specified suffix.

Example:

String str = "Hello World";
boolean result = str.endsWith("ld"); // result is true
  1. split(String regex): Splits the string into an array of substrings based on the specified regular expression.

Example:

String str = "Hello World";
String[] arr = str.split(" "); // arr is ["Hello", "World"]
  1. valueOf(Object obj): Returns the string representation of the specified object.

Example:

int num = 10;
String str = String.valueOf(num); // str is "10"
  1. format(String format, Object... args): Returns a formatted string using the specified format string and arguments.

Example:

String str = String.format("The value of x is %d and the value of y is %d", 5, 10); 
// str is "The value of x is 5 and the value of y is 10"
  1. matches(String regex): Returns true if the string matches the specified regular expression.

Example:

String str = "Hello World";
boolean result = str.matches(".*World.*"); // result is true
  1. intern(): Returns a canonical representation for the string object.

Example:

String str1 = "Hello World";
String str2 = new String("Hello World");
boolean result = (str1 == str2.intern()); // result is true

These are some of the most commonly used methods of the String class in Java. There are a few more methods available in the String class, but these should cover most of your needs when working with strings in Java.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *