1 / 12

Introduction to Programming G50PRO University of Nottingham Unit 10 : Strings

Introduction to Programming G50PRO University of Nottingham Unit 10 : Strings. Essam Eliwa http://www.cs.nott.ac.uk/~eoe eoe@cs.nott.ac.uk Room B49. Overview. Java String classes The String Class Example : StringDemo Concatenating Strings StringBuffer Class Example : ReverseString

Download Presentation

Introduction to Programming G50PRO University of Nottingham Unit 10 : 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. Introduction to ProgrammingG50PROUniversity of NottinghamUnit 10 : Strings Essam Eliwa http://www.cs.nott.ac.uk/~eoe eoe@cs.nott.ac.uk Room B49

  2. Overview • Java String classes • The String Class • Example : StringDemo • Concatenating Strings • StringBuffer Class • Example : ReverseString • Converting Strings to Numbers • String valueOf Method http://www.cs.nott.ac.uk/~eoe

  3. Java String classes • Two java classes that store and manipulate Strings: • String, for constant strings.(Java strings are immutable;  once a string object has a value, it cannot be changed) • StringBuffer, is like a String, but can be modified. http://www.cs.nott.ac.uk/~eoe

  4. creating String and StringBuffer objects  : • String • String str = new String(“hello”); • String str = “hello”; • char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloString = new String(helloArray); • StringBuffer • StringBuffer dest = new StringBuffer();// Constructs a string buffer with no characters in it and an initial capacity of 16 characters. • StringBuffer dest = new StringBuffer(“hello”);// Constructs a string buffer initialized to hello Check Java API for all String and StringBuffer Constructors http://www.cs.nott.ac.uk/~eoe

  5. The String Class • The String class is defined in the java.lang package (and isimplicitly imported by default) • Many helpful methods are defined in the String class such as • length() • equals(String) • toUpperCase() • toLowerCase() • indexOf(String) • subString(int begin) • subString(int begin, int end) • charAt(int index) http://www.cs.nott.ac.uk/~eoe

  6. Example : StringDemo public class StringDemo { public static void main(String[] args) { String str = “HELLO"; int len = str.length(); char[] tempCharArray = new char[len]; char[] charArray = new char[len]; // put original string in an array of chars for (int i = 0; i < len; i++) { tempCharArray[i] = str.charAt(i); } // reverse array of chars for (int j = 0; j < len; j++) { charArray[j] = tempCharArray[len - 1 - j]; } String revStr = new String( charArray ); System.out.println(revSTR); }//end of main }//end of class http://www.cs.nott.ac.uk/~eoe

  7. Concatenating Strings • The String class includes a method for concatenating two strings: • string1.concat(string2); //This returns a new string that is string1 with string2 added to it at the end. • Strings are more commonly concatenated with the + operator, as in: • String str = "Hello," + " world" + "!" // which sets str value to "Hello, world!" http://www.cs.nott.ac.uk/~eoe

  8. StringBuffer Class • StringBuffer represents extendable and writable character sequences. • The main operations on a StringBuffer are the append and insert methods: • The append method always adds these characters at the end of the buffer • The insert method adds the characters at a specified point. http://www.cs.nott.ac.uk/~eoe

  9. Example : ReverseString public class ReverseString {    public static String reverseIt( String source ) {      int len = source.length();       StringBuffer dest = new StringBuffer( len );      for ( int i = len - 1; i >= 0; i-- ) {         dest.append( source.charAt( i ) );       }       return dest.toString();    } // end method reverseIt } // end ReverseString http://www.cs.nott.ac.uk/~eoe

  10. Converting Strings to Numbers • The String class itself does not provide any methods forconverting a String to numbers • Four of the "type wrapper" classes offer methods to convert a given String to a number • Integer • Double • Float • Long String str = “5.12”; float a = (Float.valueOf( str ) ).floatValue(); int i = Integer.parseInt( str ); http://www.cs.nott.ac.uk/~eoe

  11. String valueOf Method • String class offers valueOf() methods which converts between various types to String boolean result = true;int in = 234;double dou = 34.86;float flt = 54.889f;System.out.println("boolean value:“ + String.valueOf(result)        + "\ninteger value: " + String.valueOf(in)        + "\ndouble value: " + String.valueOf(dou)        + "\nfloat value: " + String.valueOf(flt)); http://www.cs.nott.ac.uk/~eoe

  12. Summary • Java String classes • The String Class • Example : StringDemo • Concatenating Strings • StringBuffer Class • Example : ReverseString • Converting Strings to Numbers • String valueOf Method http://www.cs.nott.ac.uk/~eoe

More Related