The Class String Outline n The String Class















- Slides: 15
The Class String
Outline n The String Class n Explicit String Objects n String variables are References n String Methods n Escape Sequences 25 December 2021 ICS 102: The class String 2
- The String Class n n There is no primitive type for strings in Java The class (type) String is a predefined class in Java that is used to store and process strings A String object is a sequence of characters that is treated as a single value. n When any “ ” (quote) appears, a String object is created automatically. A variable of type String can be given the value of a String object 25 December 2021 ICS 102: The class String 3
- Explicit String Objects n A declaration and object creation are needed for instances of the String class. For example, String name 1; name 1 = new String("Salam"); n We normally use shorthand notation (only for Strings): These two statements are equivalent. String name 1; name 1 = "Salam"; 25 December 2021 ICS 102: The class String 4
- String variables are References… Code A String word 1, word 2; word 1 = new String("Java"); word 2 = word 1; State of Memory word 1 L word 2 L After A is executed Both word 1 and word 2 are allocated memory (to store references), but the objects themselves are not yet created, so they both contain null.
-…String variables are References… Code String B One String object is created and assigned to word 1, so word 1 contains the address of this object. word 1, word 2; word 1 = new String("Java"); word 2 = word 1; word 1 String Java State of Memory word 2 L After B 25 December 2021 is executed ICS 102: The class String 6
-…String variables are References. Code String Content of word 1, which is an address, is assigned to word 2, making word 2 refer to the same object. word 1, word 2; word 1 = new String("Java"); C word 2 = word 1; word 1 String Java State of Memory word 2 After C 25 December 2021 is executed ICS 102: The class String 7
- String Methods n String Concatenation n String Length n String Starting Position n Other Useful String Operators 25 December 2021 ICS 102: The class String 8
-- String Concatenation n Concatenation: Using the + operator on two (or more) strings in order to connect them to form one longer string String greeting = "Hello "; String course = "ICS 102"; System. out. println(greeting + course); n n Hello ICS 102 A new String object is created operands are not affected. When a string is combined with almost any other type of item, the result is a string int x = 6, y = 10; String s = ". Hi. "; System. out. println(s System. out. println(x System. out. println(s 25 December 2021 + + x); y + s); (x + y)); x + y); ICS 102: The class String . Hi. 6 16. Hi. 610 9
-- String Length n We determine the number of characters in a String with the length method. String name = "Espresso!"; String str 2 = ""; String str 3; 9 str 2. length(); str 3. length(); 0 Error! Error because no object is created for str 3, so it is a null.
- String Starting Position n n Individual characters in a String can be accessed with the char. At method. Position of a character or String can be found with index. Of method. String text = "Java is fun. "; text This variable refers to the whole string. text. index. Of("is") The method returns the position of the string “is”. text. char. At(8) The method returns the character at position # 8.
-- Other Useful String Operators Method equals Meaning Checks if two strings are equal. (Use equals. Ignore. Case for case insensitive) str 1. equals( str 2 ) compare. To Compares the two strings. str 1. compare. To( str 2 ) substring Extracts the a substring from a string. str 1. substring( 1, 4 ) str 1. substring( 5 ) trim to. Upper. Case Removes the leading and trailing spaces. str 1. trim( ) Converts a string to all caps string. (Use to. Lower. Case for all small) str 1. to. Upper. Case( )
- Escape Sequences … n A backslash () immediately preceding a character (i. e. , without any space) denotes an escape sequence or an escape character n n 25 December 2021 The character following the backslash does not have its usual meaning Although it is formed using two symbols, it is regarded as a single character ICS 102: The class String 13
… - Escape Sequences 25 December 2021 ICS 102: The class String 14
THE END 25 December 2021 ICS 102: The class String 15