1 / 22

Strings

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.

bdietz
Download Presentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings And other things

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

  3. 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.

  4. 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”

  5. 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)

  6. 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

  7. String literals • “Hello” • “Good bye” • “10,100” • “576DSW” • “700-6000” • “Bill”

  8. 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

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

  10. 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

  11. String Conversions • String.valueof(number) • Returns a string • And Integer.parseint(String) • Return a number

  12. Converting Strings • ToLowerCase() • ToUppercase()

  13. 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

  14. 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)

  15. 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.

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

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

  18. 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)

  19. 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

  20. 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

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

  22. Summary • Strings • Characters • Char • Format and sprint methods • Importance of API documentation

More Related