🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

String Methods

Commonly Used String Methods in Java

Commonly Used String Methods in Java

The String class in Java provides a variety of methods for manipulating strings. Here are some commonly used String methods:

  1. Length of a String: length()
    Returns the length (number of characters) of a string.
    String str = "Hello, World!";
    int length = str.length(); // length will be 13
  2. Accessing Characters: charAt(int index)
    Returns the character at the specified index.
    char firstChar = str.charAt(0); // firstChar will be 'H'
  3. Substring: substring(int beginIndex) and substring(int beginIndex, int endIndex)
    - substring(int beginIndex): Returns a substring starting from the specified index.
    - substring(int beginIndex, int endIndex): Returns a substring from beginIndex (inclusive) to endIndex (exclusive).
    String substring1 = str.substring(7);       // substring1 will be "World!"
    String substring2 = str.substring(0, 5);    // substring2 will be "Hello"
  4. Concatenation: concat(String str) or + operator
    - concat(String str): Concatenates the specified string to the end of the current string.
    - + operator: Concatenates two strings.
    String str1 = "Hello";
    String str2 = "World";
    String result = str1.concat(", " + str2); // result will be "Hello, World"
  5. Comparison: equals(String anotherString) and equalsIgnoreCase(String anotherString)
    - equals(String anotherString): Compares the content of two strings for equality.
    - equalsIgnoreCase(String anotherString): Compares two strings, ignoring case.
    String s1 = "Hello";
    String s2 = new String("Hello");
    boolean isEqual = s1.equals(s2); // isEqual will be true
  6. Search: indexOf(String str) and lastIndexOf(String str)
    - indexOf(String str): Returns the index within the string of the first occurrence of the specified substring.
    - lastIndexOf(String str): Returns the index within the string of the last occurrence of the specified substring.
    int index1 = str.indexOf("World");     // index1 will be 7
    int index2 = str.lastIndexOf("l");     // index2 will be 10
  7. Changing Case: toUpperCase() and toLowerCase()
    Converts the entire string to uppercase or lowercase.
    String upperCaseStr = str.toUpperCase();     // upperCaseStr will be "HELLO, WORLD!"
    String lowerCaseStr = str.toLowerCase();     // lowerCaseStr will be "hello, world!"
  8. Trimming: trim()
    Removes leading and trailing whitespaces from the string.
    String spacedStr = "    Hello, World!    ";
    String trimmedStr = spacedStr.trim(); // trimmedStr will be "Hello, World!"
  9. Replacing Characters: replace(char oldChar, char newChar)
    Replaces all occurrences of a specified character with another character.
    String originalStr = "apple";
    String replacedStr = originalStr.replace('p', 'b'); // replacedStr will be "abble"
  10. Splitting: split(String regex)
    Splits the string into an array of substrings based on a specified regular expression.
    String sentence = "The quick brown fox";
    String[] words = sentence.split(" "); // words will be {"The", "quick", "brown", "fox"}

These are just a few examples of the many methods available in the String class. Understanding and using these methods effectively can simplify string manipulation tasks in Java.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.