120 likes | 235 Views
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
E N D
Introduction to ProgrammingG50PROUniversity of NottinghamUnit 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 • Converting Strings to Numbers • String valueOf Method http://www.cs.nott.ac.uk/~eoe
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
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
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
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
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
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
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
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
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
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