Section 12.2. Strings: Character Sequences


[Page 387 (continued)]

12.2. Strings: Character Sequences

Text is typically manipulated as strings. In Java a string is a sequence of characters. Strings in Java are not just arrays of characters as they are in some other languages, like C. Strings are objects of the String class.

You use a double quote to indicate the start and end of a string in Java. If you want to write a Java program that creates HTML pages, you will need strings that contain quotes. If you want the string to include a double quote, you must use a backslash '\' in front of it.

> System.out.println("He said, \"Hi.\""); He said, "Hi."


You can include single quotes in strings.

> System.out.println("He won't go out!"); He won't go out!


A string contains a sequence of characters. We can think about the characters of the string as being in boxes, each with its own index number. You can get a character from a string using the method charAt(index).

> String hello = "Hello"; > System.out.println(hello.charAt(0)); H > System.out.println(hello.charAt(2)); l



[Page 388]

You can use a for loop to walk through all the characters of a string.

> String test = "Hello"; > for (int i = 0; i < test.length(); i++)      System.out.println(test.charAt(i)); H e l l o


Notice that you do need the parentheses after length(). It is a method of the String class, not a public field as it is for an array.

12.2.1. Unicode

Java uses Unicode to encode each character. Unicode is an encoding for characters where two bytes are used for each character. Two bytes gives us 65,536 possible combinations. Having that many possible combinations allows us to go beyond a simple Latin alphabet, numbers, and punctuation. We can represent Hiragana, Katakana, and other glyph (graphical depictions of characters) systems.

What this should tell you is that there are many more possible characters than can be typed at a standard keyboard. Not only are there special symbols, but there are invisible characters like tabs and backspace. We specify these in Java strings (and in many other languages, such as C) using backslash escapes. Backslash escapes are the backslash key \ followed by a character.

  • \" allows for a double quote inside of a string.

  • \t is the same as typing the tab key.

  • \b is the same as typing the backspace key (which is not a particularly useful character to put in a string, but you can). When you print \b, it shows up as a box on most systemsit's not actually printable.

  • \n is the same as typing the enter/return key. It is often called the new-line character.

  • \uXXXX where XXXX is a code made up of 0-9 and A-F (known as a hexadecimal number) represents the Unicode character with that code. You can look up the codes at http://www.unicode.org/charts.

Here are some strings with special characters in them. Try out a string with a backspace in it for yourself.

> System.out.println("A string with a tab \t in it"); A string with a tab       in it > System.out.println("A string with a newline character \n in it"); A string with a newline character  in it



[Page 389]

12.2.2. String Methods

The String class has many methods for working with String objects.These methods are useful for processing text.

  • charAt(int position) returns the character at the given position in the string.The first character is at position 0, just as the first element of an array is at index 0.

    > String str1 = "Bye"; > System.out.println(str1.charAt(0)); B

  • compareTo(Object o) returns a negative number if this object is less than the passed object, 0 if this object is equal to the passed object and a positive number if this object is greater than the passed object.

    > String str1 = "Bye"; > String str2 = "Hi"; > String str3 = new String("Bye"); > System.out.println(str1.compareTo(str2)); -6 > System.out.println(str2.compareTo(str1)); 6 > System.out.println(str1.compareTo(str3)); 0

  • substring(int n,int m) returns a new string which is a substring of the string starting at the nth character and preceding up to but not including the mth character. A substring includes part of the original string.

    > String str2 = "Do you go to school?"; > String str3 = str2.substring(3,6); > System.out.println(str3); you

  • substring(int n) returns a new string which is a substring of the string starting at the nth character and including the rest of the characters in the string.

    > str3 = str2.substring(6); > System.out.println(str3);  go to school?

  • startsWith(String prefix) returns true if the string starts with the given prefix, else it will return false.

    > String letter = "Mr. Guzdial requests the "; > letter = letter + "pleasure of your company ..."; > System.out.println(letter.startsWith("Mr.")); true > System.out.println(letter.startsWith("Mrs.")); false

  • endsWith(String suffix) returns true if the string ends with the given suffix, else it will return false.


    [Page 390]
    > String filename="barbara.jpg"; > if (filename.endsWith(".jpg")) System.out.println("it is a picture"); it is a picture

  • indexOf(String str) returns the first index of the passed str, if it is found. If str isn't in the current string, it will return 1.

    > System.out.println(letter); Mr. Guzdial requests the pleasure of your company ... > System.out.println(letter.indexOf("Guzdial")); 4 > System.out.println(letter.indexOf("Mark")); -1

  • indexOf(String str, int fromIndex) returns the first index of the passed str at or after the passed fromIndex, if it is found. If str isn't in the current string at or after the fromIndex, it will return 1.

    > String t = "That which is, is.That which is not, is not."; > System.out.println(t.indexOf("is",14)); 15

  • lastIndexOf(String str) returns the last index of the passed str, if it is found.If str isn't in the current string, it will return 1.

    > String s = "It is a nice day, isn't it?"; > System.out.println(s.lastIndexOf("it")); 24

  • lastIndexOf(String str, int index) returns the last index of the passed str found looking backward starting at index. If str isn't in the current string before the given index, it will return 1.

    > String s = "It is a nice day, isn't it?"; > System.out.println(s.lastIndexOf("is",17)); 3

  • toUpperCase() returns a new string with all the characters in uppercase.

    > System.out.println("Hello".toUpperCase()); HELLO

  • toLowerCase() returns a new string with all the characters in lowercase.

    > System.out.println("Hello".toLowerCase()); hello

  • replace(String oldStr, String newStr) returns a new string with the characters in the oldStr replaced with the characters in the newStr for all occurrences of the oldStr. This is new in Java 1.5.

    > System.out.println(letter); Mr.Guzdial requests the pleasure of your company ... > System.out.println(letter.replace("a","!")); Mr.Guzdi!l requests the ple!sure of your comp!ny ...


  • [Page 391]
  • replaceAll(String regex, Sting newStr) this will return a new string with all the matching substrings specified by the regular expression (regex) replaced with the characters in newStr. A regular expression can be just a string of characters, or it can also use special characters to indicate that it will match any character, any digit, only uppercase characters, and so on. If you use it with a string of characters, it is the same as the replace method.

    > System.out.println(letter); Mr.Guzdial requests the pleasure of your company ... > System.out.println(letter.replaceAll("a","!")); Mr.Guzdi!l requests the ple!sure of your comp!ny ...

    To replace all digits (09) with a space, do:

    > String test = "This9 is a test7"; > System.out.println(test.replaceAll("\\d"," ")); This  is a test

    To remove all non-digits, do:

    > String input = "7a8c"; > System.out.println(input.replaceAll("\\D","")); 78

  • replaceFirst(String regex, Sting newStr) this will return a new string with the first substring that matches the regular expression specified by regex replaced with the characters in newStr. A regular expression can be just a string of characters or it can also use special characters to indicate that it will match any character, any digit, only uppercase characters, and so on.

    > System.out.println(letter); Mr.Guzdial requests the pleasure of your company ... > System.out.println(letter.replaceFirst("a","!")); Mr.Guzdi!l requests the pleasure of your company ...

  • split(String regex) this will return an array of String objects.It will split the current string into many strings by breaking it wherever it matches the regular expression specified in regex.

    > System.out.println(letter); Mr.Guzdial requests the pleasure of your company ... > String[] strArray = letter.split(" "); > for (int i = 0; i < strArray.length; i++)      System.out.println(strArray[i]); Mr. Guzdial requests the pleasure of your company ...


  • [Page 392]
  • TRim() this will return a new string with all white space (spaces and tabs) removed from the beginning and end of the string.

    > String strWithSpaces = " Janet Hund "; > System.out.println(strWithSpaces.trim()); Janet Hund

    These methods can be cascadedone modifying the result of another.

    > String test ="This is a test of Something." > System.out.println(test.substring(5).toUpperCase()); IS A TEST OF SOMETHING.

