Saturday, 2 March 2013

EXAMPLE ON STRING HANDLING FUNCTIONS in JAVA


EXAMPLE -1

// Demonstrate toUpperCase() and toLowerCase().

class ChangeCaseDemo {
  public static void main(String args[])
  {
    String s = "This is a satya.";

    System.out.println("Original: " + s);

    String uppr = s.toUpperCase();
    String lwr = s.toLowerCase();

    System.out.println("Uppercase: " + uppr);
    System.out.println("Lowercase: " + lwr);
  }
}
OUTPUT
C:\SATYA>javac ChangeCaseDemo.java
C:\SATYA>java ChangeCaseDemo

C:\satya>
Original:This is Satya
Uppercase:THIS IS SATYA
Lowercase:this is satya

EXAMPLE -2

// Using concatenation to prevent long lines.
class ConCatDemo {
  public static void main(String args[]) {
    String longStr = "This is object oriented programming " +
      "through java " +
      "by v.satya" ;

    System.out.println(longStr);
  }
}
OUTPUT
C:\SATYA>javac ConCatDemo.java
C:\SATYA>java ComCatDemo
C:\satya>
This is object oriented program through java by V.Satya
EXAMPLE -3
// Demonstrate delete() and deleteCharAt()
class deleteDemo {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("This is oops through java.");

    sb.delete(4, 9);
    System.out.println("After delete: " + sb);

    sb.deleteCharAt(0);
    System.out.println("After deleteCharAt: " + sb);
  }
}
OUTPUT
C:\SATYA>javac deleteDemo.java
C:\SATYA>java deleteDemo
C:\satya>
After delete:thisops through java.
After deletechartAt:hisops through java.

EXAMPLE -4

// Demonstrate equals() and equalsIgnoreCase().
class equalsDemo {
  public static void main(String args[]) {
    String s1 = "Satya";
    String s2 = "Satya";
    String s3 = "Good-bye";
    String s4 = "SATYA";
    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 + " equalsIgnoreCase " + s4 + " -> " +
                       s1.equalsIgnoreCase(s4));
  }
}
OUTPUT
C:\SATYA>javac equalsDemo.java
C:\SATYA>java equalsDemo
C:\satya>
Satya equals Satya->true
Satya equals Good-bye->false
Satya equals SATYA->false
Satya equals ignorecase SATYA->true
EXAMPLE -5

// equals() vs ==
class EqualsNotEqualTo {
  public static void main(String args[]) {
    String s1 = "satya";
    String s2 = new String(s1);

    System.out.println(s1 + " equals " + s2 + " -> " + 
                       s1.equals(s2));
    System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
  }
}

OUTPUT
C:\SATYA>javac EqualIsNotEqualTo.java
C:\SATYA>java EqualsNotEqualTo
C:\satya>
Satya equals satya ->true
Satya==satya->false

EXAMPLE -6

class getCharsDemo {
  public static void main(String args[]) {
    String s = "this is satya.";
    int start = 10;
    int end = 14;
    char buf[] = new char[end - start];

    s.getChars(start, end, buf, 0);
    System.out.println(buf);
  }
}
OUTPUT
C:\SATYA>javac getCharsDemo.java
C:\SATYA>java getCharDemo
C:\satya>
jayb


EXAMPLE -7

// Demonstrate indexOf() and lastIndexOf().
class indexOfDemo {
  public static void main(String args[]) {
    String s = "this is object oriented programming through java " +
               "by v.satya";

    System.out.println(s);
    System.out.println("indexOf(t) = " +
                       s.indexOf('t'));
    System.out.println("lastIndexOf(t) = " +
                       s.lastIndexOf('t'));
    System.out.println("indexOf(the) = " +
                       s.indexOf("the"));
    System.out.println("lastIndexOf(the) = " +
                       s.lastIndexOf("the"));
    System.out.println("indexOf(t, 10) = " +
                       s.indexOf('t', 10));
    System.out.println("lastIndexOf(t, 60) = " +
                       s.lastIndexOf('t', 60));
    System.out.println("indexOf(the, 10) = " +
                       s.indexOf("the", 10));
    System.out.println("lastIndexOf(the, 60) = " +
                       s.lastIndexOf("the", 60));
  }
}
OUTPUT
C:\SATYA>javac indexOfDemo.java
C:\SATYA>java indexOfDemo
C:\satya>
This is object oriented program through java by V.Satya
Index<t>=0
Lastindex<t>==36
Indexof<the>=-1
Last index of<the>=-1
Indexof<t,10>=13
Last index of<t,60>=36
Indexof<the,10>=-1;
Last index of <the,60>=-1


