Download presentation
Presentation is loading. Please wait.
Published byΛεφτέρις Κρεστενίτης Modified over 5 years ago
1
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.
2
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 s1 = "Hello!"; String s2 = 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','l','o'}; x[2] = 'q';
3
Conversion to/from character arrays
To convert from a character array to a String: char[] x = new char[] {'h','e','l','l','o'}; String s1 = new String( x ); To convert from a String to a character array: char[] x = aString.toCharArray( );
4
Useful String methods Suppose we have String message = "Hello World!";
To find the length of a string: int theStringLength = message.length(); To find the character at position i (numbered from 0): int i = 4; char theChar = message.charAt( i );
5
Useful String methods To change any primitive data type to a String :
int anInteger = 17; String aString = String.valueOf( anInteger ); To append one string after another (concatenation): String joinedString = string1 + string2;
6
Useful String methods To find a particular character or String within a String : First occurrence of 'x' int location = aString.indexOf('x'); First occurrence of "world" starting from position pos: int location2 = aString.indexOf( "world", pos ); There is also a corresponding method lastIndexOf( ) occurrence of "world" starting from position pos:
7
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 str2 = str.substring( 2, 5 ); Result is "cde“ Case changes toLowerCase( ), toUpperCase( )
8
Comparing Strings A String is a reference type and so they are NOT compared with ==. The String class has a method compareTo() 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 str1 < str2, then compareTo() returns an int < 0 If str1 > str2, then compareTo() returns an int > 0 If the character at every index matches, and the strings are the same length, the method returns 0
9
Comparing Strings What is the value of result for these examples?
String str1 = "abcde" ; String str2 = "abcfg" ; int result = str1.compareTo(str2); Example 2: String str2 = "ab" ;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.