|
|
Java Examples
OOPS |
Exception |
RMI |
String |
Thread |
io |
Util |
Net |
AWT |
JDBC |
|
|
| STRING PROGRAM:: |
| 1.String Operations Program |
public class StrOperations {
public static void main(String[] args) {
String cs,bs,str;
char[] chars = {'a','b','c','d'}; // CHAR ARRAY ASSIGNMENT TO STRING
cs = new String(chars,0,chars.length);
byte[] bytes = {65,66,67,68}; // BYTE ARRAY ASSIGNMENT TO STRING
bs = new String(bytes,0,bytes.length);
str = "SK dOTCOM TECHNOLOGIES"; // LITERAL ARRAY ASSIGNMENT TO STRING
String str1 = "SK dOTCOM "+"TECHNOLOGIES"; // CONCATENATION OF STRING WITH STRING
System.out.println();
System.out.println("String concatenation with String str1 = :"+str1);
System.out.println();
String str2 = "SK dOTCOM "+2 + 2 ; // CONCATENATION OF STRING WITH OTHER TYPES
System.out.println("String concatenation with other data type str2 = :"+str2);
System.out.println();
System.out.println("SubString of str1.substring(0,10) :"+str1.substring(0,10));
System.out.println();
System.out.println("Concatenation using Method cs.concat(bs) :"+cs.concat(bs));
System.out.println();
String st3="Hello , Have a Nice Day ";
System.out.println("Replace character 'a' by 'o' in [Hello , Have a Nice Day]:");
System.out.println(st3.replace('a','o'));
System.out.println();
st3=" Hello , Have a Nice Day ";
System.out.println("Trim to String size suppress the white space :");
System.out.println(st3.trim());
System.out.println();
st3=" Hello , Have a Nice Day ";
System.out.println("Convert to uppercase :" + st3.toUpperCase());
System.out.println();
st3=" HELLO , HAVE A NICE DAY ";
System.out.println("Convert to Lower case :" + st3.toLowerCase());
System.out.println();
System.out.println("Obtaining String length :" + st3.length());
System.out.println();
}
}
|
| 2.String Comparission Program |
public class StrComparision {
public static void main(String[] args) {
String str1 = "Sk dotcom Technologies ";
String str2 = "Sk dotcom Technologies ";
String str3 = "SK DOTCOM TECHNOLOGIES ";
System.out.println();
System.out.println(" STR1 EQUALS STR2 :"+str1.equals(str2));
System.out.println();
System.out.println(" STR1 EQUALS STR3 :"+str1.equals(str3));
System.out.println();
System.out.println(" STR1 EQUALSIGNORECASE STR3 :"+str1.equalsIgnoreCase(str3));
System.out.println();
System.out.println(" Region Macthes in str1 & 'dotcom' :"+str1.regionMatches(3,"dotcom",0,6));
System.out.println();
System.out.println(" Region Macthes ignore case in str1 & 'sk' :"+str1.regionMatches(true,0,"sk",0,2));
System.out.println();
System.out.println("-Nice Day- starts with Nice " + "Nice Day".startsWith("Nice"));
System.out.println();
System.out.println("-Nice Day- ends with Day " + "Nice Day".endsWith("Day"));
System.out.println();
String s1= " Be Happy ";
String s2 = new String(s1);
System.out.println(" s1.equals(s2) = "+ s1.equals(s2));
System.out.println();
System.out.println(" s1==s2 = "+ (s1==s2));
System.out.println();
System.out.println("index of 'H' in s1 = "+ s1.indexOf('H'));
}
}
|
| 3.String Swapping |
class StrSwapping
{
static String name[] = {"Chennai", "Bangalore", "Andhra", "Kerala", "Mumbai","Delhi"};
public static void main(String args[])
{
int size = name.length;
String temp = null;
for(int i = 0; i<size; i++)
{
for(int j = i+1; j<size; j++)
{
if(name[j].compareTo(name[i]) < 0)
{
//SWAP THE STRINGS
temp = name[i];
name[i] = name[j];
name[j] = temp;
}
}
}
for(int i = 0; i<size; i++)
{
System.out.println(name[i]);
}
}
}
|
| 4.Serialization program |
import java.io.*;
public class Serialization {
public static void main(String[] args) throws IOException {
Myclass ob = new Myclass("Sachin","Bombay","Cricket");
System.out.println("My class Object is : "+ob);
FileOutputStream fos = new FileOutputStream("e:/serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(ob);
oos.flush();
oos.close();
Myclass ob2;
FileInputStream fis = new FileInputStream("e:/serial.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
ob2= (Myclass)ois.readObject();
System.out.println("After Deserialization My class Object is : "+ob);
System.out.println("My class Object is : "+ob);
}
}
class Myclass {
String name,city,game;
public Myclass(String name,String city,String game) {
this.name = name;
this.city = city;
this.game = game;
}
public String toString() {
return "Nam : " + name + "City : " + city + "Game : " + game ;
}
}
|
|
Back to Top |
|