Saturday 3 August 2013

30:Strings, String Buffer, and its Functions

Learning Objectives
After completing this session, you will be able to:
‰  Identify the String class and its methods
The String Class
Definition:
‰  Represents combinations of character literals
‰  Using Java, strings can be represented using:
oArray of characters
oThe String class
‰  Note:A String object is different from an array of characters
‰  There are totally 12 constructors available in the String class in Java 2, v5.0.
The String Class: Constructors
1 class StringConstructorsDemo {
2 public static void main(String args[]) {
3 String s1 = new String(); //empty string
4 char chars[] = { 'h', 'e', 'l', 'l', 'o'};
5 String s2 = new String(chars); //s2="hello";
6 byte bytes[] = { 'w', 'o', 'r', 'l', 'd' };
7 String s3 = new String(bytes); //s3="world"
8 String s4 = new String(chars, 1, 3);
9 String s5 = new String(s2);
10 String s6 = s2;
11 System.out.println(s1);
12 System.out.println(s2);
13 System.out.println(s3);
14 System.out.println(s4);
15 System.out.println(s5);
16 System.out.println(s6);
17 }
18 }
The String Class: Methods
The methods of String class are:
‰  public char charAt (int index):Returns the character located in the
specified index.
‰  public int compareTo (String anotherString):Compares this string with
the specified parameter. Returns a negative value if this string comes lexicographically
before the outer string, zero if both of the strings have the same value and a positive
value if this string comes after the other string lexicographically.
‰  public int compareToIgnoreCase (String str):Like compareTo but
ignores the case used in this string and the specified string.
‰  public boolean equals (Object anObject): Returns true if this string has
the same sequence of characters as that of the Object specified, which should be a
String object. Otherwise if the specified parameter is not a String object or it does not
match the sequence of symbols in this string, then the method will return false.
‰  public boolean equalsIgnoreCase (String anotherString):Like equals
but ignores the case used in this string and the specified string.
‰  public void getChars (int srcBegin, int srcEnd, char[] dst, int
dstBegin):Gets the characters from this stringstarting at the srcBegin index up to
the srcEnd index and copies these charcters to the dst array starting at the dstBegin
index.
‰  public int length():Returns a length of this string.
‰  public String replace (char oldChar, char newChar): Returns the
string wherein all occurences of the oldChar in this string is replaced with newChar.
‰  public String substring (int beginIndex, int endIndex):Returns the
substring of this string starting at the specified beginIndex up to the endIndex index.
‰  public char[] toCharArray():Returns the character array equivalent of this
string.
‰  public String trim():Returns a modified copy of the string wherein the leading
and trailing white space are removed.
‰  public static String valueOf(-): Takes in a simple data type such as
boolean, integer, or character, or it takes in an object as a parameter and returns the
String equivalent of the specified parameter.
Try It Out
Problem Statement:
Write a program that illustrates the usage of String constructors.
Code:
class StringConstructors {
public static void main(String args[]) {
String s1 = new String(); // empty string
char chars[] = { 'h', 'e', 'l', 'l', 'o' };
String s2 = new String(chars); // s2="hello";
byte bytes[] = { 'w', 'o', 'r', 'l', 'd' };
String s3 = new String(bytes); // s3="world"
String s4 = new String(chars, 1, 3);
String s5 = new String(s2);
String s6 = s2;
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
}
}
Refer File Name: StringConstructors.javato obtain soft copy of the program code
How It Works:
When you run the program, it produces the following results:
hello
world
ell
hello
hello
Tips and Tricks:
List out key points on Strings.
Solution:
‰  Strings are immutable.
‰  They can be created from a literal, a byte array, a char array, or a string buffer.
‰  A String created by a new operator is alwaysa different new object, even if it is
created from a literal.
‰  All string operations (concat, trim, replace,substring, and so on) construct and return
new strings.
‰  toUpperCase and toLowerCase will return the same string if no case conversion was
needed.
‰  Passing null to indexOf or lastIndexOf will throw NullPointerrException, passing empty
string returns zero, and passing a string that is not in the target string returns -1.
‰  trim method removes all leading and trailing white-space from a String and returns a
new String. White spaces means, all characterswith values less than or equal to the
space character – ‘\u0020’.
‰  String class is final.
‰  = and += operators are overloaded for Strings.
‰  reverse, append, and insert are not String methods.
Summary
‰  String objects are immutable, and String reference variables are not.
‰  If you create a new String without assigning it, it will be lost to your program.
‰  If you redirect a String reference to a new String, the old String can be lost.
‰  String methods use zero-based indexes, except for the second argument of
substring().
‰  The String class is final – its methods can’t be overridden.

No comments:

Post a Comment