12.2.3. Processing Delimited Strings Using Split

Sometimes you get data for an object as a delimited string.A delimited string is a string with special characters that separate the string into different parts.If we read student data from a file it may be represented in a file as a delimited string.In the strings below the name is separated from the grades by ':' and the grades are separated from each other with a comma (',').

Jane Dorda       :88, 92, 95, 87, 93, 85 Mike Koziatek    :75, 92, 83, 81, 91, 87 Sharquita Edwards:91, 93, 95, 92, 94, 99


Let's add another constructor to the Student class which takes a delimited string, the delimiter that separates the name from the grades, and the delimiter that separates the grades. It should parse (break it up into parts) the delimited string and fill in the Student fields.We can use the split method of the String class to split the string into the name and grades and then split again to split the grades up into an array of strings. We will need to convert the array of strings into an array of doubles. To do this we can use the method parseDouble(String doubleStr), which is a class (static) method of the Double class.

Making it Work Tip: Converting Strings to Numbers

You often need to convert something from a string to a number. Remember that these are represented differently in the computer. Each of the wrapper classes (Integer, Float, Double, etc.) has a class (static) method to do this. Each of these can result in a runtime exception (error) if the string doesn't contain a valid number. To convert a string to an integer, use Integer.parseInt(intStr).


Program 101. Constructor that Takes a Delimited String
(This item is displayed on pages 392 - 393 in the print version)

/**  * Constructor that takes a delimited string, the  * name delimiter, and the grade delimiter.  It fills  * in the fields from the delimited string.  * @param delimString student information as a  * delimited string  * @param nameDelim what delimits the name field 
[Page 393]
* from the grades * @param gradeDelim what delimits the grades */ public Student(String delimString, String nameDelim, String gradeDelim) { // split string based on name delimiter String[] splitArray = delimString.split(nameDelim); this.name = splitArray[0].trim(); // get the grade string and break it and convert to double String grades = splitArray[1]; String[] gradeStrArray = null; if (grades != null) { gradeStrArray = grades.split(gradeDelim); this.gradeArray = new double[gradeStrArray.length]; for (int i = 0; i < gradeStrArray.length; i++) this.gradeArray[i] = Double.parseDouble(gradeStrArray[i]); } }


You can try this out using the following main method:

/* Used to test */ public static void main (String[] args) {   // test the constructor that takes a delimited string   Student student3 =     new Student("Susan Ericson:50,60,70,80,90,100",":",",");   System.out.println(student3); }


Compile and run Student to test the new constructor.

> java Student Student object named: Susan Ericson Average: 75.0


12.2.4. Strings Don't Have a Font

Strings don't have a font (characteristic look of the letters) or style (boldface, italics, underline, and other effects applied to a string) associated with them. Font and style information is added to strings in word-processors and other programs. Typically, these are encoded as style runs.

A style run is a separate representation of the font and style information with indices into the string for where the changes should take place. For example, The old brown fox runs might be encoded as [[bold 0 6][italics 8 12]].

Most software that manages formatted text will encode strings with style runs as an object. Objects have data associated with them, perhaps in several parts (like strings and style runs). Objects know how to act upon their data, using methods that may be known only to objects of that type. If the same method name is known to multiple objects, it probably does the same thing, but maybe not in the same way.


[Page 394]


Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net