String:

In Java, a string is an object that represents a sequence of characters. The String class in Java is used to create and manipulate strings. Strings in Java are immutable, which means once a string object is created, its value cannot be changed.

Here are some key concepts related to strings in Java:

  1. String Creation: Strings can be created using the String class constructor or by assigning a string literal to a variable.
java
String str1 = new String("Hello"); String str2 = "World";
  1. Concatenation: Strings can be concatenated using the + operator or the concat() method.
java
String result1 = str1 + str2; String result2 = str1.concat(str2);
  1. String Length: The length() method returns the number of characters in a string.
java
int length = str1.length();
  1. Accessing Characters: Individual characters in a string can be accessed using the charAt() method.
java
char ch = str1.charAt(0); // Gets the character at index 0
  1. Substring: The substring() method is used to extract a portion of a string based on specified start and end indexes.
java
String substring = str1.substring(1, 4); // Extracts characters from index 1 to 3
  1. String Comparison: The equals() method is used to check if two strings have the same content.
java
boolean isEqual = str1.equals(str2);
  1. String Manipulation: The String class provides various methods for manipulating strings, such as toUpperCase(), toLowerCase(), replace(), trim(), etc.
java
String upperCase = str1.toUpperCase(); String replaced = str1.replace('H', 'J');

These are just some of the fundamental concepts related to strings in Java. The String class in Java offers many more methods and functionalities for working with strings.

String All Method:

In Java, the String class provides several useful methods for working with strings. Here is a list of some commonly used methods:

  1. length(): Returns the length of the string.
  2. equals(): Check two string contain same content or not.Click here for more Information
  3. charAt(int index): Returns the character at the specified index.
  4. substring(int beginIndex): Returns a new string that is a substring of the original string, starting from the specified index.(Click here for more Information)Click here for more Information
  5. substring(int beginIndex, int endIndex): Returns a new string that is a substring of the original string, starting from the beginIndex and ending at the endIndex (exclusive).Click here for more Information
  6. indexOf(int ch): Returns the index of the first occurrence of the specified character, or -1 if the character is not found.Click here For More Information
  7. indexOf(int ch, int fromIndex): Returns the index of the first occurrence of the specified character, starting from the specified index.Click here For More Information
  8. indexOf(String str): Returns the index of the first occurrence of the specified substring, or -1 if the substring is not found.Click here For More Information
  9. indexOf(String str, int fromIndex): Returns the index of the first occurrence of the specified substring, starting from the specified index.Click here For More Information
  10. lastIndexOf(int ch): Returns the index of the last occurrence of the specified character, or -1 if the character is not found.
  11. lastIndexOf(int ch, int fromIndex): Returns the index of the last occurrence of the specified character, starting from the specified index.
  12. lastIndexOf(String str): Returns the index of the last occurrence of the specified substring, or -1 if the substring is not found.
  13. lastIndexOf(String str, int fromIndex): Returns the index of the last occurrence of the specified substring, starting from the specified index.
  14. toUpperCase(): Converts the string to uppercase.
  15. toLowerCase(): Converts the string to lowercase.
  16. trim(): Removes leading and trailing whitespace from the string.
  17. startsWith(String prefix): Checks if the string starts with the specified prefix.
  18. endsWith(String suffix): Checks if the string ends with the specified suffix.
  19. contains(CharSequence sequence): Checks if the string contains the specified sequence of characters.
  20. replace(char oldChar, char newChar): Replaces all occurrences of the specified oldChar with the newChar.
  21. replaceAll(String regex, String replacement): Replaces all occurrences of the specified regular expression with the replacement string.