You are on page 1of 22
STRING FUNCTIONS [lett Pe 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 eet behee) 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 ee of 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 Thy 7 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 aie 6 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 ete int 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 ty i¢ 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 rong rt 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". ~— ee etd 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 etnn vetted 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