VXL String function_081904
VXL String function_081904
- Strings in Java are created by declaring object of String type class and
initializing it with a string literal.(Eg:”Java”, “Hello” etc)
- String class instances or objects can hold unchanging string (immutable
String) ie., once initialized its contents cannot be modified.
- In Java, string is basically an object that represents sequence of char
values.
For example, "hello" is a string containing a sequence of characters 'H' , 'e' ,
'l' , 'l' , and 'o' .
Creating Strings:
String str;
Or
String str=”Hello”
Or
5) String trim() – removes space from both ends of the given string
Example : Hi Java
String str=” Hi Java “; 0 1 2 3 4 5 6 7 8 9 10 11 12 13
// There are 2 spaces in the beginning and in between 3 spaces and at the end 3
spaces.
//the below command is to check the length before applying trim() command, so
that changes can be verified by applying trim() command
int len=str.length();
System.out.println(len);//14 – check the string with index given in pic
System.out.println(“Hello”+str+”easy”); //Hello Hi Java easy
len=res.length();
System.out.println(len);//9 , previously length was 14
System.out.println(“Hello”+str+”easy”); //Hello Hi Java easy
Note : Original string remains unchanged
6) int indexOf(char ch) – returns the index of the first occurrence of specified
character from a given string.
Example :String str=”Letter”; L e t t e r
0 1 2 3 4 5
int pos=str.indexOf(„e‟);
System.out.println(pos);//1
7) int lastIindexOf(char ch) – returns the index of the last occurrence of
specified character from a given string.
Example :String str=”Letter”;
int pos=str.lastIndexOf(„e‟); L e t t e r
0 1 2 3 4 5
System.out.println(pos);//4
8) String concat(String str) – Concatenate the specified string to the end of the
given string
Example :
String str1=”Computer”;
String str2=”Applications”;
String str3=str1.concat(str2);
System.out.println(str1);//Computer
System.out.println(str2);//Applications
System.out.println(str3);//ComputerApplications
OR , using + to concatenate ,
Str3=str1+str2;
System.out.println(str);// Examination
10) String substring(int begindex,int endIndex) - returns a part of the
string(substring) from the beginning index up to the ending index specified from a
given string.
Note : Extracts up to ending index( Excluding ending index)
String str=”Examination”;
String res=str.substring(0,4); E xa m i n a t i o n
0 1 2 3 4 5 6 7 8 9 10
System.out.println(res);//Exam
System.out.println(str);// Examination Up to 4 but 4 not included
Eg 1: String s1=”Adit”;
String s2=”Abhi”
int res=s1.compareTo(s2);
System.out.println(res); // 2
Explanation:
First letters of both the strings are same , therefore it takes the 2nd letter & the
letters are different , then it gives difference of these letters.
S1 - d – Ascii value 100
S2 - b – Ascii value-------- 98
Difference 2 ( +ve result ie., when string1>string2 it gives >0)
Explanation:
First letters of both the strings are same , therefore it takes the 2nd letter & the
letters are different , then it gives difference of these letters.
S2 - b – Ascii value 98
S1 - d – Ascii value------- 100
Difference -2 ( -ve result ie., when string1<string2 then <0)
S1 - B – Ascii value 66
S2 - b – Ascii value-------- 98
Difference -32 ( -ve result ie., when string1<string2 then <0))
S1 - B – Ascii value 66
S2 - B – Ascii value ------- 66
Difference 0 ( Zero result ie., when string1=
String2 then 0))
17) String valueOf(all types) - returns the String representation of the type of data
passed to it as an argument .
Example :
1) to convert an integer to String
int i=100;
String s1=String.valueOf(i);// converts int to String
System.out.println(s1+9); //1009 – concatenates as 100 is String type
Output :
First Name : Subhash
Middle Name : Chandra
Last Name : Bose