[go: up one dir, main page]

0% found this document useful (0 votes)
14 views9 pages

4 - Strings

Strings in Java are objects backed by character arrays. The String class contains methods for examining, modifying, and comparing string values. Common string methods include length(), charAt(), indexOf(), substring(), toLowerCase(), trim(), equals(), and valueOf(). Strings can be concatenated using the + operator or concat() method and compared using equals(), compareTo(), and equalsIgnoreCase().

Uploaded by

annwesa.panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

4 - Strings

Strings in Java are objects backed by character arrays. The String class contains methods for examining, modifying, and comparing string values. Common string methods include length(), charAt(), indexOf(), substring(), toLowerCase(), trim(), equals(), and valueOf(). Strings can be concatenated using the + operator or concat() method and compared using equals(), compareTo(), and equalsIgnoreCase().

Uploaded by

annwesa.panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

String handling in Java

▪ Strings in Java are Objects that are backed


internally by a char array. Set of alpha numeric
characters within double quotes is String.
▪ JCL contains String class in java.lang package
▪ Syntax:
▪ <String_Type> <string_variable> =
"<sequence_of_string>";
▪ i.String str=“Strings”;
▪ ii. String st=“Computer”;
▪ String str1=new String(st);
String is a Character array

▪ char ch[ ]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};


▪ String str=new String(ch);
▪ char ch[ ]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’};
▪ String str=new String(ch,2,3);
▪ str=“MPU”
▪ Using Scanner class:
▪ next(),nextLine();
▪ InputStreamReader(),BufferedReader()(not
there in syllabus)
▪ readLine();
String functions:
▪ int length() is a member method of String It gives
the number of characters present in a string.
▪ String str=“Computer”;
▪ char ch=str.charAt(3);//ch=p
▪ sc.next().charAt(0)//first character
▪ If index exceeds length, String out of bound
exception
▪ String str=“Computer Science”;
▪ char ch=str.charAt(8); ch=‘ ‘
String Functions

▪ int p=str.indexOf(‘e’);//first occurrence of e


▪ int p=str.indexOf(‘e’,7); // occurrence of e from the 7
th index
▪ int p=str.lastIndexOf(‘e’); // last occurrence of e
▪ String substring(int startindex)// part of the string
▪ String str=“Computer Science”;
▪ String p=str.substring(5);//characters from 5 th index
to end of the String will get printed
▪ String p=str.substring(6,11); characters from 6 th
index to 10 th index of the String will get printed,
character at 11 th index will be excluded.
String functions:
▪ String toLowerCase();
▪ String toUpperCase();
▪ String replace(char old,char new);
▪ Ex:
▪ String str=“MxLxYxLxM”;
▪ String newstr=str.replace(‘x’,’A’);
▪ //newstr=MALAYALAM
▪ String st1=“The green bottle is in green bag”;
▪ String str2=st1.replace(“green”,”red”);
concat() and equals()

▪ String concat()
▪ = assignment, == ,equality,relational
▪ String x=“Computer”;
▪ String y=“Applications”;
▪ String z=x.concat(y);//ComputerApplications
▪ Z=x+y // ComputerApplications
▪ boolean equals(string s)
▪ Ex:
▪ if(x.equals(y)) //false(case sensitive)
equals() vs compareTo()

▪ String x=“Science”;
▪ String y=“SCIENCE”;
▪ boolean equalsIgnoreCase(String s)
▪ boolean c=x.equalsIgnoreCase(y);
//true(ignores the case)
▪ int compareTo(String s)
▪ Returns 0 if both strings are equal , a negative
value st1<st2, a positive number, if st1>st2
▪ int compareToIgnoreCase(String str)
trim(),startsWith(),endsWith()

▪ String trim():Returns the copy of the String, by


removing whitespaces at both ends. It does not
affect whitespaces in the middle. String word1 = “
Learn Share Learn “;
▪ String word2 = word1.trim(); // returns “Learn Share
Learn”
▪ endsWith(),startsWith() return boolean value
▪ String p=“computer is fun”;
▪ String b=“fun”;
▪ boolean x=b.endsWith(‘N’);//false
▪ String s=“1245”;
▪ Double d=Double.valueOf(s);//d=1245.0
valueOf()

▪ int n;
▪ String s=“245”;
▪ n=Integer.parseInt(s); //n=245(int form)
▪ n=Integer.valueOf(s); //n=245(int form)
▪ ---------------------------------
▪ String valueOf(primitive type)
▪ int n1=134;
▪ float n2=12.386;
▪ String s1=String.valueOf(n1);// 134 in String form
▪ String s2=String.valueOf(n2);// 12.386 in String form

You might also like