Strings

Strings

Previous Home Next

 

Strings is a set of characters.handling character string in java supported by two final classes String and String buffer the  String class implement immutable  character string   which are read only once created and initialized
whereas String buffer class implements the dynamic string.String literals in  java are implemented as instance of strings class .

The java program provide string class to  create and manipulate strings.
A java.lang.string class is final which implies no class and extend it.
 Important string methods are:

1.public String Concat(Strings)
  String s= "www.r4r"
  System.out.println(s.concat(".co.in"));
 
This method returns a string with the value of string pass into the method appended to the  end of string which is used to invoke the method.
output is : www.r4r.co.in

2.public in length()
This method returns the length of string used to invoke the method

String s = "hello";
System.out.println (s.length())

the output is: 5  //the length of string hello is 5

3.public String  toLowerCase()
 String s ="ABCD"
System.out.println(s.toLowerCase())
This method convert the string to the small alphabets.
output is :abcd
similarly other method toUpperCase is working. it convert the string from small alphabets to the capital alphabets.

4.public String Trim()
String s = "www.r4r.co.on"
System .out.println(s.trim());
It removes white spaces from both ends of the string.
The output is :wwwr4r.co.in

5.public Stringreplace( char 'old char', 'char newchar')
String s = " wurld"
System.out.println( s.replace ( 'u','o')

This  converts the old character to new one.
the output is: wurld


class Replace()
{

public
Static void main(String args[])
 
{
 String str1 = "hello world";
  System.out.println (str.Replace 'o','u');
}
}


Above is the example of method public Stringreplace(char 'char old ', 'char new')
The output of the Example is hellu wurld

Example of public StringConcat()

 class public Concat
{

public
Static void main(String args[])

{
  String str1 = "hello";
String
str2 = "world";
  String str3 = str1 + " " + str2;
  System.out.println(str3);
 }
}

The output of above example is hello world







 

Previous Home Next