Strings:
--------
package p1;
public class Program1 {
public static void main(String[] args) {
String s1 = "KTS";
String s2 = new String("KTS");
String s3 = new String(s1);
char[] ch = {'K','T','S'};
String s4 = new String(ch);
String s5 = s1;
s1="Java";
System.out.println("s1 = "+s1);
System.out.println("s2 = "+s2);
System.out.println("s3 = "+s3);
System.out.println("s4 = "+s4);
System.out.println("s5 = "+s5);
}
package p1;
public class Program2 {
public static void main(String[] args) {
String s = "I Love";
//s=s+" Java";
s = s.concat(" Java");
System.out.println("s = "+s);
System.out.println("character at index 2: "+s.charAt(2));
System.out.println(s.contains("Love"));
System.out.println(s+" is starts with I: "+s.startsWith("I"));
System.out.println(s+" is ends with Java: "+s.endsWith("Java"));
System.out.println(s.substring(2));
System.out.println(s.substring(2,6));
String s1 = "i love java";
System.out.println("s equals s1: "+s.equals(s1));
System.out.println("s equals ignore case s1: "+s.equalsIgnoreCase(s1));
}
}
package p1;
public class Program3 {
public static void main(String[] args) {
String s1 = "ant";
String s2 ="Boy";
int i = s1.compareTo(s2);
if(i<0){
System.out.println(s1+" is smaller than "+s2);
}else if (i>0){
System.out.println(s2+" is smaller than "+s1);
}else{
System.out.println("Both are Equal");
}
}
}
package p1;
public class Program4 {
public static void main(String[] args) {
String s = " Welcome to KTS ";
System.out.println("s = "+s);
System.out.println("length = "+s.length());
s = s.trim();
System.out.println("s = "+s);
System.out.println("length = "+s.length());
}
}
package p1;
public class Program5 {
public static void main(String[] args) {
String s = "c,c++,java,c#,php,sql,linux";
String[] courses = s.split(",");
System.out.println("Courses are:");
for(String course : courses){
System.out.println("\t"+course);
}
}
}
---------------------------
package p1;
public class Program6 {
public static void main(String[] args) {
String s = "I Like Java";
System.out.println(s.toLowerCase());
System.out.println(s.toUpperCase());
char[] ch = s.toCharArray();
System.out.println("Array Elemenst are:");
for(char x : ch){
System.out.println(x);
}
}
}
------------------------------------
package p1;
public class Program7 {
public static void main(String[] args) {
String s1 = "I Like JAVA, I Like Coding";
s1 = s1.replace("Like", "Love");
s1 = s1.replace('I', 'i');
System.out.println("s1: "+s1);
}
}
__________________________________________________________
---------------------------------
StringBuffer
package p2;
public class Program1 {
public static void main(String[] args) {
StringBuffer s = new StringBuffer("I Love");
s.append(" Java");
System.out.println("s = "+s);
System.out.println(s.capacity());
System.out.println(s.delete(2, 6));
s.insert(2, "Like");
System.out.println("s = "+s);
s.replace(2, 6, "Love");
System.out.println("s = "+s);
s.reverse();
System.out.println("s = "+s);
}
}
___________________________________________________________________
StringTokenizer:
----------------
package p2;
import java.util.StringTokenizer;
public class Program2 {
public static void main(String[] args) {
String s = "c,c++,java,c#,php,sql,linux";
StringTokenizer st = new StringTokenizer(s, ",");
System.out.println("Elements are:");
while(st.hasMoreTokens()){
System.out.println("\t"+st.nextToken());
}
}
}