EXAMPLE -8

// Demonstrate insert().
class insertDemo {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("I like!");

    sb.insert(2, "love ");
    System.out.println(sb);
  }
}
OUTPUT
C:\SATYA>javac insertDemo.java
C:\SATYA>java insertDemo
C:\satya>
I Love Like!
EXAMPLE -9

// Construct one String from another.
class MakeString {
  public static void main(String args[]) {
    char c[] = {'V', 'i', 'j', 'a',’y’};
    String s1 = new String(c);
    String s2 = new String(s1);

    System.out.println(s1);
    System.out.println(s2);
  }
}
OUTPUT
C:\SATYA>javac MakeString.java
C:\SATYA>java MakeString
C:\satya>
‘V’,’I’,’J’,’A’,’Y’
EXAMPLE -10

// Demonstrate replace()
class replaceDemo {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("This is satya.");

    sb.replace(5, 7, "was");
    System.out.println("After replace: " + sb);
  }
}
OUTPUT
C:\SATYA>javac replaceDemo.java
C:\SATYA>java replaceDemo
C:\satya>
After replace:this was satya
EXAMPLE -11

// Using reverse() to reverse a StringBuffer.
class ReverseDemo {
  public static void main(String args[]) {
    StringBuffer s = new StringBuffer("java");

    System.out.println(s);
    s.reverse();
    System.out.println(s);
  }
}
OUTPUT
C:\SATYA>javac ReverseDemo.java
C:\SATYA>java ReverseDemo
C:\satya>
Java
avaj
EXAMPLE -12

// Demonstrate charAt() and setCharAt().
class setCharAtDemo {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("satya");
    System.out.println("buffer before = " + sb);
    System.out.println("charAt(1) before = " + sb.charAt(1));
      sb.setCharAt(1, 'i');
      sb.setLength(2);
      System.out.println("buffer after = " + sb);
      System.out.println("charAt(1) after = " + sb.charAt(1));
  }
}
OUTPUT
C:\SATYA>javac setCharAtDemo.java
C:\SATYA>java setCharAtDemo
C:\satya>
Buffer before=satya
charAt<1>before=i
buffer after=vi
charAt<1>after=i

EXAMPLE -13

// A bubble sort for Strings.
class SortString {
  static String arr[] = {
    "satya", "isaac", "manasa", "joshna"
  };
  public static void main(String args[]) {
    for(int j = 0; j < arr.length; j++) {
      for(int i = j + 1; i < arr.length; i++) {
        if(arr[i].compareTo(arr[j]) < 0) {
          String t = arr[j];
          arr[j] = arr[i];
          arr[i] = t;
        }
      }
      System.out.println(arr[j]);
    }
  }
}
OUTPUT
C:\SATYA>javac setCharAtDemo.java
C:\SATYA>java setCharAtDemo
C:\satya>
Isaac
Joshna
Manasa
satya
EXAMPLE -14

// StringBuffer length vs. capacity.
class StringBufferDemo {
  public static void main(String args[]) {
    StringBuffer sb = new StringBuffer("satya");

    System.out.println("buffer = " + sb);
    System.out.println("length = " + sb.length());
    System.out.println("capacity = " + sb.capacity());
  }
}
OUTPUT
C:\SATYA>javac ReverseDemo.java
C:\SATYA>java ReverseDemo
C:\satya
Buffer=satya
Length==5
Capacity=21


EXAMPLE -16

// Construct string from subset of char array.
class SubStringConsDemo {
  public static void main(String args[]) {
    byte ascii[] = {65, 66, 67, 68, 69, 70 };

    String s1 = new String(ascii);
    System.out.println(s1);

    String s2 = new String(ascii, 2, 3);
    System.out.println(s2);
  }
OUTPUT
C:\SATYA>javac SubStringConsDemo.java
C:\SATYA>java SubStringConsDemo
ABCDEF
CDE
C:\satya>





No comments:

Post a Comment