String Methods in Java?
public class StringDemo {
public static void main(String args[]){
String str="welcome";
System.out.println("Original String="+str);
length() method: Return number of characters in a string
System.out.println("Length of a string="+str.length());
toUpperCase() method: Converts string into Uppercase
System.out.println("String in uppercase="+str.toUpperCase());
toLowerCase() method: Converts string into Lowercase
System.out.println("String in lowercase="+str.toLowerCase());
replace() method: It will replace specified character(s) in a string
replace method have 2 arguments:
First Argument - Specific character(s) you want to replace
Second Argument - Replaces with our specified character
System.out.println("String Replace="+str.replace("l","Z"));
replaceAll() method: Replaces all characters in string
replaceAll() method have 2 arguments:
First Argument - Specific character(s) you want to replace
Second Argument - Replaces all characters in string with our specified character
System.out.println("String ReplaceAll="+str.replaceAll("e","F"));
System.out.println("String concat="+str.concat("to Java"));
String str1=" world ";
System.out.println("String contains="+str.contains(str1));
System.out.println("String ReplaceFirst="+str.replaceFirst("l","Z"));
System.out.println("String Trim="+str1.trim());
System.out.println("String equals method="+str.equals(str1));
System.out.println("String equalsIgnoreCase="+str.equalsIgnoreCase(str1));
System.out.println("String contentEquals="+str.contentEquals(str1));
System.out.println("String Starts With="+str.startsWith("w"));
System.out.println("String Ends With="+str.endsWith("e"));
System.out.println("String substring with begin index="+str.substring(1));
System.out.println("String substring with begin and end index="+str.substring(1,3));
System.out.println("String indexOf="+str.indexOf(2));
System.out.println("String indexOf with String ="+str.indexOf("l"));
System.out.println("String indexOf from character="+str.indexOf("e",1));
System.out.println("String lastindexOf with string="+str.lastIndexOf("e"));
System.out.println("String lastindexOf="+str.lastIndexOf(4));
System.out.println("String lastindexOf from character="+str.lastIndexOf("c",6));
System.out.println("String isEmpty="+str.isEmpty());
// System.out.println("String compareTo="+str.compareTo(str1));// System.out.println("String compareToIgnoreCase="+str.compareToIgnoreCase(str1)); System.out.println("String toCharArray="+str.toCharArray());
String exp="one two three";
System.out.println("String split ="+exp.split(" "));
for(int i=0;i<str.length();i++){
System.out.print(str.charAt(i)+" ");
}
System.out.println("\nAgain String="+str);
}
No comments:
Post a Comment
Please comment below to feedback or ask questions.