Skip to main content

Command Palette

Search for a command to run...

String in-built basic and important functions to remember .

Published
•2 min read•View as Markdown
String in-built basic and important functions to remember .
H

I am a engineering student who's determined to change the time ahead .

  1. charAt(int index): Returns the character at the specified index.

     javaCopy codechar charAtIndex = myString.charAt(3);
    
  2. length(): Returns the length of the string.

     javaCopy codeint strLength = myString.length();
    
  3. substring(int beginIndex): Returns a substring starting from the specified index.

     javaCopy codeString subString = myString.substring(2);
    
  4. substring(int beginIndex, int endIndex): Returns a substring within the specified range.

     javaCopy codeString subString = myString.substring(2, 5);
    
  5. toLowerCase(): Converts all characters in the string to lowercase.

     javaCopy codeString lowerCaseString = myString.toLowerCase();
    
  6. toUpperCase(): Converts all characters in the string to uppercase.

     javaCopy codeString upperCaseString = myString.toUpperCase();
    
  7. trim(): Removes leading and trailing whitespaces from the string.

     javaCopy codeString trimmedString = myString.trim();
    
  8. equals(Object another): Compares the content of two strings for equality.

     javaCopy codeboolean isEqual = myString.equals(anotherString);
    
  9. equalsIgnoreCase(String another): Compares two strings for equality, ignoring case differences.

     javaCopy codeboolean isEqualIgnoreCase = myString.equalsIgnoreCase(anotherString);
    
  10. startsWith(String prefix): Checks if the string starts with the specified prefix.

    javaCopy codeboolean startsWithPrefix = myString.startsWith("abc");
    
  11. endsWith(String suffix): Checks if the string ends with the specified suffix.

    javaCopy codeboolean endsWithSuffix = myString.endsWith("xyz");
    
  12. indexOf(int ch): Returns the index of the first occurrence of the specified character.

    javaCopy codeint indexOfChar = myString.indexOf('a');
    
  13. lastIndexOf(int ch): Returns the index of the last occurrence of the specified character.

    javaCopy codeint lastIndexOfChar = myString.lastIndexOf('a');
    
  14. contains(CharSequence sequence): Checks if the string contains the specified sequence of characters.

    javaCopy codeboolean containsSequence = myString.contains("abc");
    
  15. replace(char oldChar, char newChar): Replaces all occurrences of a specified character with another character.

    javaCopy codeString replacedString = myString.replace('a', 'b');
    

More from this blog

Harsehrawat's blog

52 posts