DOC PREVIEW
Penn CIT 591 - String and StringBuilder

This preview shows page 1-2-3-25-26-27 out of 27 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

String and StringBuilder Part I String Jan 14 2019 About Strings Strings are objects but there is a special syntax for writing String literals Hello Strings unlike most other objects have a defined operation as opposed to a method This is String concatenation Strings can contain any character but some of them must be escaped in order to write them in a literal stands for the double quote character n stands for the newline character stands for the backslash character Each of these is written as a two character sequence but represents a single character in the string 2 Useful String methods I char charAt int index boolean startsWith String prefix Returns the character at the given index position 0 based Tests if this String starts with the prefix String boolean endsWith String suffix Tests if this String ends with the suffix String 3 Useful String methods II boolean equals Object obj boolean equalsIgnoreCase String other Tests if this String is the same as the obj which may be any type false if it s not a String Tests if this String is equal to the other String where case does not matter int length Returns the length of this string note that this is a method not an instance variable 4 Useful String methods III int indexOf char ch int indexOf char ch int fromIndex Returns the position of the first occurrence of ch in this String or 1 if it does not occur Returns the position of the first occurrence of ch starting at not after the position fromIndex There are two similar methods that take a String instead of a char as their first argument 5 Useful String methods IV int lastIndexOf char ch int lastIndexOf char ch int fromIndex Returns the position of the last occurrence of ch in this String or 1 if it does not occur Returns the position of the last occurrence of ch searching backward starting at position fromIndex There are two similar methods that take a String instead of a char as their first argument 6 Useful String methods V String substring int beginIndex Returns a new string that is a substring of this string beginning with the character at the specified index and extending to the end of this string String substring int beginIndex int endIndex Returns a new string that is a substring of this string beginning at the specified beginIndex and extending to the character at index endIndex 1 Thus the length of the substring is endIndex beginIndex 7 Understanding index With charAt index indexOf x and lastIndexOf x just count characters starting from zero She said Hi 0 1 2 3 4 5 6 7 8 9 10 11 12 13 With substring from and substring from to it works better to count positions between characters She said Hi 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 So for example substring 4 8 is said and substring 8 12 is H If indexOf is 8 then substring 0 indexOf is She said and substring indexOf 1 is Hi 8 Useful String methods VI String toUpperCase String toLowerCase Returns a new String similar to this String in which all letters are uppercase Returns a new String similar to this String in which all letters are lowercase String trim Returns a new String similar to this String but with whitespace removed from both ends 9 Useful String methods VII String split String regex Breaks the string up into an array of strings The parameter is a regular expression that defines what separates the strings For example String s one two three String ss s split This assigns the array one two three to ss Regular expressions are complex expressions that assign meanings to many common punctuation marks such as period and Hence regular expressions are powerful but can be treacherous if you aren t very familiar with them 10 Finally a useless String method String toString Returns this String Why do we have this method Consistency Every Object has a toString method 11 Strings are immutable A String once created cannot be changed None of the preceding methods modify the String although several create a new String Statements like this create new Strings myString myString anotherCharacter Creating a few extra Strings in a program is no big deal Creating a lot of Strings can be very costly 12 More about equals If you write String s abc String t abc the compiler only creates the string abc once and makes s and t both refer to this one string However if you now write String u a bc the test s u will be false It can do this because strings are immutable Hence the test s t will be true This is because they are different strings Moral Use equals for strings not 13 Still more about equals Suppose you want to test whether a variable name has the value Dave Here s the obvious way to do it if name equals Dave But you could also do it this way if Dave equals name It turns out that the second way is usually better Why If name null the first way will cause a NullPointerException but the second way will just return false 14 Strings Etc Part II StringBuilder Jan 14 2019 About StringBuilders A StringBuilder has a capacity the number of characters it can hold and a length the number of characters it is currently holding If the capacity is exceeded the StringBuilder is copied to a new location with more room StringBuilder is a reimplementation of StringBuffer The API collection of methods is the same StringBuffers are threadsafe but StringBuilders are more efficient StringBuilders are used to implement String concatenation Whenever you say String s ab cd Java creates a StringBuffer containing the characters a and b appends the characters c and d to it and converts the result back to a String As you might guess this isn t terribly efficient but it s fine if you don t overdo it 16 StringBuilder constructors StringBuilder StringBuilder int capacity Constructs a StringBuilder with a capacity of 16 characters Constructs a StringBuilder with the requested capacity StringBuilder String str Constructs a StringBuilder containing the String str 17 Useful StringBuilder methods I StringBuilder append X Appends X to the end of this StringBuilder also as a convenience returns this StringBuilder The append method is so heavily overloaded that it will work with any argument if the argument is an object its toString method is used 18 Useful StringBuilder methods II int length Returns the number of characters in this StringBuilder void setLength int newLength Sets the number of characters in this StringBuilder this may result in truncation of characters at the end or addition of null characters 19 Useful StringBuilder methods III char charAt int index void setCharAt int


View Full Document

Penn CIT 591 - String and StringBuilder

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Loading Unlocking...
Login

Join to view String and StringBuilder and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view String and StringBuilder and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?