Strings and StringBuffers

A string is a sequence of character. A string will have fixed number of character. In java a string is object of the class which belongs to the java lang.string.

    String constructors:
The string class supports several constructors to create an empty string we can call the default constructors.

  1. String object=new String();
  2. String object=new String(char, char[])
  3. String object = new String(char array, int start, int numchars)
  4. String object=new String(string object)
  5. String object=new String(byte ASCII char array)
  6. String object=new String(byte ASCII char array, int start, int num chars)

 

WRITE A PROGRAM USING STRING CONSTRUCTORS.

class Sample
{
public static void main(String args[])
{
String S=new String();
S="Hello";
System.out.println(S);

                char A[]={'v','i','s','i','o','n'};
String K=new String(A);
System.out.println(K);

                String P=new String(A,2,3);
System.out.println(P);

                String S1="Welcome";
String S2=new String(S1);
System.out.println(S1+S2);

                byte ascii[]={65,66,67,68,69};
String S3=new String(ascii);
System.out.println(S3);

String S4=new String(ascii,2,3);
System.out.println(S4);
}
}

length()
                       
It is used to find the string length.

Syntax: object.length()

charAt(): Returns the chat at the specified index of the string.

Syntax: charAt(index);

indexOf(): Returns the index of the first occurrence of the specified character or -1 if the character not found.

Syntax:           indexOf(char);
indexOf(char,fromposition);

lastIndexOf:   Returns the index of the last occurrence of the specified character.

Syntax:          
lastIndexOf(char, fromposition);
lastIndexOf(char);
lastIndexOf(String);
lastIndexOf(string,fromposition);

subString:      To print the string from specified position to specified characters

Syntax:-     subString(begin Index, end index);
 
replace: To replace the old character with new char.

Syntax :          replace(old character, new character)

startsWith:     Returns true if the character sequence represented by the argument is a prefix of the character sequence represented by the string.

Syntax:-          startsWith(String prefix);
endsWith(string);

toLowerCase:                        To convert the string into lower case

syntax:-           object.toLowerCase()

toUpperCase:                        To convert the string into uppercase

Syntax:                       object.toUpperCase()

trim:                removes white spaces from both ends.

Syntax:           object.trim()

equals:            it returns true if both the strings are equal.

Syntax:                1.object.equals(string)
2.object.equalsIgnoreCase(string)

comparateTo:                        To know whether two string are identical for sorting applications you need to know which is <(or)=(or)>

Syntax            :           object.compareTo(“string”);

getChars         : it is used to get the characters from the string and stores in to an array.

Syntax:-          object.getchars(sourcestart,sourceend,chararray,targetstart)

contact:           we can concatenate the two strings.
Syntax:           object.contact(string)

toString:         It is used to convert the other data types to string data type.

Syntax            :           String obj=object.toString();

WRITE A PROGRAM ON STRING FUNCTIONS.

class Sample
{
public static void main(String args[])
{
String s="VISION";
System.out.println("Length: "+s.length());
System.out.println("Char AT: "+s.charAt(3));
System.out.println("INdex Of: "+s.indexOf(3));
System.out.println("Welcome To"+s.lastIndexOf("To"));
System.out.println("Sub string: "+s.substring(2,3));
System.out.println("Replace: "+s.replace('s','k'));
System.out.println("Starts With: "+s.startsWith("Vis"));
System.out.println("Ends With: "+s.endsWith("on"));
System.out.println("Tolowercase: "+s.toLowerCase());
System.out.println("Touppercase: "+s.toUpperCase());
System.out.println("Trim: "+s.trim());
System.out.println("Equals: "+s.equals("vision"));
System.out.println("Equalsignorecase: "+s.equalsIgnoreCase("vision”));
System.out.println("Compare: "+s.compareTo("vision"));
char c[]=new char[10];
String s1="welcome to vision";
s1.getChars(3,5,c,0);
System.out.println(s);
s1.concat(s);
System.out.println(s1);
}
}

String Buffers


A string buffer is a peer class of string that provides much of the functionality of strings. String represents fixed length, immutable character sequences. String buffer represents grow able and writable character sequences. String buffer will automatically grow to make room for string additions. string buffer has the following three constructors.
1) StringBuffer();
2) StringBuffer(int Size);
3) StringBuffer(String);

capacity():      It will display the total allocated capacity of the string buffer.

Syntax:           StringBuffer object.capacity();

ensureCapacity:        It is used to preallocate room for a certain no.of characters after string buffer has been constructed.

Syntax:           object.ensureCapacity(capacity)

setLength( ):   it is used to set the length of the buffer.

Syntax:           object.setLength(n)

charAt():        it is used to get the char at the specified location
Syntax:           object.charAt(n)

setCharAt( ):  it is used to set the character at specified location.       

Syntax:           object.setCharAt(n,char)

append( ):       The append( )method concatenates the string representation of any other type of data to end of the invoking string buffer object.
Syntax:           object.append(string)
object.append(int num)
object.append(object)
insert( ):          it is used to insert one string into another.
Syntax:           1.object.insert(int num,string)
2.object.insert(int num,char)
3.object.insert(int num,object)

reverse( ):       It is used to reverse the string.
Syntax:           object.reverse( )

delete( ):          to remove  characters from the specified location.
Syntax:           object.delete(start,end)

deleteCharAt( ):        to delete a character at specified location.
Syntax:           object.deleteCharAt(n)

WRITE A PROGRAM USING STRING BUFFERS.

class Sample
{
public static void main(String args[])
{
StringBuffer s=new StringBuffer("vision");
System.out.println("Length: "+s.length());
System.out.println("Capacity: "+s.capacity());
s.ensureCapacity(10);
s.setLength(5);
System.out.println("length: "+s);
s.setCharAt(3,'i');
System.out.println("Set char at: "+s);
s.append("hello").append(20).append("vision");
System.out.println("Append: "+s);
s.insert(3,"Insert");
System.out.println("Insert: "+s);
s.insert(4,"k");
System.out.println("insert: "+s);
s.reverse();
System.out.println("reverse: "+s);
s.delete(5,5);
System.out.println("Delete: "+s);
s.deleteCharAt(3);
System.out.println("delete char at: "+s);
}
}

 

For
More Explanation
&
Online Classes

Contact Us:
+919885348743