Responsive Ads Here

Thursday, June 4, 2015

String Processing



String Class


Definition:

Represents combinations of character literals
Using Java, strings can be represented using:
        ● Array of characters
        ● The String class
                 – Note: A String object is different from an array of characters!
        ● String constructors
                 – 11 constructors

The StringBuffer Class

Problem with String objects:
       – Once created, can no longer be modified (It is a final class)
A StringBuffer object
       – Similar to a String object
       – But, mutable or can be modified
 ● Unlike String in this aspect
 ● Length and content may changed through some method calls


String Searching

There are several overloaded versions of the method indexOf() available in the String class for searching a string forward, the lastIndexOf() method for searching a string backward, and the regionMatches() method for comparing a “region” of text within a string.

The IndexOf() Method

SearchString.java
package com.appfworks.string;

/**
* Created by suresh on 29/10/15.
*/
// Demonstrates searching a character in a string
public class SearchString {
public static void main(String[] s) {
String str = "OCPJP";
// The indexOf() method searches the specified character— here it is, the character J— and
// returns the first occurrence of the character (note that the index starts from 0, not 1!).
System.out.println("Character J occurs at index: " + str.indexOf('J'));
String str2 = "I am preparing for OCPJP";
// The indexOf() method is overloaded, and one definition of the method takes a string as a
// search argument.
System.out.println("Substring \"for\" occurs at index: " + str2.indexOf("for"));
String str3 = "I am a student. I am preparing for OCPJP";
int fromIndex = 0;
while(str3.indexOf("am", fromIndex) > -1) {
fromIndex = str3.indexOf("am", fromIndex);
System.out.println("Substring \"am\" occurs at index: " + fromIndex);
fromIndex++;
}
}
}

The regionMatches() Method

MatchRegionInString.java
package com.appfworks.string;

/**
* Created by suresh on 29/10/15.
*/
// This example demonstrates how to search a "region" of text within a string
public class MatchRegionInString {
public static void main(String[] s) {
String chat = "Tarzan: Hi Jane, wanna ride an Elephant? \n Jane: No thanks! I'm preparing for OCPJP now!";
String matchString = " Jane: No thanks!";
// first get the index of the position from which the search region starts
int startIndex = chat.indexOf('\n');
System.out.println("Jane's response starts at the index: " + startIndex);
// if '\n' found, then try matching for the string " Jane: No thanks!" from there
if(startIndex > -1) {
// remember that the index starts from 0 and not 1, so add 1 to startIndex
boolean doesMatch = chat.regionMatches(startIndex + 1, matchString, 0,
matchString.length());
if(doesMatch)
System.out.println("Jane's response matches with the string" +
matchString);
}
}
}

String Conversions

StringConvertions.java
package com.appfworks.string;

/**
* Created by suresh on 29/10/15.
*/
public class StringConvertion {
public static void main(String[] args){
String str1 = String.valueOf(10); // right way to convert from an integer to String

// String str2 = 10; // compiler error—cannot convert from int to String
// String str3 = (String) 10; // compiler error—cannot convert from int to String

// int i = "10"; // compiler error—cannot convert from String to int
// int ii = (int) "10"; // compiler error—cannot convert from String to int

int i1 = Integer.parseInt("10"); // right way to convert from a String to an int

float f = Float.parseFloat("no such value");
}
}

The Split() Method

ParseString.java

package com.appfworks.string;

/**
* Created by suresh on 29/10/15.
*/
// this example demonstrates the usage of split() method
public class ParseString {
public static void main(String[] s) {
String quote = "Never lend books-nobody ever returns them!";
String [] words = quote.split(" "); // split strings based on the delimiter " "(space)
for (String word : words) {
System.out.println(word);
}

String str = "c:\\work\\programs\\parser";
String [] dirList = str.split("\\\\");
for (int i=0; i<dirList.length; i++) {
System.out.println(dirList[i]);
}
}
}


The Wrapper Classes

Some Facts:
      – Primitive data types are not objects
               ● Cannot access methods of the Object class
      – Only actual objects can access methods of the Object class
      – Why wrapper classes?
               ● Need an object representation for the primitive type variables to use Java built-in methods

Definition:
      – Object representations of simple non-object variables

No comments:

Post a Comment