Java String Manipulation

  1. String creation in Java
  2. charArray to String in Java
  3. isEmpty() & length() in Java
  4. Different way to use concat() in Java
  5. charAt(), indexOf() & lastIndexOf() in Java
  6. compareTo() & compareToIgnoreCase() in Java
  7. contentEquals() in Java
  8. codePointAt(), codePointBefore(), codePointCount() & contains() in Java
  9. startsWith() & endsWith() in Java
  10. replace(), replaceAll() & replaceFirst() in Java
  11. split() & StringArray.length in Java
  12. substring(), subSequence() & intern() in Java
  13. toUpperCase(), toLowerCase() & toCharArray() in Java
  14. equals() & equalsIgnoreCase() in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. String creation in Java


String_Ex1.java

public class String_Ex1 {
	
	public static void main(String args[]) {
		
		String text = "Hello SampleCodez";
		
		System.out.println(text);
	}
}

Sample Output

Hello SampleCodez




B. charArray to String in Java


String_Ex2.java

public class String_Ex2 {
	
	public static void main(String args[]) {
		
		char[] charArray = {'J', 'A', 'V', 'A' };
		
		String text = new String(charArray);
		
		System.out.println(text);
	}
}

Sample Output

JAVA




C. isEmpty() & length() in Java


String_Ex3.java

public class String_Ex3 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez.com";
		
		if(text.isEmpty());
		else		
			System.out.println(text.length());
	}
}

Sample Output

15




D. Different way to use concat() in Java


String_Ex4.java

public class String_Ex4 {
	
	public static void main(String args[]) {
		
		String text1 = "samplecodez";
		String text2 = "com";
		
		String ans1 = "samplecodez".concat(".").concat("com");
		String ans2 = text1.concat(".").concat("com");
		String ans3 = text1.concat(".").concat(text2);
		String ans4 = text1 + "." + text2;
		
		System.out.println(ans1);
		System.out.println(ans2);		
		System.out.println(ans3);		
		System.out.println(ans4);		
	}
}

Sample Output

samplecodez.com
samplecodez.com
samplecodez.com
samplecodez.com




E. charAt(), indexOf() & lastIndexOf() in Java


String_Ex5.java

public class String_Ex5 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
		
		int index = text.charAt(5);
		char ch = text.charAt(5);
		
		int indexOf = text.indexOf('e');
		int lastIndexOf = text.lastIndexOf('e');
				
		System.out.println("ASCII value : " + index);
		System.out.println("Value for index '5' is : "+ ch);
		
		System.out.println("\nFirst index of 'e' : " + indexOf);
		System.out.println("Last index of 'e' : " + lastIndexOf);
	}
}

Sample Output

ASCII value : 101
Value for index '5' is : e

First index of 'e' : 5
Last index of 'e' : 9




F. compareTo() & compareToIgnoreCase() in Java


String_Ex6.java

public class String_Ex6 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
		
		int val1 = text.compareTo("samplecodez");
		int val2 = text.compareTo("Samplecodez");
		int val3 = text.compareToIgnoreCase("sampleCODEZ");
		
		System.out.println("If value is 0, both are equal. Otherwise not equal.");
 
		System.out.println("\n" + val1);
		System.out.println(val2);
		System.out.println(val3);
	}
}

Sample Output

If value is 0, both are equal. Otherwise not equal.

0
32
0




G. contentEquals() in Java


String_Ex7.java

public class String_Ex7 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
 
		boolean val1 = text.contentEquals("sample");
		boolean val2 = text.contentEquals("samplecodez");
		boolean val3 = text.contentEquals("Samplecodez");
		
		System.out.println("If value is 'true', both are equal..");
		System.out.println("If value is 'false', both are not equal..\n");
 
		System.out.println(val1);
		System.out.println(val2);
		System.out.println(val3);
	}
}

Sample Output

If value is 'true', both are equal..
If value is 'false', both are not equal..

false
true
false




H. codePointAt(), codePointBefore(), codePointCount() & contains() in Java


String_Ex8.java

