0% found this document useful (0 votes)
9 views13 pages

Strings Part5

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 13

String class

Lesson 5
• Lesson Objectives
• To understand the usage of the following String methods

• concat()
• trim()
• replace()
• valueOf()
• lastIndexOf()
Concatenation
concat() – Joins or concatenates two Strings.
string1.concat(string2)
string2 is added to the end of string1

String str1 = "world";


String str2 = "wide“;
String str3 = "web";
String str4 = str1.concat(str2.concat(str3));

System.out.println(str4); //Displays, “worldwideweb”

System.out.println("to".concat("get".concat("her")));
Displays, "together“
Difference between concat() and + operator
 When numeric data is concatenated with a String, the
+ operator automatically converts the number to a
String, whereas the concat() method does not convert
a number to a String type and the compiler gives an
error.
trim()
 This method removes leading and trailing spaces in a
String.
 Example:
 String s=“ good “;
 String s1=“day”;
 System.out.println(s+s1);
 Output:
 good day
 System.out.println(s.trim()+s1);
 Output:
 goodday
replace()
Replaces all occurrences of a character or a String with
another character or String respectively and it returns a new
String. It is case-sensitive.
replace(oldChar, newChar)
The other form:
replace(oldstring, newstring)
String s="mesquite in your cellar“;
String s1=s.replace('e', 'o')
String s1 now contains "mosquito in your collar"
String S2=“teh sun rises in teh east”
System.out.println(S2.replace(“teh”, “the”));
Displays, “the sun rises in the east”, but String S2 remains
unaffected.
More examples for replace()
class stringprog {
public static void main() {
String st="World matters";
System.out.println("before replacing"+st);
st=st.replace("t","$$");
System.out.println("after replacing"+st);
String st1="I am Studying in GMA. GMA is in Dubai";

st1=st1.replace("GMA", "Gems Modern Academy");


}
}
Counting the number of words in a String
String st1="I am Studying in GMA. GMA is in Dubai";

String st2=st1.replace(" ","");// replacing space with null

int cnt=st1.length() - st2.length() + 1;

System.out.println("No.of words :"+cnt);


String Operations
 valueOf() – Returns the string representation of the
given argument.
 char ch[]={‘a’, ’b’, ‘c’};
String str1 = String.valueOf(ch)
 In the above example, the contents of the character
array are converted to a String and stored in “str”.

Other forms are:


String str2 = String.valueOf(true);
String str3 = String.valueOf(3456);
String str4 = String.valueOf(653.67);
String str5 = String.valueOf(‘A’);
long m = 457786545;
String str6 = String.valueOf(m);
float f = 10.05f;
double d = 10.0;
int x=10;
boolean z=true;
String s1 = String.valueOf(f);
String s2 = String.valueOf(d);
String s3 = String.valueOf(x);
Output:
String s4 = String.valueOf(z);
System.out.println(s1.length());
5
System.out.println(s2.length());
4
System.out.println(s3.length());
2
System.out.println(s4.length());
4
lastIndexOf()
lastIndexOf(char ch):
lastIndexOf(String str):
Searches for the first occurrence of the specified character or String
starting from the end of the calling String object and returns the
position if the character or String is found or -1 if the character or String
does not occur.

lastIndexOf(char ch, int position):


lastIndexOf(String str, int position):

Searches for the first occurrence of the specified character or String


starting from the specified position (from the end) of the calling String
object and returns the position if the character or String is found or -1 if
the character or String does not occur.
lastIndexOf examples
 String
 st="She sells sea Shells on the sea shore. She also sells bangles";
 0123
System.out.println(st.lastIndexOf("She")); //displays 39
System.out.println(st.lastIndexOf("She", 3)); //displays 0
System.out.println(st.lastIndexOf("She ",20));//displays 0

String s=“12/06/2009”
System.out.println(s.lastIndexOf(‘/’ )); //displays 5

System.out.println(s.indexOf(‘/’, 3 )); //displays 2


Sample code with output
public class Test
{
public static void main()
{
String Str = new String("good day");
System.out.println(Str);
System.out.print("first occurrence of o :" );
System.out.println(Str.indexOf( 'o' ));
System.out.print("last occurrence of o :" );
System.out.println(Str.lastIndexOf( 'o' ));
System.out.print("next occurrence of o starting from index 1:" );
System.out.println(Str.indexOf( 'o',1 ));
}
}

You might also like