Upload
marian-bates
View
220
Download
0
Embed Size (px)
Citation preview
Strings
And other things
Strings Overview
The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes Printf() method The StringBuffer class
What is a string
A sequence of characters Representing alphanumeric data. Defined in the java.lang.String class String is a class and objects of String type
are reference variables.
Making a string
Declared as a typical object would be. String s= new String(“Hello”)
Or using a short hand that java provides String s = “Hello”
The string class provides many methods. charAt(int idx) compareTo(string s) concat(string s) equals(string s) indexOf(int ch,int fromhere) lastIndexOf(int ch,int fromhere) length() replace(char old, char new) substring(int beginidx, int endidx) trim() toUpper(String s)
Joining strings
Concatenation is the operation of joining one string on the end of another.
S3 = S.concat(S2) Or S3 = S + S2 Java will implicitly type cast a numeric
value as a string
String literals
“Hello” “Good bye” “10,100” “576DSW” “700-6000” “Bill”
String equality
String s1=“Hello” String s2=“Hello”
If (s1==s2) System.out.print(“Equal”) Not equal
If (s1.equals(s2)) System.out.print(“Equal”) Equal
String s3=s1 If (s3==s1) System.out.print(“Equal”)
Equal
String comparisons
If S1 < S2 may not work correctly If (S1.compareTo(s2)) < 0
System.out.print(“Less then”) If (S1.compareTo(s2)) == 0
System.out.print(“Equals”) If (S1.compareTo(s2)) > 0
System.out.print(“Greater then”)
Finding strings in strings
indexOf(int ch,int fromhere) lastIndexOf(int ch,int fromhere) indexOf(String S, int fromhere) S1=“Hello” S1.indexof(‘l’)
Will return 2 S1.lastindexof(‘l’)
Will return 3
String Conversions
String.valueof(number)Returns a string
And Integer.parseint(String)Return a number
Converting Strings
ToLowerCase() ToUppercase()
The stringbuffer class
Like the string class Why 2 string classes? Other methods: .delete(int loc, int toloc) .reverse() .capacity() the size of the stringbuffer
Char data type
A char is a single unicode value Unicode is a code used to specify character
data.
char c; char[] carray = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ }; A character array can be used to build a string String s= new String(carray)
Command line arguments
Command line arguments are passed to the args array in the main method.
We can find the length of this array and process these arguments in our program.
That Character class
A wrapper class These are likes like Double and Integer which can be
used in place of the primitive data types. There is one for each primitive data type
Provides methods like: compareTo( char) equals(char) Character.isDigit(char) // a static method Also: isLetter(char) and isLowerCase()…
StringBuilder
An object that can be created to manipulate strings values mutably then store the result back into a string.
Methods: append( String) insert(index, String) deleteCharAT( index) replace( startindex, endindex, string) toString()
StringTokenizer
Used to break strings apart. This process is also known as parsing a string.
A token is a special character used to delimit the parts of a string. nextToken() countTokens()
The default token is set to “ \t\n\r\f” the white space characters.
We can change this using the optional constructor.. StringTokenizer( string, tokenString)
Formatting data
The printf() method of PrintStream class Format specifiers:
%.2f - a double value, 2 decimals %d - an integer %x - hexvalue
System.out.printf(“data = %.2f”, data); Formatting dates:
%tA, %tB, %td, %tY For day, month, dayof month, year
Argument index value
System.out.printf(“%1$d, %2$.2f” , int, double) The 1$ and 2$ are used to specify the index of
the parameter to use. By default the value of 1 is used. Field width: %3d - specifies the integer with room for 3 digits
Justification
%-15s - left justify a string of 15 characters %15s- default right justify the string
The format method Formatter f = new Formatter() F.format( “formatstring”, args…)
Or outstring =String.format( formatstring, args…)
Summary
Strings Characters Char Format and sprint methods Importance of API documentation