String Accessor Method in JAVA
String functions in Java with Illustrative Examples
length ( )
Syntax
 int x = S. length ( ) , where S is the string object
Definition
 Returns length of the string or number of characters in the string
Example 
 System out println ("Apple".length ());
 output - 5
 System out println("LENGTH OF NAME IS " + "ABHISHEK".length());
 Output -  LENGTH OF NAME IS 8
charAt ( )
Syntax
 Char x = S.charAt(n), where n is an integer
Definition
 This method returns the character at the given location n of a string.
 String t = "Computer";
 System out println(t.charAt(5));
 Output -t
Indexof ( )
Syntax
 int a = s.indexOf(p), where p is a character
Definition 
 This method returns the first occurence (location) of a character in a string.
Example
 System.out.println("Computer".IndexOf('u'));
 output - 4
 If the character is not found then output = -1
lastIndex Of ( )
Syntax
 int a = s. lastIndexOf(n);
Definition
 This method retuns the last  occurence (location) of a character in a string
Example
 System out. println (" MONTESSORI".last Index Of ('o'));
 Output - 7
replace  ( )
Syntex
 S = s replace ( p, q)
Definition
 This method replaces all the occurences of a given old character p with the new character q.
Example
 s= "BALLOON". replace ( 'L',  'B');
 Output - BALLOON
subString ( )
Syntax
 String t = s substring (m, n), where m and n are the start and end locations
Definition
 This method  extracts a substring t form a given substring s starting from the location m upto n -1. If n is not given them the entire string starting from m is exteracted.
Example
 System.out.println("OCCASSION".subString(2,6));
 Output - "CASS"
       System.out.println( "OCCASSION".subString(3, 7));
        Output - "ASSI"
concat (  )
Syntax
 s = s.concat(t) or s = s + t , where s and t are two strings
Definition
 This method adds one string to the end of other
Example
 s1 =  "Computer";
 S2= "Programming";
 S1= s1.concat (s2);
        System.out.println( s2);
 output - ComputerProgramming
toUpperCase (  )
Syntax
 s =s.toUpperCase(  );
Definition
 This method converts all the lower case alphabets  in a string  to upper case ignoring the rest.
Example
       System.out.println("15 August".toUpperCase(  ));
Output - "15 AUGUST"
toLowerCase (  )
Syntax
 s =s.toLowerCase(  )
Definition
 This converts the alphabets of uppercase in string to lowercase ignoing the others.
Example
       System.out.println(" 15 August".toLowerrCase (  ));
       Output - "15 august"
startsWith (  )
Syntax
 boolean a = S.stratsWith (t), where t denotes a character
Definition
 This method checks whether a given string s starts with a given prefix t or not, if starts with t then a = true else false
Example
System.out.println("ATUL".stratsWith('A'));
Output - True
endsWith (  )
Syntax
 boolean  a = S.endsWith (t), where t denotes a character
Definition
 This method checks whether a given string s ends with a given suffix  t or not. If starts with t then a = true, false otherwise.
Example
Systen.out.println("ATUL".endsWith('p'));
Output - False
equals(  )
Syntax
 boolean B = s.equals(t), where s and t are two strings
 If s = = t, B is true otherwise B is false
Definition
 This method checks whether the two strings are same or not
Example
String s1 = "ATUL"
String s2 = "Atul"
System.out.println(s1.equals(s2));
Output : false
equalsIgnoreCase ( )
Syntax
boolean b = s.equalsIgnoreCase (t);
Definition
It cheks whether the two strings are same or not irrespective of the case. If s==t then b=true and if s!=t then b=false.
Example
System.out.println ("RACHIT".equalsIgnoreCase ("Rachit"));
Output - true
CompareTo ( )
Syntax
int a = s.compareTo(t)
if (a > 0) then s > t
if (a < 0)  S< t
if (a==0)  s = t
'a' is the difference of 1st uncommon pair of characters. If both the strings are same then a=0.
Definition
This method compares the two strings on the basis of the ASCII values of their characters.
Example
System.out.println ("MANGO".compareTo("MANGOES"));
Output  32 –101 = – 69
System.out.println ("ATUL".compareTo ("AMUL"));
Output  84 – 77 = 7
trim ( )
Syntax
s = s.trim (), where s is a string.
Definition
This method removes the white spaces from both the ends of the strings.
Example
System.out.println("     SUNNY    DAY    ".trim ());
Output  "SUNNY    DAY" (Note that spaces from mid will not be omited)
Free Web Counter

 
 

0 comments:
Post a Comment