String is nothing but a sequence of characters, for e.g. “Hello” is a string of 5 characters. In java, string is an immutable object which means it is constant and can cannot be changed once it has been created.


Creating Strings:

The most direct way to create a string is to write:
String greeting ="Hello world!";

  • The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:
class String
{
public static void main(String args[])
{
Try
{
String  name 1 , name 2
System. out. Println (“Enter any  name”);
name 1=in.readLIne();
name 2=name 1.To lowercase();
System.out.Println(“name=” +  name1);
System.out.Println(“name=” +  name2);
System.out.Println(“name=” +  name1.to uppercase());
System.out.Println(“name=” +  name1.replace( n ,z);
System.out.Println(“name=” +  name1.trim());
}
Catch(Exception e)
{
System.out.Println(e);
}
}

String Length:

  • Methods used to obtain information about an object are known as accessor methods.
  • One accessor method that You can use with strings is the length() method, which returns the number of characters contained in the string object.
class String
{
public static void main(String args[])
{
Try
{
Int a,b;
System.out.Println(“Enter a ,b values”);
a=Integer.parseInt(in.readLIne());
System.out.Println(“A=“ + a );
System.out.Println(“B=“ + b );
System.out.Println(“A/B=“ + (a/b) );
}
Catch(IOException e)
{
System.out.Println(IOExeption Raised);
Catch( number  format  Exception  e1)
{
System.out.Println(e1);
}
Catch(Arithmetic Exception e2)
{
System.out.Println(e2);
}
}
}

Strings:

  • You have printf() and format() methods to print output with formatted numbers. The String class has an equivalent class method, format(), that returns a String object rather than a PrintStream object.
  • Using String's static format() method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of:
    • S.NO Methode Discription
      1String 2 = String 1 . to lower case () ;Converts the String 1 to all lower case
      2 String 2 = String 1 . to upper case () ; Converts the String 1 to all upper case
      3String 2 = String 1 . replace ( X ,Y ) ; Replace all appearance of X with Y
      4 String 2 = String 1 . trim () ; Remove white spaces at the begging and end of the string 1
      5String 1 . equals (String 2)Return’s “true” if string 1= string 2
      6String 1 . equals ignore case (String 2)Return’s if string 1= string 2,ignoring the case of char
      7String 1 . length ();Gives length of String1
      8String 1 . char at (n)Gives nth character of string1
      9String 1 . compare to (String 2Return’s negative if string1
      10String 1 . contact (String )Concretes string 1 and string 2
      11String 1 . sub string (n , m)Gives substring from nth character to mth
      12String . index of (X) Give the position of X that occurs after the position in the String1
      13String 1 . index of (X , n)Give the position of X that occurs after the position in the String1
      14String . value of (X)Converts the parameter value of string representation

      News Walls


Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.