22
Strings And other things

Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

Embed Size (px)

Citation preview

Page 1: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

Strings

And other things

Page 2: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

Strings Overview

The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes Printf() method The StringBuffer class

Page 3: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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.

Page 4: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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”

Page 5: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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)

Page 6: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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

Page 7: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

String literals

“Hello” “Good bye” “10,100” “576DSW” “700-6000” “Bill”

Page 8: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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

Page 9: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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”)

Page 10: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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

Page 11: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

String Conversions

String.valueof(number)Returns a string

And Integer.parseint(String)Return a number

Page 12: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

Converting Strings

ToLowerCase() ToUppercase()

Page 13: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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

Page 14: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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)

Page 15: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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.

Page 16: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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()…

Page 17: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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()

Page 18: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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)

Page 19: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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

Page 20: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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

Page 21: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

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…)

Page 22: Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes

Summary

Strings Characters Char Format and sprint methods Importance of API documentation