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); } }
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); } }
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); } }
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); } }
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); } }
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)); } }
equalsIgnoreCase(String anotherString)
class Test { public static void main(String[] args) { String str1 = "alegrutechblog"; String str2 = "AleGruTechBlog"; System.out.println(str1.equalsIgnoreCase(str2)); } }
int length()
Returns the length of this string.
class Test { public static void main(String[] args) { String str = "alegrutechblog"; System.out.println(str.length()); } }
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); } }
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); } }
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); } }