Characters Strings and the String Buffer Jim Burns

Characters, Strings and the String Buffer Jim Burns

Identifying problems that can occur when you manipulate string data String is not a simple data type like int, float, or double l String creates an instance of a class, the class String l As such it contains a reference or an address and not the actual string l So you cannot do equality comparisons of two different instances of String, because you are simply testing if the addresses are the same l

An example of incorrect code import javax. swing. JOption. Pane; public class Try. To. Compare. Strings { public static void main(String[] args) { String a. Name = "Carmen"; String another. Name; another. Name = JOption. Pane. show. Input. Dialog(null, "Enter your name"); if(a. Name == another. Name) JOption. Pane. show. Message. Dialog(null, a. Name + " equals " + another. Name); else JOption. Pane. show. Message. Dialog(null, a. Name + " does not equal " + another. Name); System. exit(0); } l }

Correct Code l l l l l import javax. swing. JOption. Pane; public class Compare. Strings { public static void main(String[] args) { String a. Name = "Carmen"; String another. Name; another. Name = JOption. Pane. show. Input. Dialog(null, "Enter your name"); if(a. Name. equals(another. Name)) JOption. Pane. show. Message. Dialog(null, a. Name + " equals " + another. Name); else JOption. Pane. show. Message. Dialog(null, a. Name + " does not equal " + another. Name); System. exit(0); } }

Three classes for working with strings l l l Character—a class whose instances can hold a single character value—provides methods that can manipulate or inspect single-character data String—a class for working with fixed-string data —that is unchanging data composed of multiple characters, strings that are immutable String. Buffer—a class for storing and manipulating changeable data composed of mulltiple characters

Manipulating Characters We know the char data type can hold any single character l Character class provides the following methods l l is. Upper. Case(), to. Upper. Case(), is. Lower. Case(), to. Lower. Case(), is. Digit(), is. Letter. Or. Digit(), is. Whitespace() l Methods that begin with ‘is…’ perform tests delivering true or false values l Methods that begin with ‘to…’ perform conversions

Declaring a String Object l l We know that characters enclosed within double quotation marks are literal strings We’ve learned to print these strings using println() and show. Message. Dialog() An literal string is an unnamed object, or anonymous object, of the String class A String variable is simply a named object of the same class. l The class String is defined in java. lang. String, which is automatically imported into every program you write

Declaring a String variable When you declare a String variable, the String itself—that is, the series of characters contained in the String—is distinct from the variable you use to refer to it. l Can initialize a String variable with or without a String constructor l

With or without a String Constructor With the constructor String a. Greeting = new String(“Hello”); l Without the constructor String a. Greeting = “Hello”; l l Unlike other classes, you can create a String object without using the keyword new or explicitly calling the class constructor

Comparing String Values Consider the following two statements: String a. Greeting = “hello”; a. Greeting = “Bonjour”; These statements are syntactically correct. What happens is that the address contained in a. Greeting is changed to point to “Bonjour” rather than “hello”, both of which are contained at different locations in memory. Eventually, the garbage collector discards the “hello” characters.

