L - 9 String Manipulation
L - 9 String Manipulation
String = “Computer”
Character = ‘A’
String s = C o m p u t e r
position/ index = 0 1 2 3 4 5 6 7
● String Constructors
● String Length
2. By concat() method
class TestStringConcatenation2{
public static void main(String args[]){
String s=50+30+"Sachin"+40+40;
System.out.println(s);//80Sachin4040
} }
2) String Concatenation by concat() method
class TestStringConcatenation3{
public static void main(String args[]){
String s1="Sachin ";
String s2="Tendulkar";
String s3=s1.concat(s2);
System.out.println(s3);//Sachin Tendulkar
}
}
● The ValueOf() method
—> Converts all data types to String.
int i=456;
float f=17.34f;
double d=45.23d;
String is=String.valueOf(i); is --------> “456”
String fs=String.valueOf(f); fs---------> “17.34”
String ds=String.valueOf(d); ds--------->”45.23”
● Character Data Type
→ used to represent a single character.
→ Character literal is always enclosed within a single quote.
Ex: char ‘A’;
Characters Unicode
A to Z 65 to 90
a to z 97 to 122
0 to 9 48 to 57
char x=’A’;
int y=x;
Sop(y); → 65
class Checkdemo
{
static void test(char x)
{
if(x>='A' && x<='Z')
System.out.println("uppercase character");
else if(x>='a' && x<='z')
System.out.println("lowercase character");
}
}
● Extraction of a character from a string
→ to extract a character from a string we can use the charAt()
method.
→ position or index.
index 0 1 2 3 4 567
String s1=”COMPUTER”;
char c;
c=s1.charAt(1); —> O
}
}
}
● Extraction of Substring
→part of string →substring—> substring() method
Character
String replace(char original, char replacement)
Ex: String s1=”BABA”,s2;
s2=s1.replace(‘B’,’T;);
—> s2=TATA
String
String replace( String original , String replacement)
Ex:String s=”he went to movie and he also went to market”;
String r;
r=s.replace(“he”,”she”);
→She went to movie and She also went to market
● Equalisation of Strings( equals() method)
→ compare characters in a string and check whether two strings are
equal or not.
class String
{
static void check()
{
String s1=”COMPUTER”, s2=”COMPUTER”;
if(s1.equals(s2))
System.out.println(“Equal”);
else
System.out.println(“ Not Equal”);
}
}
Searching Strings
System.out.println(“abc”.compareTo(“abde”));
c=99 d=100 = 99-100= -1
System.out.println(“AB”.compareTo(“ABCD”));
No index position so it will display -2
“AB”.length()-”ABCD”.length() = 2-4= -2
“abcd”.compareTo(“abCD”)
c=99 C=67 = 99-67=32
The trim() Function
Hello World!
Hello World!
import java.util;
public class StringDemo {
public static void main(String[] args) {
String str = " Tutorials Point ";
System.out.println("The length of the string before trimming is: "
+ str.length());
System.out.println("The string without trimming is: " + str);
String t = str.trim();
System.out.println("The length of the string after trimming is: " +
t.length());
System.out.println("The string with trimming is:" + t);
}
}
Output
The length of the string before trimming is: 25
The string without trimming is: Tutorials Point
The length of the string after trimming is: 15
The string with trimming is:Tutorials Point
Section A
Answer as directed
System.out.println(n.substring(0,8).concat(m.substring(9)));
Computer Applications
System.out.println(n.endsWith(‘e’)); = true