Strings in Java Strings in Java are also

  • Slides: 12
Download presentation
Strings in Java • Strings in Java are also a reference type. – They

Strings in Java • Strings in Java are also a reference type. – They are similar to an array of characters in that they represent an ordered sequence of characters with positions numbered from 0. – String constants are enclosed in double quotes. • Example: String message = "Hello World!"; System. out. println( message ); • The class String provides many useful methods. 1

Comparison with character arrays • Although a String has many similarities to an array

Comparison with character arrays • Although a String has many similarities to an array of characters, there are some important differences. • You don’t need to use new to create a string unless you want to use one of the String class constructors String s 1 = "Hello!"; String s 2 = new String("Hello!"); • You DO NOT use [] to access the characters in the string. • The contents of a String cannot be changed after it has been created. – You can have a String variable refer to a different string, but you cannot change characters within the original string. – There is NO equivalent to: char[] x = new char[] {'h', 'e', 'l', 'o'}; x[2] = 'q'; 2

Conversion to/from character arrays • To convert from a character array to a String:

Conversion to/from character arrays • To convert from a character array to a String: char[] x = new char[] {'h', 'e', 'l', 'o'}; String s 1 = new String( x ); • To convert from a String to a character array: char[] x = a. String. to. Char. Array( ); 3

Useful String methods • Suppose we have String message = "Hello World!"; • To

Useful String methods • Suppose we have String message = "Hello World!"; • To find the length of a string: int the. String. Length = message. length(); • To find the character at position i (numbered from 0): int i = 4; char the. Char = message. char. At( i ); 4

Useful String methods • To change any primitive data type to a String :

Useful String methods • To change any primitive data type to a String : int an. Integer = 17; String a. String = String. value. Of( an. Integer ); • To append one string after another (concatenation): String joined. String = string 1 + string 2; 5

Useful String methods • To find a particular character or String within a String

Useful String methods • To find a particular character or String within a String : – First occurrence of 'x' int location = a. String. index. Of('x'); – First occurrence of "world" starting from position pos: int location 2 = a. String. index. Of( "world", pos ); – There is also a corresponding method last. Index. Of( ) occurrence of "world" starting from position pos: 6

Useful String methods • Substrings: – The substring method returns a String consisting of

Useful String methods • Substrings: – The substring method returns a String consisting of the characters starting from the first position inclusive up to but NOT including the second position: String str = "abcdefghijklmno"; String str 2 = str. substring( 2, 5 ); – Result is "cde“ • Case changes to. Lower. Case( ), to. Upper. Case( ) 7

Comparing Strings • A String is a reference type and so they are NOT

Comparing Strings • A String is a reference type and so they are NOT compared with ==. • The String class has a method compare. To() to compare 2 strings. – The characters in each string are compared one at a time from left to right, using the collating sequence. – The comparison stops after a character comparison results in a mismatch, or one string ends before the other. • If str 1 < str 2, then compare. To() returns an int < 0 • If str 1 > str 2, then compare. To() returns an int > 0 – If the character at every index matches, and the strings are the same length, the method returns 0 8

Comparing Strings • What is the value of result for these examples? • Example

Comparing Strings • What is the value of result for these examples? • Example 1: String str 1 = "abcde" ; String str 2 = "abcfg" ; int result = str 1. compare. To(str 2); • Example 2: String str 1 = "abcde" ; String str 2 = "ab" ; int result = str 1. compare. To(str 2); 9

String Methods • Some Basic methods of String: – length() • String s =

String Methods • Some Basic methods of String: – length() • String s = “Hello Worldn”; • int num. Of. Characters = s. length(); – char. At(int index) • String s = “Saeid”; • char c = s. char. At(3); – equals(Object an. Object) • String s 1 = “Hello World”; • String s 2 = “Hello World”; • if (s 1 == s 2) //12 // ‘i’ Never true • if (s 1. equals(s 2)) True since s 1 and s 2 contains a same text 10

String Methods (2) • Some Useful methods of String: – starts. With(String prefix) •

String Methods (2) • Some Useful methods of String: – starts. With(String prefix) • String s = “Hello Worldn”; • if (s. starts. With(“Hell”)) // true – to. Lower. Case() • String s = “Hello Worldn”; • String s 2 = s. to. Lower. Case(); // “hello world” – value. Of(int i) • int n = 102; • String s = String. value. Of(n); // “ 102” 11

String Methods (3) • Some Advanced methods of String: – index. Of(String str) •

String Methods (3) • Some Advanced methods of String: – index. Of(String str) • String s = “Hello Worldn”; • int i = s. index. Of(“W”); // 6 • i = s. index. Of(“orl”); // 7 – substring(int begin. Index, int end. Index) String s = “Hello Worldn”; • String s 2 = s. substring(6, 10); // ”Worl” – trim() • String s = “ t. Worldn”; • String s 2 = s. trim(); // “World” 12