1. length() 2. tostring() 3. charat() 4. getchars() 5. getbytes() 6. tochararray() 7. equals() 8. equalsignorecase() 9. regionmatches() 10. startswith() 11. endswith() 12. compareto() 13. indexof() 14. lastindexof() 15. substring() 16. concat() 17. replace() String 18. trim() 19. valueof() 20. tolowercase() 21. touppercase() 22. capacity() 23. ensurecapacity() 24. setlength() 25. setcharat() 26. getchars() 27. append() 28. insert() 29. reverse() 30. delete() 31. deletecharat() 32. replace() 33. substring() 1
String is a sequence of characters. String Other languages that implement strings as character arrays Java implements strings as objects of type String. Once a String object has been created, you cannot change the characters that comprise that string Java provides two options: StringBuffer and StringBuilder. strings that can be modified after they are created. Both hold The strings within objects of type String are unchangeable means that the contents of the String instance cannot be changed after it has been created. A variable declared as a String reference can be changed to point at some other String object at any time. 2
To create an empty String The String Constructors String s = new String(); you will want to create strings that have initial values char chars[] = { 'a', 'b', 'c' }; String s = new String(chars); String(char chars[ ]) You can specify a subrange of a character array as an initializer using the following constructor: String(char chars[ ], int startindex, int numberofchars) char chars[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; String s = new String(chars, 2, 3); 3
You can construct a String object that contains the same character sequence as another String object using this constructor: String(String strobj) 4
5
String Length int length( ) char chars[] = { 'a', 'b', 'c' }; String s = new String(chars); System.out.println(s.length()); Special String Operations The automatic creation of new String instances from string literals Concatenation of multiple String objects by use of the + operator, The conversion of other data types to a string representation. There are explicit methods available to perform all of these functions, but Java does them automatically as a convenience for the programmer 6
String Literals char chars[] = { 'a', 'b', 'c' }; String s1 = new String(chars); String s2 = "abc"; you can call methods directly on a quoted string as if it were an object reference, as the following statement shows. System.out.println("abc".length()); String Concatenation String age = "9"; String s = "He is " + age + " years old."; System.out.println(s); 7
String Concatenation with Other Data Types int age = 9; String s = "He is " + age + " years old."; System.out.println(s); String s = "four: " + 2 + 2; System.out.println(s); This fragment displays four: 22 rather than the four: 4 To complete the integer addition first, you must use parentheses, like this: String s = "four: " + (2 + 2); Now s contains the string four: 4. 8
String Conversion and tostring( ) Every class implements tostring() because it is defined by Object tostring() can be overridden General form String tostring() 9
String Conversion and tostring( ) // Override tostring() for Box class. class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; } public String tostring() { return "Dimensions are " + width + " by " + depth + " by " + height + "."; } } class tostringdemo { public static void main(string args[]) { Box b = new Box(10, 12, 14); String s = "Box b: " + b; // concatenate Box object System.out.println(b); // convert Box to string System.out.println(s); } } 10
String Conversion and tostring( ) // Override tostring() for Box class. class Box { double width; double height; double depth; Box(double w, double h, double d) { width = w; height = h; depth = d; } public String tostring() { return "Dimensions are " + width + " by " + depth + " by " + height + "."; } } class tostringdemo { public static void main(string args[]) { Box b = new Box(10, 12, 14); String s = "Box b: " + b; // concatenate Box object System.out.println(b); // convert Box to string System.out.println(s); } } Output: Dimensions are 10.0 by 14.0 by 12.0. Box b: Dimensions are 10.0 by 14.0 by 12.0. 11
12
Converts all characters in a String object into a character array. 13
14
true false false true 15
Compares a specific region inside a string with another region in another string 16
true true 17
== compares two objects references to see whether they refer to the same instance 18
Output Hello equals Hello true Hello == Hello false 19
20
21
Int indexof(char ch) Int lastindexof(char ch) 22
23
7 65 7 55 11 55 44 55 24
It extracts a substring public class StringDemo2{ public static void main(string args[]){ String s1="my India"; System.out.println("indexOf(ia)"+s1.indexOf("ia")); System.out.println("substring(0,5)"+s1.substring(0,5)); } } 25
It extracts a substring public class StringDemo2{ public static void main(string args[]){ String s1="my India"; System.out.println("indexOf(ia)"+s1.indexOf("ia")); System.out.println("substring(0,5)"+s1.substring(0,5)); } } Output: indexof(ia)6 substring(0,5)my In 26
It extracts a substring Home Work 27
It extracts a substring Home Work 28
Replaces all occurrences of character with other in the invoking string 29
Replaces all occurrences of character with other in the invoking string Output: Hewwo 30
class demo { public static void main(string args[]) { String s="hello".replace("lo","ooll"); System.out.println(s); } } 31
class demo { public static void main(string args[]) { String s="hello".replace("lo","ooll"); System.out.println(s); } } Output: Helooll 32
Removes all leading and trailing whitespaces. 33
Removes all leading and trailing whitespaces. Output: Hello World 34
35
36
import java.util.scanner; public class StringDemo2{ public static void main(string args[]){ Scanner in=new Scanner(System.in); String s2=new String(); System.out.println("valueOf(5.5)"+s2.valueOf(5.5)); } } 37
import java.util.scanner; public class StringDemo2{ public static void main(string args[]){ Scanner in=new Scanner(System.in); String s2=new String(); System.out.println("valueOf(5.5)"+s2.valueOf(5.5)); } } valueof(5.5)5.5 38
39
40
41
String represents fixed length, immutable characters sequences, whereas, StringBuffer represents grow able and writable character sequences, it automatically grows to accommodate. 42
Total characters present Total size of variable 43
Total characters present Total size of variable 5 21 44
It sets capacity in advance A single character can be obtained 45
46
Buffer = Hello charat(1) = e New Buffer = Hi charat(1) = i 47
48
a=42! 49
50
insert(2,"like")= I LikeJava 51
52
53
Output: After delete:this a test. After deletecharat:his a test. 54
class demo { public static void main(string args[]) { StringBuffer sb=new StringBuffer("This is a test."); sb.replace(4,7,"kk"); System.out.println("After replace:"+sb); } } 55
class demo { public static void main(string args[]) { StringBuffer sb=new StringBuffer("This is a test."); sb.replace(4,7,"kk"); System.out.println("After replace:"+sb); } } Output: After replace:thiskk a test. 56
57
58