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

A. String creation in Java

String_Ex1.java

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

Sample Output

Hello Marslogitek




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 = "marslogitek.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 = "marslogitek";
String text2 = "com";
String ans1 = "marslogitek".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

marslogitek.com
marslogitek.com
marslogitek.com
marslogitek.com




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

String_Ex5.java

public class String_Ex5 {
public static void main(String args[]) {
String text = "marslogitek";
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 : 111
Value for index '5' is : o

First index of 'e' : 9
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 = "marslogitek";
int val1 = text.compareTo("marslogitek");
int val2 = text.compareTo("marslogitek");
int val3 = text.compareToIgnoreCase("marsLOGITEK");
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
0
0




G. contentEquals() in Java

String_Ex7.java

public class String_Ex7 {
public static void main(String args[]) {
String text = "marslogitek";

boolean val1 = text.contentEquals("mars");
boolean val2 = text.contentEquals("marslogitek");
boolean val3 = text.contentEquals("Marslogitek");
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 = "marslogitek";

int codeAt = text.codePointAt(1);
int codeAtBefore = text.codePointBefore(2);
int codeCount = text.codePointCount(1, 6);
boolean contain1 = text.contains("log");
boolean contain2 = text.contains("lo g");
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 = "marslogitek";

boolean start = text.startsWith("mars");
boolean end = text.endsWith("tek");
boolean start_case = text.startsWith("mar");
boolean end_case = text.endsWith("tek1");

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
false
true
true




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

String_Ex10.java

public class String_Ex10 {
public static void main(String args[]) {
String text = "marslogitek";

String str1 = text.replace('m', 'M');
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

Marslogitek
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
System.out.println(str1[i]);
}
System.out.println();
for(int i=1; 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 = "marslogitek";

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

slogitek
slogi
slogi

marslogitek




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

String_Ex13.java

public class String_Ex13 {
public static void main(String args[]) {
String text = "marslogitek";

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
System.out.print(charArray[i] + " ");
}
}
}

Sample Output

MARSLOGITEK
marslogitek
m a r s l o g i t e k 




N. equals() & equalsIgnoreCase() in Java

String_Ex14.java

public class String_Ex14 {
public static void main(String args[]) {
String text = "marslogitek";

if(text.equals("marslogitek")) 
System.out.println("The Both are Equal");
else
System.out.println("The Both are not Equal");
if(text.equalsIgnoreCase("MaRsLogiTeK"))
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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.