Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
21 views
String Functions
understanding string in java
Uploaded by
Movie Storage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save String Functions For Later
Download
Save
Save String Functions For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
21 views
String Functions
understanding string in java
Uploaded by
Movie Storage
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save String Functions For Later
Carousel Previous
Carousel Next
Save
Save String Functions For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 22
Search
Fullscreen
STRING FUNCTIONS [lettPe a of Sere tern ahead © me « Pe ee eg Jength() returns the number of characters contained in the string object. int variable=String variable.length(); return type : int Example public class StringDemo { public static void main(String args{]) {String str="Board Examinations” ; int len = strlength(); System.out.printin( "String Length is :" + len ); yy This will produce the following result ~ Output String Length is : 18 Se Meng eetbehee) method cet ms he ae me ter The ouhee woke Tet ee pany thes 4 beong?® of strong 1) char charAt(int index) method returns the character at the specified index in a string. The index value that we pass in this method should be between 0 and (length of string-1). For example: String str = "Java"; System.out.printin(str.charAt(2)); Output :v s.charAt(0) would return the first character of the string represented by instance s. Java String charAt method throws IndexOutOfBoundsException, if the index value passed in the charAt() method is less than zero or greater than or equal to the length of the string Java - —_——, Ped the (Rar Rel) method ees har The engtt of he string lowe> Ne SF meengeen — at ae oe ge public class CharAtExample x public static void main(String args{]) { String str = "Welcome to string manipulations"; /[This will return the first char of the string char chi = str.charAt(0); /[This will return the 6th char of the string char ch2 = str.charAt(5); //This will return the 21st char of the string char ch4 = str.charAt(20); System.out.printin( "Character at 0 index is: "+ch1); System.out.printin("Character at Sth index is: "+ch2); System.out.printin("Character at 20th index is: "+ch4); ) om Sete 6 EP meee eeof Vermgy } es pe Bad mg at he ond | Concatenation of Strings String concat () This method combines specified string at the end of this string. It returns combined string. It is like appending another string. syntax String variable = String1.concat(String 2) This returns a new string that is string1 with string2 added to it at the end. "My name is ".concat("Zara"); sone Tee")teem eee vod ee me wee public class ConcatExample { public static void main(String args[]) { String s1="java string"; s1.concat("is immutable’ System.out.printin(s1); $1=s1.concat(" is immutable so assign it explicitly"); System.out.printin(s1); y output java string java string is immutable so assign it explicitly Tt ete we ogee cage Thy7 OR od Sh Re ge, rev Strings are more commonly concatenated with the + operator "Hello," +" world" + "I" which results in - “Hello, world!" public class StringDemo public static void main(String args[]) { String string] = "class X"; System.out.printin("Hello " + string1 + "students"); * } anne a aie6 Ct Oe tes of 6 age Mad Mar m te indexOf() This method is used to find the index of a specified character or a substring ina given String. int indexOf(char ch): It returns the index of the first occurrence of character ch in a given String. int indexOf(char ch, int fromindex): It returns the index of first occurrence of character ch in the given string starts from the specified index “fromindex”. int indexOf{String str): Returns the index of string str in a particular String. int indexOf(String str, int fromindex): Returns the index of string str in the given string starts from the specified index “fromIndex”. ( ediibeeateetinemedinentnentitie Metin onl tt ere Meet me mee ot oF Dim Ne ee: et tee eteint posOfS = str.indexOf(ch2); int posOfSubstr = str.indexOf(subStr); int posOft=str.indexOf(‘t’,7); int posOfing=str.indexOf(“ing”,10); int posOfp=str.indexOf(‘p’); Note : It returns -1 if the specified char/substring is not found in the Particular String. De Lf the age Mare (Rar vedbetring 6 ]’ em ee me ge Rat Rem ew 6 be ee ee em te od fm ee lastindexOf() To find out the last occurrence of the specified character or a string, this method starts the search from the end of string and proceeds backwards from there. int lastindexOf(char ch): It returns the last occurrence of character ch in the given String. int lastindexOf(char ch, int fromindex): It returns the last occurrence of ch, it starts looking backwards from the specified index “fromindex”. int lastindexOf{String str): Returns the last occurrence of substring str in a String. int lastindexOf(String str, int fromIndex): Returns the last occurrence of str, starts searching backward from the specified index “fromindex”. Reg St Se Rete ns Me ee me ee Ret ee ee Bs mute 8 tyi¢ Str © “Deginmersboot « for beg che String str = char ch = char ch2 ='s'; String subStr = "beginners"; int posOfb = str.lastIndexOf(ch); int posOfs = str.lastindexOf(ch2); int posOfSubstr = str.lastindexOf(subStr); System.out.printIn(posOfb); System.out.printIn(posOfs); System.out.printIn(posOfSubstr); Pm Ont printimn( posOh) Pm Ot Dr nti DOOM ast!) “beginnersbook is for beginners";rn 2 re string That a 8 wating substring() This function returns a new string that is a substring of given string. String substring(int beginindex) It Returns the substring starting from the specified index i.e beginindex and extends to the character present at the end of the string. For example — "Chaitanya".substring(2) would return “aitanya". _ This method throws IndexOutOfBoundsException If the beginindex is less than zero or greater than the length of String Oe) Ne OT OPE a epmor 11 Grete Man The eng of rongrt Degeneres ot ereieuies) String substring(int beginindex, int endindex) It returns the substring which begins at the specified beginindex and extends to the character at index endindex — 1. Thus the length of the substring is endindex-beginIndex. For example — "Chaitanya" substring(2,5) would return “ait.he hereto, of song ote ome (one pit one Ca toLowerCase() This method converts the characters of a String into lower case characters. It returns String type of value String st=st:toLowerCase(); toUpperCase() This method converts the characters of a String into upper case characters. It returns String type of value String st=st.toUpperCase(); trim() It returns a String after removing leading and trailing white spaces from the input String. Fore.g. " Hello".trim() would return the String "Hello". ~— eeetd of ring a8) 8 oeed tor Ne startsWith() The startsWith() method of String class is used for checking prefix of a String. It returns a boolean value true or false based on whether the given string begins with the specified letter or word. boolean variable= String st.startsWith(String str) String s = "This is just a sample string"; //checking whether the given string starts with "This" System.out.println(s.startsWith("This")); //checking whether the given string starts with "Hi" System.out.printin(s.startsWith("Hi")); peher the geen etomg etarty ath he” Or Bertrand heh)| the occurrences of 2 ott characte replace() It replaces all the occurrences of a old character with new character. String replace(old character , new character) It returns String type of value. String st=str.replace(char ch,char ch1); String st=str.replace(String str1,String str2) For e.g. “welcome ".replace('e’, '*') would return w*Icom* “red balls in red bag”.replace(“red”,"green”); wees eVSTwr SS hed bag” replace! ved” green”)ber mg Me merment Ne 8) ome > font Me endsWith() Java String endsWith(String suffix) method checks whether the String ends with a specified suffix. This method returns a boolean value true or false. If the specified suffix is found at the end of the string then it returns true else it returns false. The endsWith() Method Signature: public boolean endsWith(String suffix) String str1 = new String("This is a test String"); String str2 = new String("Test ABC"); boolean vari = str1.endsWith("String’ boolean var2 = str1.endsWith("ABC"); “wo weg ew am) Ws stl om ae Wore ) vi+ @rl eo aac”)(Strang strong) boolean equals(String string): Compares the string with the specified string and returns true if both matches else false. String str1= new String("Hell String str2= new String(" String str3= new String("Hello"); System.out.printIn("str1 equals to str2:"+str1.equals(str2)); System.out.printin("'str1 equals to str3:"+str1.equals(str3)); wen’ veri equak to ar? “eatrie ‘Wtiet wri equals to tr} “+stri«gmere’ eset Vorwng strom) n eqush meted tt * Ques | (oreate boolean equalsignoreCase(String string): It works same as equals method but it doesn’t consider the case while comparing strings. It does a case insensitive comparison. String str1= new String("Apple"); String str2= new String("MANGO") String str3= new String("APPLE"); System.out.printin("str1 equals to str2:"+str1.equalsignoreCase(str2)); System.out.printin("str1 equals to str3:"+str1 .equalsignoreCase(str3)); boolean equals(String str): Case sensitive comparison boolean equalsignoreCase(String str): Case in-sensitive comparison Mg ne eRe pe eo Ore ETE hr) ane em etnnvetted 5 wed tor (orm pe ng feo ot _-—— compareTo() compareTo() method is used for comparing two strings lexicographically. It returns positive number, negative number or 0. Each character of both the strings is converted into a unicode value for comparison. If both the strings are equal then this method returns 0 else it returns positive or negative value. It returns int type of value. int var1 = s1.compareTo( s2 ); if s1 > s2, it returns positive number if s1
You might also like
Introduction To Java String Handling - Class - 10
PDF
No ratings yet
Introduction To Java String Handling - Class - 10
5 pages
Modern Programming Tools and Techniques-I: String
PDF
No ratings yet
Modern Programming Tools and Techniques-I: String
26 pages
String
PDF
No ratings yet
String
33 pages
String Handling
PDF
No ratings yet
String Handling
33 pages
String and Character Functions
PDF
No ratings yet
String and Character Functions
43 pages
7 Strings
PDF
No ratings yet
7 Strings
80 pages
Chapter 03 - Arrays & Strings Part-02 Strings
PDF
No ratings yet
Chapter 03 - Arrays & Strings Part-02 Strings
25 pages
13. String Handling(String Class)
PDF
No ratings yet
13. String Handling(String Class)
30 pages
String
PDF
No ratings yet
String
26 pages
String Concatenation by Concat : Public
PDF
No ratings yet
String Concatenation by Concat : Public
8 pages
02 Strings
PDF
No ratings yet
02 Strings
22 pages
Strings
PDF
No ratings yet
Strings
17 pages
Strings
PDF
No ratings yet
Strings
36 pages
Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-09-18 - Reference-Material-I
PDF
No ratings yet
Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-09-18 - Reference-Material-I
34 pages
Unit Iii1
PDF
No ratings yet
Unit Iii1
100 pages
String Manipulation Process
PDF
No ratings yet
String Manipulation Process
34 pages
Stringsinjava 210722071328
PDF
No ratings yet
Stringsinjava 210722071328
28 pages
Complete String Methods
PDF
No ratings yet
Complete String Methods
30 pages
A1281368029 - 24905 - 11 - 2020 - 13. String Handling (String Class)
PDF
No ratings yet
A1281368029 - 24905 - 11 - 2020 - 13. String Handling (String Class)
33 pages
Java String Methods
PDF
No ratings yet
Java String Methods
28 pages
Lesson - 1 - String - Class
PDF
No ratings yet
Lesson - 1 - String - Class
6 pages
String
PDF
No ratings yet
String
9 pages
String Handling
PDF
No ratings yet
String Handling
10 pages
Strings: The Objectives of This Chapter Are: To Discuss The String Class and Some of Its Methods
PDF
No ratings yet
Strings: The Objectives of This Chapter Are: To Discuss The String Class and Some of Its Methods
18 pages
CH 7 String Handling: Public Class Public Static Void
PDF
No ratings yet
CH 7 String Handling: Public Class Public Static Void
6 pages
Object Oriented Programming With Java: Department of Ce/It Unit-2 Array & String OOPJ (01CE0403)
PDF
No ratings yet
Object Oriented Programming With Java: Department of Ce/It Unit-2 Array & String OOPJ (01CE0403)
48 pages
C++ and Java
PDF
No ratings yet
C++ and Java
49 pages
String Class Method
PDF
No ratings yet
String Class Method
5 pages
Strings
PDF
No ratings yet
Strings
30 pages
Strings in JAVA
PDF
No ratings yet
Strings in JAVA
11 pages
String in Java
PDF
100% (1)
String in Java
21 pages
Using Library Classes and Packages
PDF
No ratings yet
Using Library Classes and Packages
20 pages
Strings Part5
PDF
No ratings yet
Strings Part5
13 pages
What Is A String?: Tutorial 4
PDF
No ratings yet
What Is A String?: Tutorial 4
5 pages
VENU_PART 1-OOPJ -V UNIT
PDF
No ratings yet
VENU_PART 1-OOPJ -V UNIT
46 pages
20CS6205C Fund - of JAVA Programming UNIT-II
PDF
No ratings yet
20CS6205C Fund - of JAVA Programming UNIT-II
40 pages
ITC C106 Lecture - Java Pre Defined Methods
PDF
No ratings yet
ITC C106 Lecture - Java Pre Defined Methods
21 pages
Module-1 Strings Handling
PDF
No ratings yet
Module-1 Strings Handling
32 pages
05 From Python To Java1
PDF
No ratings yet
05 From Python To Java1
30 pages
Lecture 6 (Strings in Java)
PDF
No ratings yet
Lecture 6 (Strings in Java)
33 pages
Strings
PDF
No ratings yet
Strings
34 pages
Strings in Java
PDF
No ratings yet
Strings in Java
16 pages
M6L1_lyst1713802616915
PDF
No ratings yet
M6L1_lyst1713802616915
30 pages
Java String Class Tutorial
PDF
No ratings yet
Java String Class Tutorial
5 pages
Unit-I - 4 String
PDF
No ratings yet
Unit-I - 4 String
23 pages
Chapter 4
PDF
No ratings yet
Chapter 4
24 pages
Unit VI String - Handling
PDF
No ratings yet
Unit VI String - Handling
32 pages
String Manipulation
PDF
No ratings yet
String Manipulation
20 pages
Java Strings
PDF
No ratings yet
Java Strings
7 pages
Programming in Java: String Handling
PDF
No ratings yet
Programming in Java: String Handling
29 pages
Strings: Developed by
PDF
No ratings yet
Strings: Developed by
14 pages
Lect-4 String
PDF
No ratings yet
Lect-4 String
41 pages
VXL String function_081904
PDF
No ratings yet
VXL String function_081904
10 pages
String: String Is A Group of Characters. They Are Objects of Type String in Java - Lang Package
PDF
No ratings yet
String: String Is A Group of Characters. They Are Objects of Type String in Java - Lang Package
31 pages
نسخة من Lab01
PDF
No ratings yet
نسخة من Lab01
20 pages
Strings: Create String
PDF
No ratings yet
Strings: Create String
3 pages
Java String
PDF
No ratings yet
Java String
13 pages
String and String Buffer
PDF
No ratings yet
String and String Buffer
60 pages
Java - String Class: Creating Strings
PDF
No ratings yet
Java - String Class: Creating Strings
5 pages
Related titles
Click to expand Related Titles
Carousel Previous
Carousel Next
Introduction To Java String Handling - Class - 10
PDF
Introduction To Java String Handling - Class - 10
Modern Programming Tools and Techniques-I: String
PDF
Modern Programming Tools and Techniques-I: String
String
PDF
String
String Handling
PDF
String Handling
String and Character Functions
PDF
String and Character Functions
7 Strings
PDF
7 Strings
Chapter 03 - Arrays & Strings Part-02 Strings
PDF
Chapter 03 - Arrays & Strings Part-02 Strings
13. String Handling(String Class)
PDF
13. String Handling(String Class)
String
PDF
String
String Concatenation by Concat : Public
PDF
String Concatenation by Concat : Public
02 Strings
PDF
02 Strings
Strings
PDF
Strings
Strings
PDF
Strings
Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-09-18 - Reference-Material-I
PDF
Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-09-18 - Reference-Material-I
Unit Iii1
PDF
Unit Iii1
String Manipulation Process
PDF
String Manipulation Process
Stringsinjava 210722071328
PDF
Stringsinjava 210722071328
Complete String Methods
PDF
Complete String Methods
A1281368029 - 24905 - 11 - 2020 - 13. String Handling (String Class)
PDF
A1281368029 - 24905 - 11 - 2020 - 13. String Handling (String Class)
Java String Methods
PDF
Java String Methods
Lesson - 1 - String - Class
PDF
Lesson - 1 - String - Class
String
PDF
String
String Handling
PDF
String Handling
Strings: The Objectives of This Chapter Are: To Discuss The String Class and Some of Its Methods
PDF
Strings: The Objectives of This Chapter Are: To Discuss The String Class and Some of Its Methods
CH 7 String Handling: Public Class Public Static Void
PDF
CH 7 String Handling: Public Class Public Static Void
Object Oriented Programming With Java: Department of Ce/It Unit-2 Array & String OOPJ (01CE0403)
PDF
Object Oriented Programming With Java: Department of Ce/It Unit-2 Array & String OOPJ (01CE0403)
C++ and Java
PDF
C++ and Java
String Class Method
PDF
String Class Method
Strings
PDF
Strings
Strings in JAVA
PDF
Strings in JAVA
String in Java
PDF
String in Java
Using Library Classes and Packages
PDF
Using Library Classes and Packages
Strings Part5
PDF
Strings Part5
What Is A String?: Tutorial 4
PDF
What Is A String?: Tutorial 4
VENU_PART 1-OOPJ -V UNIT
PDF
VENU_PART 1-OOPJ -V UNIT
20CS6205C Fund - of JAVA Programming UNIT-II
PDF
20CS6205C Fund - of JAVA Programming UNIT-II
ITC C106 Lecture - Java Pre Defined Methods
PDF
ITC C106 Lecture - Java Pre Defined Methods
Module-1 Strings Handling
PDF
Module-1 Strings Handling
05 From Python To Java1
PDF
05 From Python To Java1
Lecture 6 (Strings in Java)
PDF
Lecture 6 (Strings in Java)
Strings
PDF
Strings
Strings in Java
PDF
Strings in Java
M6L1_lyst1713802616915
PDF
M6L1_lyst1713802616915
Java String Class Tutorial
PDF
Java String Class Tutorial
Unit-I - 4 String
PDF
Unit-I - 4 String
Chapter 4
PDF
Chapter 4
Unit VI String - Handling
PDF
Unit VI String - Handling
String Manipulation
PDF
String Manipulation
Java Strings
PDF
Java Strings
Programming in Java: String Handling
PDF
Programming in Java: String Handling
Strings: Developed by
PDF
Strings: Developed by
Lect-4 String
PDF
Lect-4 String
VXL String function_081904
PDF
VXL String function_081904
String: String Is A Group of Characters. They Are Objects of Type String in Java - Lang Package
PDF
String: String Is A Group of Characters. They Are Objects of Type String in Java - Lang Package
نسخة من Lab01
PDF
نسخة من Lab01
Strings: Create String
PDF
Strings: Create String
Java String
PDF
Java String
String and String Buffer
PDF
String and String Buffer
Java - String Class: Creating Strings
PDF
Java - String Class: Creating Strings