Presentation is loading. Please wait.

Presentation is loading. Please wait.

16 Strings.

Similar presentations


Presentation on theme: "16 Strings."— Presentation transcript:

1 16 Strings

2 Previously Classes Methods Scoping public private static

3 Overview String as an Object String as an Array of Characters
Equalities Length Index Substring

4 The Java String Class Java String is the representation of a group of consecutive characters Simplest String is a String Literal String strGreeting = "Hello world!"; You can also create a String from an array of Characters char[] strHelloArray = {'h','e','l','l','o','.'}; String strHelloString = new String(strHelloArray); System.out.println(strHelloString);

5 Immutable Java String Class is immutable
Once created and initialized, cannot be changed on the same reference The only way to change the value of immutable variables is to create a new object and point to that instead

6 Immutable String strCityName; // define the variable
Memory String strCityName; // define the variable strCityName = "Valencia"; System.out.println(strCityName); strCityName = "London"; strCityName = "Madrid"; a L o n d V l e c i R m M r Valencia London Madrid

7 Immutable By default variables are mutable
Can also be made immutable by declaring them final The String class has a number of methods that appear to modify strings Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation

8 What is the result of the comparison?
Equalities A String may contain the same characters without being the same object String str1stPersonsName = "John"; String str2ndPersonsName = "Peter"; String str3rdPersonsName = new String("John"); String str4rdPersonsName = str1stPersonsName; What is the result of the comparison? str1stPersonsName == str2ndPersonsName str1stPersonsName == str3rdPersonsName str2ndPersonsName == str3rdPersonsName str1stPersonsName == str4thPersonsName false true

9 Equalities Use the method equals(String) to compare the contents of two strings Use operator == to see if both string objects are the same Not that they contain the same group of characters but they are the same object

10 What is the result of the equalities?
String str1stPersonsName = "John"; String str2ndPersonsName = "Peter"; String str3rdPersonsName = new String("John"); String str4rdPersonsName = str1stPersonsName; What is the result of the equalities? str1stPersonsName.equal(str2ndPersonsName) str1stPersonsName.equal(str3rdPersonsName) str2ndPersonsName.equal(str3rdPersonsName) str1stPersonsName.equal(str4thPersonsName) false true

11 Properties int length() char charAt(int)
Returns number of characters that compose the string String str1stPersonsName = "John"; str1stPersonsName.length() char charAt(int) Returns the character at the specified position in the base string str1stPersonsName.charAt(3) 4 'c'

12 Searching Strings String substring(...) int indexof(...)
Return a sub-string of the base string str1stPersonsName.subString(1, 2); int indexof(...) Returns the index where the specified string exists within the base string str1stPersonsName.indexof("hn"); str1stPersonsName.indexof("hl"); "oh" starting position length 2 -1

13 Split You can split up strings to get an array of strings using the split method This uses a regular expression to break up a string So you can create an array of words in a sentence by searching for the space character HOWEVER a special character needs to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" ! Any string in Java that uses any of the special characters like, “ \ must preceded by \ to escaped is special meaning and use it as character, e.g. "Valencia \"Spain\" Europe"

14 Split String strCityNames = "Valencia London Madrid Rome"; String astrCityNames; astrCityNames = strCityNames.split(" "); System.out.println("Num cities " + astrCityNames.length()); for (String strCityName : astrCityNames) { System.out.println(strCityName ); } // end for Num cities 4 Valencia London Madrid Rome


Download ppt "16 Strings."

Similar presentations


Ads by Google