String (Java.lang.String)#

Immutable Datatype

Instantiate#

String s;

Methods (s)#

  • contains() - returns true/ false if subString is present in String

    s.contains(String subString);
  • indexOf() - returns the index of the subString/ character

    int n = s.indexOf(String subString);
    int n = s.indexOf(char ch);
  • replace() - replace old char wth new char for all occurance of old char

    s.replace(char old,char new);
    • replaceFirst() - replace old char wth new char for first occurance of old char
      s.replaceFirst(char old,char new);
  • matches() - tells whether or not the string matches the given regular expression, returns boolean

    s.matches('regex');
  • split() - split string at matching regex into string array

    String[] array = s.split('regex');
  • startsWith() - method determines whether a string begins with the characters of a specified string, returning true or false

    s.startsWith(String prefix);
    s.startsWith(String prefix,int position); // starts searching from the position index
  • endsWith() - method determines whether a string ends with the characters of a specified string, returning true or false

    s.endsWith(String suffix);
  • toLowerCase() - converts string to lowercase string

    String str = s.toLowerCase();
  • toUpperCase() - converts string to uppercase string

    String str = s.toUpperCase();
  • trim() - removes leading and training whitespace

    String str = s.trim();
  • join() - returns string joined with given delimiter

    String str = s.join(CharSequence delimiter,CharSequence elements); //elements here are strings
  • compareTo() - returns int value after comparing two strings lexographically

    int n = s1.compareTo(s2);

  • Character.isLetter() - returns true/false if char is Letter
    Character.isLetter(char ch);
  • Character.isDigit() - returns true/false if char is digit
    Character.isDigit(char ch);
  • Character.isWhitespace() - returns true/false if char is space
    Character.isWhitespace(char ch);
  • Character.isUpperCase() - returns true/false if char is a Uppercase letter
    Character.isUpperCase(char ch);
  • Character.isLowerCase() - returns true/false if char is a Lowercase letter
    Character.isLowerCase(char ch);