Comparing String Values The String class provides methods for comparing strings l In the example above the == sign is comparing memory addresses, not the actual strings. l The String class equals() method evaluates the contents of two String objects to determine if they are equivalent. l
![Import javax. swing. JOption. Pane; Public class Compare. Strings { public static void main(String[] Import javax. swing. JOption. Pane; Public class Compare. Strings { public static void main(String[]](http://slidetodoc.com/presentation_image_h2/db5657d28774e2c9e25644de7b244334/image-12.jpg)
Import javax. swing. JOption. Pane; Public class Compare. Strings { public static void main(String[] args) { String a. Name = “Carmen”, another. Name; another. Name = JOption. Pane. show. Input. Dialog(null, “Enter your name”); if(a. Name. equals(another. Name)) JOption. Pane. show. Message. Dialog(null, a. Name + “ equals “ + another. Name); else JOption. Pane. show. Message. Dialog(null, a. Name + “ does not equal “ + another. Name)); System. exit(0); }

The equals() method l From the code above, we can see that the equals() method returns a Boolean value of true or false

equals. Ignore. Case() Method Similar to the equals() method l Ignores case String a. Name = “Roger”; If(a. Name. equals. Ignore. Case(“ro. GER”))… evaluates to true l

compare. To() method Returns an integer that is the numeric difference between the first two nonmatching characters


Using other String Methods to. Upper. Case() and to. Lower. Case() convert any String to its uppercase and lowercase equivalent l Length() returns the length of a String l
![import javax. swing. *; public class Business. Letter { public static void main(String[] args) import javax. swing. *; public class Business. Letter { public static void main(String[] args)](http://slidetodoc.com/presentation_image_h2/db5657d28774e2c9e25644de7b244334/image-18.jpg)
import javax. swing. *; public class Business. Letter { public static void main(String[] args) { String name; String first. Name = ""; String family. Name = ""; int x; char c; name = JOption. Pane. show. Input. Dialog(null, "Please enter customer's first and last name"); x = 0; while(x < name. length()) { if(name. char. At(x) == ' ') { first. Name = name. substring(0, x); family. Name = name. substring(x + 1, name. length()); x = name. length(); } ++x; } JOption. Pane. show. Message. Dialog(null, "Dear " + first. Name + ", n. I am so glad we are on a first name basis" + "nbecause I would like the opportunity to" + "ntalk to you about an affordable insurance" + "nprotection plan for the entire " + family. Name + "nfamily. . Call A-One Family Insurance today" + "nat 1 -800 -555 -9287. "); System. exit(0); } }

x = 0; l while(x < name. length()) l { l if(name. char. At(x) == ' ') l { l first. Name = name. substring(0, x); l family. Name = name. substring(x + 1, name. length()); l x = name. length(); l } l ++x; l } l


Concatenation l You know you can concatenate strings to strings as in System. out. println(first. Name + “ “ + last. Name);

Concatenation—numbers to strings by using + The following is permissible: Int my. Age = 25; String a. String = “My age is “ + my. Age; l Another example would be l String another. String; float some. Float = 12. 34 f; another. String = “” + some. Float; l

Concatenation by using the to. String() method String the. String; Int some. Int = 10; the. String = Integer. to. String(some. Int); String a. String; double some. Double = 8. 25; a. String = Double. to. String(some. Double);

Converting Strings to Numbers Use a wrapper like the Integer class which is a part of java. lang l A wrapper is a class that is “wrapped around” a simpler element l Int an. Int = Integer. parse. Int(“ 649”) stores the value 649 in the variable an. Int l
![public class Test. Character { public static void main(String[] args) { char a. Char public class Test. Character { public static void main(String[] args) { char a. Char](http://slidetodoc.com/presentation_image_h2/db5657d28774e2c9e25644de7b244334/image-25.jpg)
public class Test. Character { public static void main(String[] args) { char a. Char = 'C'; System. out. println("The character is " + a. Char); if(Character. is. Upper. Case(a. Char)) System. out. println(a. Char + " is uppercase"); else System. out. println(a. Char + " is not uppercase"); if(Character. is. Lower. Case(a. Char)) System. out. println(a. Char + " is lowercase"); else System. out. println(a. Char + " is not lowercase"); a. Char = Character. to. Lower. Case(a. Char); System. out. println("After to. Lower. Case(), a. Char is " + a. Char); a. Char = Character. to. Upper. Case(a. Char); System. out. println("After to. Upper. Case(), a. Char is " + a. Char); if(Character. is. Letter. Or. Digit(a. Char)) System. out. println(a. Char + " is a letter or digit"); else System. out. println(a. Char + " is neither a letter nor a digit"); if(Character. is. Whitespace(a. Char)) System. out. println(a. Char + " is whitespace"); else System. out. println(a. Char + " is not whitespace"); } } //see next slide
![Test Character App public class Test. Character { public static void main(String[] args) { Test Character App public class Test. Character { public static void main(String[] args) {](http://slidetodoc.com/presentation_image_h2/db5657d28774e2c9e25644de7b244334/image-26.jpg)
Test Character App public class Test. Character { public static void main(String[] args) { char a. Char = 'C'; System. out. println("The character is " + a. Char); if(Character. is. Upper. Case(a. Char)) System. out. println(a. Char + " is uppercase"); else System. out. println(a. Char + " is not uppercase"); if(Character. is. Lower. Case(a. Char)) System. out. println(a. Char + " is lowercase"); else

System. out. println(a. Char + " is not lowercase"); a. Char = Character. to. Lower. Case(a. Char); System. out. println("After to. Lower. Case(), a. Char is " + a. Char); a. Char = Character. to. Upper. Case(a. Char); System. out. println("After to. Upper. Case(), a. Char is " + a. Char); if(Character. is. Letter. Or. Digit(a. Char)) System. out. println(a. Char + " is a letter or digit"); else System. out. println(a. Char + " is neither a letter nor a digit"); if(Character. is. Whitespace(a. Char)) System. out. println(a. Char + " is whitespace"); else System. out. println(a. Char + " is not whitespace"); } }

Learning about the String. Buffer Class Some strings are not constants, not immutable String some. Chars = “Goodbye”; some. Chars = “Goodbye Everybody”; some. Chars = “Goodbye” + “ Everybody”; l You cannot change the string “Goodbye” l To overcome these limitations, you can use the String. Buffer class l The String. Buffer class was invented to accommodate strings that are not immutable (constants) l

The String. Buffer Class Uses a buffer that is much larger than any one string; actual size of the buffer is the capacity l Provides methods that can change individual characters within a string l Must initialize String. Buffer objects as follows: l l String. Buffer event. String = new String. Buffer(“Hello there”); l Cannot use String. Buffer event. String = “Hello there”;

Methods in the String. Buffer Class set. Length() will change the length of a String in a String. Buffer object l capacity() will find the capacity of an object l Has four constructors: l l public String. Buffer() constructs a String. Buffer with no characters and a default size of 16 characters l public String. Buffer(int Capacity) creates a String. Buffer with no characters and a capacity defined by the parameter l public String. Buffer(String s) contains the same characters as those stored in the String object s

Still more methods in the String. Buffer Class Append() lets you add characters to the end of a String. Buffer object l Insert() lets you add characters at a specific location within a String. Buffer object l String. Buffer some. Buffer = new String. Buffer(“Happy Birthday); some. Buffer. insert(6, ” 30 th “); Produces “Happy 30 th Birthday”

Still more methods in String. Buffer l set. Char. At() method allows you to change a single character at a specified location l l l some. Buffer. set. Char. At(6, ’ 4’); Changes some. Buffer to “Happy 40 th Birthday” Can use char. At() method will return the character at an offset number of positions from the first character l l String. Buffer text = new String. Buffer(“Java Programming); Then text. char. At(5) returns the character ‘P’.

package Strings; import javax. swing. *; public class Repair. Name { /** * @param args */ public static void main(String[] args) { String name, save. Original. Name; int string. Length; int i; char c; name = JOption. Pane. show. Input. Dialog((null, "Please enter your first and last name"); save. Original. Name = name; string. Length = name. length(); for (i=0; i < string. Length; i++) { c = name. char. At(i); if(i ==0) if(i==0) { c = Character. to. Upper. Case (c); ); Character. to. Upper. Case(c name = c + name. substring(1, string. Length); } else if(name. char. At(i)) == ' ') { ++i; c = name. char. At(i); c = Character. to. Upper. Case (c); ); Character. to. Upper. Case(c name = name. substring(0, i) + c + name. substring(i + 1, string. Length); } } JOption. Pane. show. Message. Dialog((null, "Original name was " + save. Original. Name + "n. Repaired name is " + name); System. exit (0); System. exit(0); } // TODO Auto-generated method stub }
- Slides: 33