Working With Strings in Java – Part 2

The java.lang.String class provides many useful methods for dealing with a sequence of char values. This tutorial is part 2 of the series. Read part 1 to learn about other methods that the String class provides.

toLowerCase()

Converts all of the characters in this String to lower case using the rules of the default locale.

class Test {
    
  public static void main(String[] args) {
    String str = "ALEGRUTECHBLOG";
    String strInLowerCase = str.toLowerCase();
        
    System.out.println(strInLowerCase);
  }
}
Output: alegrutechblog
 

toUpperCase()

Converts all of the characters in this String to the upper case using the rules of the default locale.

 
class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblog";
    String strInUpperCase = str.toUpperCase();
    
    System.out.println(strInUpperCase);
  }
}
Output: ALEGRUTECHBLOG
 

String valueOf(primitive data type x)

Returns the string representation of the passed data type argument.

class Test {
    
  public static void main(String[] args) {
    int number = 10;
    String numString = String.valueOf(number); // number to string representation
        
    System.out.println(numString);
  }
}
Output: 10
 

copyValueOf(char[] data)

Returns a String that represents the character sequence in the array specified.

class Test {
    
  public static void main(String[] args) {
    char[] chArray = {'a', 'l', 'e', 'g', 'r', 'u'};
    String str = String.copyValueOf(chArray);
        
    System.out.println(str);
  }
}
Output: alegru
 

endsWith(String suffix)

Tests if this string ends with the specified suffix.

 
class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblog";
    boolean endsWithBlog = str.endsWith("blog");
     
    System.out.println(endsWithBlog);
  }
}
Output: true
 

equals(Object anObject)

Compares this string to the specified object.

class Test {
    
  public static void main(String[] args) {
    String str1 = "alegrutechblog";
    String str2 = "alegrutechblog";
    String str3 = "techblog";
        
    System.out.println(str1.equals(str2));
    System.out.println(str1.equals(str3));
  }
}
Output: true false
 

equalsIgnoreCase(String anotherString)

Compares this String to another String, ignoring case considerations.
 
class Test {
  
  public static void main(String[] args) {
    String str1 = "alegrutechblog";
    String str2 = "AleGruTechBlog";
        
    System.out.println(str1.equalsIgnoreCase(str2));
  }
}
Output: true
 

int length()

Returns the length of this string.

class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblog";
    System.out.println(str.length());
  }
}
Output: 14
 

replace(char oldChar, char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblog";
    String newString = str.replace('e', 'W');
        
    System.out.println(newString);
  }
}
Output: alWgrutWchblog
 

String[] split(String regex)

Splits this string around matches of the given regular expression.

class Test {
    
  public static void main(String[] args) {
    String str = "tech-blog";
    String[] strArray = str.split("-");
    String firstElement = strArray[0];
    String secondElement = strArray[1];
     
    System.out.println(firstElement);
    System.out.println(secondElement);
  }
}
Output: tech blog
 

startsWith(String prefix)

Tests if this string starts with the specified prefix.

class Test {
    
  public static void main(String[] args) {
    String str = "alegrutechblog";
    boolean startsWithAle = str.startsWith("ale");
      
    System.out.println(startsWithAle);
  }
}
Output: true
 
This was part 2 of working with the String class in Java. Read part 1 to learn about other methods that the String class provides.
 

Leave a Reply

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