public class stringManipulation {
public static void main(String[] args) {
String aName="Arya";
String bName="Aryas";
//byteCount method to get the length of the string
int byteCount=aName.getBytes().length;
System.out.println("length of the string aName: " +byteCount);
int byteCount1=bName.getBytes().length;
System.out.println("length of the string bName: " +byteCount1);
//length method to get the length of the string
int len=aName.length();
System.out.println("The length of the string aName is " +len);
//character at the specified position
char c= aName.charAt(3);
System.out.println("Character at position 3 is " +c);
//Modifier methods:
//concatenation
String s= aName.concat(bName);
System.out.println("After concatenation String becomes " +s);
//convert to upperCase
String g=aName.toUpperCase();
System.out.println("uppercase will be" +g);
String r=s.replace(" ", "-");
System.out.println("The string r becomes" +r );
String r1=g.replace("A","B");
System.out.println("r1 becomes " +r1);
String r2=r.replaceAll("a","s");
System.out.println("r2 becomes"+r2);
//use of string buffer and append
String x=new StringBuffer().append("a").append(4).append("c").toString();
System.out.println("value of x is:" +x);
//difference between insert and append method
/*append meaning add something into buffer(just like writing sthng into buffer)
While insert method insert smthng into specified position as shown below before
inserting in a specified location we have to write into buffer if we give the
position beyond the length of the string (StringIndexOutOfBoundsException)*/
StringBuffer sb=new StringBuffer();
sb.capacity();
System.out.println(" capacity" +sb.capacity());
sb.append("MyNameIsAryarweasd");
System.out.println(" "+ sb.indexOf("r"));
/*sb.indexOf("x", 4);confusion confusion??????????????????????????
System.out.println("Returns the index within this string of the first occurrence of the specified substring, starting at the specified index" + sb.indexOf("x", 4));*/
sb.capacity();
System.out.println("sb capacity is" +sb.capacity());
System.out.println("sb: is " +sb);
sb.insert(6,"situ");
System.out.println("sb is" +sb);
if(aName.equals(bName)){
System.out.println("Srings are equal");
}
else{
System.out.println("Strings are not equal");
}
}
}
إرسال تعليق