Java2all.com  

Methods & Examples





Share This Topic -

String Methods in java:

Example :

 import java.util.*;
class str1
{
	public static void main(String args[])
	{
		String s1 = "Bhagirath";
		System.out.println("S1 = " + s1);
		int length = s1.length();
		System.out.println("S1 lenth = " + length);
		System.out.println("S1 lowercase = " + s1.toLowerCase());
		System.out.println("S1 uppercase = " + s1.toUpperCase());
		System.out.println("S1 replace a with z = " + s1.replace('a','z'));
		System.out.println("S1 indexOf('e')= " + s1.indexOf('e'));
		System.out.println("S1 lastindexof('e') = " + s1.lastIndexOf('e'));
		String s2 = "ViewSonic";
		System.out.println("S2 = " + s2);
		System.out.println("S1 and S2 trim = " + s1.trim() + s2.trim());
		System.out.println("S1 and S2 equals = " + s1.equals(s2));
		System.out.println("S1 and S2 equals ignoring case = " + s1.equalsIgnoreCase(s2));
		System.out.println("S1 and S2 compareTo = " + s1.compareTo(s2));
		System.out.println("S1 and S2 concate = " + s1.concat(s2));
		System.out.println("S1 substring(n) = " + s1.substring(5));
		System.out.println("S1 substring(n,m) = " + s1.substring(5,8));
		System.out.println("S1 toString() = " + s1.toString());
		int i = 100;
		System.out.println("S1.valueOf(variable) = " + (s1.valueOf(i)).length()); // converts the parameter to string 
		System.out.println("Start with " + s1.startsWith("P"));
		System.out.println("Start with " + s1.endsWith("y"));
		
	}
}

 
Output :
 
S1 = Bhagirath
S1 lenth = 9
S1 lowercase = bhagirath
S1 uppercase = BHAGIRATH
S1 replace a with z = Bhzgirzth
S1 indexOf('e')= -1
S1 lastindexof('e') = -1
S2 = ViewSonic
S1 and S2 trim = BhagirathViewSonic
S1 and S2 equals = false
S1 and S2 equals ignoring case = false
S1 and S2 compareTo = -20
S1 and S2 concate = BhagirathViewSonic
S1 substring(n) = rath
S1 substring(n,m) = rat
S1 toString() = Bhagirath
S1.valueOf(variable) = 3
Start with false
Start with false
 import java.util.*;
class str4
{
	public static void main(String args[])
	{
		String s = "This is a dAmo of the getChars method.";
		int start = 10;
		int end = 14;
		char buf[] = new char[10];
		//System.out.println("Character at 10 = " + s.charAt(10));
		s.getChars(start, end, buf,0);	
		System.out.println(buf);
		s.getChars(start, end, buf,5);	
		System.out.println(buf);
		
		byte bt [] = new byte[10];
		s.getBytes(start, end, bt,0);	
		System.out.println(bt[0]);
		System.out.println(bt[1]);
		System.out.println(bt[2]);
		System.out.println(bt[3]);
		
		char buf1[] = s.toCharArray();	
		System.out.println(buf1);
		
	}
}

 
Output :
 
 jav      
 jav  jav 
32
74
97
118
Welcome to Java2all

 

 import java.util.*;
class str5
{
	public static void main(String args[])
	{
		String s1 = "Rome was not built in a not day";
		System.out.println("S1 = " + s1);
		/*System.out.println("S1 = " + s1.indexOf('o'));
		System.out.println("S1 = " + s1.indexOf("not"));
		System.out.println("S1 = " + s1.indexOf('o',5));
		System.out.println("S1 = " + s1.indexOf("not", 15));
		
		System.out.println("S1 lastIndexOf= " + s1.lastIndexOf('o'));
		System.out.println("S1 lastIndexOf= " + s1.lastIndexOf("not"));
		System.out.println("S1 lastIndexOf= " + s1.lastIndexOf('o',15));
		System.out.println("S1 lastIndexOf= " + s1.lastIndexOf("not", 15));
		*/
		String s2 = "Rome was not built in a Not day";
		System.out.println("S2 = " + s2);
		//System.out.println("S1 = " + s1.indexOf("not"));
		//System.out.println("S1 = " + s1.lastIndexOf("not"));
		System.out.println("Region Matches = ");
		boolean b1 = s1.regionMatches(false,9,s2,24,3);
		System.out.println("b1 = " + b1);
		
	
	}
}

 
Output :
 
S1 = Rome was not built in a not day
S2 = Rome was not built in a Not day
Region Matches = 
b1 = false
 
 import java.util.*;
class str6 
{
	public static void main(String args[]) 
	{
		String s1 = "Hello";
		String s2 = new String(s1);
		System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
		System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
		
		
	}
}

 
Output :
 
Hello equals Hello -> true
Hello == Hello -> false