public class String_Ex8 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
 
		int codeAt = text.codePointAt(1);
		int codeAtBefore = text.codePointBefore(2);
		int codeCount = text.codePointCount(1, 6);
		boolean contain1 = text.contains("code");
		boolean contain2 = text.contains("co De");
				
		System.out.println("ASCII value of 'a' : " + codeAt);
		System.out.println("ASCII value of 'a' : " + codeAtBefore);
		System.out.println("Char count is : " + codeCount);
		
		System.out.println("\nCheck the content is " +
				"available in the string or not : " + contain1);
		System.out.println("Check the content is " +
				"available in the string or not : " + contain2);
	}
}

Sample Output

ASCII value of 'a' : 97
ASCII value of 'a' : 97
Char count is : 5

Check the content is available in the string or not : true
Check the content is available in the string or not : false




I. startsWith() & endsWith() in Java


String_Ex9.java

public class String_Ex9 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
 
		boolean start = text.startsWith("samp");
		boolean end = text.endsWith("dez");
		
		boolean start_case = text.startsWith("sAmp");
		boolean end_case = text.endsWith("Dez");
 
		System.out.println("true - the content is available in the string");
		System.out.println("false - the content is not available in the string\n");
		
		System.out.println(start);
		System.out.println(end);
		
		System.out.println(start_case);
		System.out.println(end_case);
	}
}

Sample Output

true - the content is available in the string
false - the content is not available in the string

true
true
false
false




J. replace(), replaceAll() & replaceFirst() in Java


String_Ex10.java

public class String_Ex10 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
 
		String str1 = text.replace('s', 'S');
		String str2 = text.replaceAll(text, "Hello World Hello");
		String str3 = str2.replaceFirst("Hello", "Java");
		
		System.out.println(str1);
		System.out.println(str2);
		System.out.println(str3);
	}
}

Sample Output

Samplecodez
Hello World Hello
Java World Hello




K. split() & StringArray.length in Java


String_Ex11.java

public class String_Ex11 {
	
	public static void main(String args[]) {
		
		String text = "aaa bbb ccc aaa bbb ccc aaa bbb ccc";
 
		String[] str1 = text.split("aaa");
		String[] str2 = text.split("bbb", 3);
		
		for(int i=1; i<str1.length; i++) {
			
			System.out.println(str1[i]);
		}
		
		System.out.println();
		
		for(int i=1; i<str2.length; i++) {
			
			System.out.println(str2[i]);
		}
	}
}

Sample Output

 bbb ccc 
 bbb ccc 
 bbb ccc

 ccc aaa 
 ccc aaa bbb ccc




L. substring(), subSequence() & intern() in Java


String_Ex12.java

public class String_Ex12 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
 
		String str1 = text.substring(3);
		String str2 = text.substring(3, 8);
		CharSequence str3 = text.subSequence(3, 8);
		
		String str4 = text.intern();
		
		System.out.println(str1);
		System.out.println(str2);
		System.out.println(str3);
		System.out.println("\n" + str4);
	}
}

Sample Output

plecodez
pleco
pleco

samplecodez




M. toUpperCase(), toLowerCase() & toCharArray() in Java


String_Ex13.java

public class String_Ex13 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
 
		String str1 = text.toUpperCase();
		String str2 = str1.toLowerCase();
		
		char[] charArray = text.toCharArray();
		
		System.out.println(str1);
		System.out.println(str2);
		
		for(int i=0; i<charArray.length; i++) {
			
			System.out.print(charArray[i] + " ");
		}
	}
}

Sample Output

SAMPLECODEZ
samplecodez
s a m p l e c o d e z 




N. equals() & equalsIgnoreCase() in Java


String_Ex14.java

public class String_Ex14 {
	
	public static void main(String args[]) {
		
		String text = "samplecodez";
 
		if(text.equals("samplecodez")) 
			System.out.println("The Both are Equal");
		else
			System.out.println("The Both are not Equal");
		
		if(text.equalsIgnoreCase("SaMpLeCoDeZ"))
			System.out.println("The Both are Equal");
		else
			System.out.println("The Both are not Equal");
	}
}

Sample Output

The Both are Equal
The Both are Equal






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com