Friday 31 August 2012

string handeling programs in java


string handeling programs in java

//CHANGE CASE FROM UPPER CASE TO LOWER

package p2;

public class ChangeCase {

public ChangeCase() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="this is a test";
System.out.println("original:"+s);
String upper=s.toUpperCase();
String lower=s.toLowerCase();
System.out.println("uppercase:"+upper);
System.out.println("lower:"+lower);
}

}

//CONCATINATION OF  TWO STRING

package p2;

public class ConCat {

public ConCat() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
String longStr ="this could have been"+"a very long line that would have"+"wrap around bt str concat"+
"prevent this";
System.out.println(longStr);
}

}

//CURRENT THREAD DEMO

package p2;

public class CurrentThreadDemo {

public CurrentThreadDemo() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t=Thread.currentThread();
System.out.println("current thread:"+t);
t.setName("my thread");
System.out.println("After name change:"+t);
try{
for(int n=5;n>0;n--){
System.out.println(n);
Thread.sleep(5000);
}
}catch(InterruptedException e){
System.out.println("main thread interrupt");
}
}

}

//EXCEPTION USING JOIN(),ALIVE(),SLEEP()

package p2;

public class DemoJoin {

public DemoJoin() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stubThread
class NewThread implements Runnable{
String name;
Thread t;
NewThread(String threadname){
name =threadname;
t=new Thread(this,name);
System.out.println("new thread"+t);
t.start();
}
public void run(){
try{
for(int i=5;i>0;i--)
System.out.println(name+": "+i);
Thread.sleep(1000);
}
catch(InterruptedException e){
System.out.println(name+"Interrupted");
}
System.out.println(name+"exiting");
}

}
NewThread obj1=new NewThread("one");

NewThread obj2=new NewThread("two");

NewThread obj3=new NewThread("three");
System.out.println("Thread one is alive:"+obj1.t.isAlive());
System.out.println("Thread two is alive:"+obj2.t.isAlive());

System.out.println("Thread three is alive:"+obj3.t.isAlive());

try{
System.out.println("waiting for threads for finish:");
obj1.t.join();
obj2.t.join();
obj3.t.join();
}catch(InterruptedException e){
System.out.println("main thread interrupted");
}
System.out.println("Thread one is alive :"+obj1.t.isAlive());
System.out.println("Thread two is alive :"+obj2.t.isAlive());
System.out.println("Thread three is alive :"+obj3.t.isAlive());
System.out.println("main thread exciting");


}
}

//STRING EQUAL OR NOT EQUAL TO

package p2;

public class EqualNotEqualTo {

public EqualNotEqualTo() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="hello";
String s2=new String(s1);
System.out.println(s1+"equals"+s2+"->"+s1.equals(s2));
System.out.println(s1+"=="+s2+"->"+(s1==s2));
}

}

//STRING USING EQUALS

package p2;

public class equalsDemo {

public equalsDemo() {
// TODO Auto-generated constructor stub
}

/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="hello";
String s2="hello";
String s3="godbye";
String s4="HELLO";
System.out.println(s1+"equals"+s2+"->"+s1.equals(s2));
System.out.println(s1+"equals"+s3+"->"+s1.equals(s3));
System.out.println(s1+"equals"+s4+"->"+s1.equals(s4));
System.out.println(s1+"equals"+s4+"->"+s1.equalsIgnoreCase(s4));


}

}

No comments:

Post a Comment