Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0Activity
0 of .
Results for:
No results containing your search query
P. 1
Thread

Thread

Ratings: (0)|Views: 73|Likes:
Published by MITMCA

More info:

Published by: MITMCA on Oct 20, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

04/28/2012

pdf

text

original

 
Strings in JAVA
String handling is implemented in java using String Class objects. Strings are immutable inJAVA i.e., changing content is not possible in the original String. Either it should be copiedto a new String or its companion class StringBuffer should be used.String and StringBuffer classes
are declared in java.lang package. These are “final”. String
and StringBuffer class constructors and the built in methods are demonstrated using aprogram that is given below:class StringTest{ public static void main(String ar[]){ //String class Constructors beginString s = new String();char c[] = {'a','b','c','d','e'};String s1= new String(c);String s2=new String(c,0,3);String s3 = new String(s2);byte b[] = {66,67,68,69,70};String s4 = new String(b);String s5 = new String(b,0,4);//constructor endString s6 = "ABCDEFG";int l=s6.length(); //lengthint m="ABCDEFG".length();//lengthString s7=String.valueOf(l);//converting primitive to Stringchar d = s6.charAt(3);//extracting a char from a particular position 3int st=0, ed=5;char buf[] = new char[5];s6.getChars(st,ed,buf,0);//function to extract some chars from a string s6 and storing in aarrayfor(int i=0;i<5;i++) //displaying the charsSystem.out.print(buf[i]);byte e[];e=s6.getBytes();//stores it into an array of bytesString s8 = new String(e); // dispchar f[]=s6.toCharArray();//converts all chars into a char arraySystem.out.println("\n");for(int i=0;i<7;i++)System.out.print(f[i]+" ");//disp charsSystem.out.println("\n s = "+s);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);System.out.println("s6 = "+s6);System.out.println("s7 = "+s7);System.out.println("s8 = "+s8);System.out.println("\n e = "+e);System.out.println("\n d = "+d);System.out.println("l = "+l + " m "+m);String s9="ABc"; //equals and equalsIgnoreCase and ==System.out.println(s1+ "equals "+s2+" "+s1.equals(s2));System.out.println(s2+ "equals "+s3+" "+s2.equals(s3));System.out.println(s2+ "equalsIgnoreCase "+s9+" "+s2.equalsIgnoreCase(s9));System.out.println(s2+ "=="+s3+" "+(s2==s3)); //stratsWith(), endsWith()System.out.println(s6.endsWith("FG"));System.out.println(s6.startsWith("AB")); //regionMatchesSystem.out.println(s6.regionMatches(1,s6,1,2)); //compareTo,compareToIgnoreCaseSystem.out.println(s6.compareTo(s5));System.out.println(s4.compareToIgnoreCase(s3)); // searching stringsint a = s6.indexOf('C');int b1 = s6.indexOf("AB"); int ff = s6.indexOf(66);int a1 = s6.lastIndexOf('G',0);int b2 = s6.lastIndexOf("FG",0);System.out.println("s6 =" +s6+" a = "+a+" b1= "+b1+" a1 ="+a1+" b2 = "+b2+" ff = "+ff); //substringString s10 = s6.substring(2); String s11 = s6.substring(2,6);System.out.println("s10 = "+s10+" s11 = "+s11); //concatenateString s12 = s10.concat(s11);System.out.println("s12 = s10+s11 "+s12); //replaceString s13 = s12.replace('C','I');System.out.println("s12 = "+s12+" s13 = "+s13); //TrimString s14 = " Hello "; String s15 = s14.trim();System.out.println(s6+s15); //toLowerCase and toUpperCaseString s16= s14.toUpperCase(); String s17=s6.toLowerCase(); System.out.println(s17+s16); //StringBuffer class
 
StringBuffer sb = new StringBuffer(s6);//ConstructorSystem.out.println(sb.length());//lengthSystem.out.println(sb.capacity());//capacitysb.setLength(4);System.out.println(sb);//setlengthchar m1 = sb.charAt(1);sb.setCharAt(3,'E');//charAt and setCharAtSystem.out.println(m1);System.out.println(sb);sb.append("PQR").insert(7,"66").append(55).delete(7,8).deleteCharAt(8).replace(6,8,"MNP").reverse();//append, insert, delete, deleteCharAt, replace, reversesb.substring(2,7);//substringSystem.out.println(sb);}}Questions:1.
 
Write a program to replace an entered word with “Hello”
 2.
 
Demonstrate equals() and == with a program.3.
 
Count and all the repeated appearance of an entered word. Replace each one with anew word interactively.4.
 
Use a String, reverse it and concatenate both and store them in a new String. Find thelength, reverse each character of the resultant string.5.
 
Use a sentence, reverse each word, characterwise and wordwise.6.
 
Convert the starting word’s char of a sentence to
its uppercase.7.
 
Take a String, replace the first char from the left with the last char from right, till itleft coincides with right.8.
 
Take a string find the repeating chars in it replace them with some other char that isentered through the keyboard.

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->