1 / 10

Manipulation of String Objects

Manipulation of String Objects. Extraction of individual characters from Strings The Character wrapper class Counting letters String Buffers -- a class for manipulating String objects Constructing StringBuffer objects Editing StringBufer objects.

dena
Download Presentation

Manipulation of String Objects

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. Manipulation of String Objects Extraction of individual characters from Strings The Character wrapper class Counting letters String Buffers -- a class for manipulating String objects Constructing StringBuffer objects Editing StringBufer objects

  2. Extraction of Indvidual Characters from Strings String objects cannot be changed, but the individual characters can be observed using the method: char charAt (int index) Use of this method is illustrated by a simple example; import java.io.*; publicclass CountTheIs { publicstaticvoid main (String [ ] args) { String str = new String (“Mississippi”); int numChar = str.length(), numIs = 0; System.out.print(“The string is : ” + str); for(int i = 0; i < numChar; i++) if (str.charAt(i) ==‘i’) numIs++; System.out.println(“ The number of I\’s is: ” + numIs); } }

  3. Space, tab, CR used, for ex., to append ch to str The Character Wrapper Class The wrapper class Character has methods for identifying and manipulating primitive char variables publicstaticboolean isLetter(char ch) publicstaticboolean isDigit(char ch) publicstaticboolean isLowerCase(char ch) publicstaticboolean isUpperCase(char ch) publicstaticboolean isSpaceChar(char ch) publicstaticboolean isWhitespace(char ch) publicstaticchar toLowerCase(char ch) //or toUpperCase publicstaticString toString(char ch)

  4. Counting Letters The following example program illustrates how static methods in the wrapper class Character can be used with the String method charAt to identify characteristics of a String. import java.io.*; public class StringData { publicstaticvoid main (String [ ] args) { String str1 = new String (“1. \nThe trees are in their autumn beauty.\n”); String str2 = new String(“The woodland paths are dry.\n….”); String str = str1.concat(str2); int numLets = 0, numOthers = 0; int numChar = str.length( ); char next; for (int inx = 0; inx < numChar; inx++) { next = str.charAt(inx); if (Character.isLetter(next) numLets++; else numOthers++; } System.out.println(“Number of char: ”+numChar+ “Letters = ”+numLets); } }

  5. The StringBuffer class The StringBuffer class should be used whenever a string object needs to be edited and and undergo more than very infrequent modification. StringBuffers have the following properties: The length of a StringBuffer object can be changed to accommodate changes that increase the length of a string. 2.The contents of a StringBuffer may be modified StringBuffer objects automatically resize when additional storage space is required. Primitive types can be appended to a StringBuffer without the programmer having to perform conversions 5. Characters can be easily added to and deleted from a StringBuffer

  6. Constructing StringBuffer Objects EmptyStringBuffer objects have an initial capacity of 16 characters StringBuffer( ) Or may have an initial capacity specified by the programmer StringBuffer(int length) StringBuffer objects can be constructed from a String StringBuffer(String str) And will have a capacity of the length of the string + space for 16 additional characters

  7. Common StringBuffer class Methods A StringBuffer has methods for: Appending primitive types and objects to the end of the contents of a StringBuffer StringBuffer append (typeKind paramName) Where typeKind may be any of the following: boolean, char, char[ ], int, long, double, float, String, Object, and a subarray with parameters char[ ], int offset, int length Determining the capacity of the StringBuffer and the length of its contents int capacity( ) int length( )

  8. Common StringBuffer class Methods (cont.) Manipulating and replacing characters and substrings char charAt(int index) void setCharAt(int index, char newCh) StringBuffer replace(int start, int end, String str) StringBuffer reverse( ) StringBuffer delete(int startPos, int endPos) void getChars(int srcBegin, int srcEnd, char[ ] dest, int destBegin) Inserting into a StringBuffer at an arbitrary location StringBuffer insert (int offset, type paramName) StringBuffer insert(int index, char[ ] str, int offset, int length) Where type designates any of the primitives or objects listed with append String toString( ) //extract a String from a StringBuffer

  9. Examples The following code fragments illustrate the use of StringBuffer methods Testing for a palindrome String str = new String (“radar”); StringBuffer sb = new StringBuffer(str); StringBuffer sb2 = sb.reverse( ); String str2 = sb2.toString( ); if (str.equals(str2)) System.out.println(str + “ is a palindrome); else System.out.println(str + “ is not a palindrome);

  10. Examples Change program to Propaganda String str = “program”; StringBuffer sb = new StringBuffer(str); char ch = sb.charAt(0); sb.setCharAt(0,Character.toUpper(ch)); sb.delete(3,7); sb.append(“paganda”); //or use replace(3,7,”paganda”) instead of //the delete / append pair System.out.println(str + “ becomes “ + sb);

More Related