MY mENU


Thursday 9 February 2012

Strings in Java

String class

public final class String extends Object implements Serializable

The String class represents character strings.
All string literals in Java programs, such as "abc", are implemented as instances of this class.

Strings are constant; their values cannot be changed after they are created.

String str = "abc";
is equivalent to:

char data[] = {'a', 'b', 'c'};
String str = new String(data);

How strings can be used:

System.out.println("abc");
String cde = "cde";
System.out.println("abc" + cde);
String c = "abc".substring(2,3);
String d = cde.substring(1, 2);

The String class supports several constructors:

To create an empty String we call the default constructor
String s = new String();
Will create an instance of String with no characters in it.


Methods From String Class:

char charAt(int index)
Returns the char value at the specified index.
String concat(String)
Concatenates the String with the specified String.
String toUpperCase()
String toLowerCase()
boolean startsWith(String)
String subString(int start_index)
String subString(int start_index,int end_index)
int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
boolean endsWith(String suffix)
Tests if this string ends with the specified suffix.

boolean equals(Object anObject)
Compares this string to the specified object.

boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations

int length()
Returns the length of this string.


StringBuffer class:

public final class StringBuffer extends Object implements Serializable, CharSequence

A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.


StringBuffer defines three constructors:
1.StringBuffer();
2.StringBuffer(int size)
3.StringBuffer(String str)

  • The default constructor (the one with no parameters) reserves room for 16 characters without reallocation.
  • The second version accepts an integer that explicitly sets the size of the buffer.
  • The third version accepts a String argument that sets the initial contents of the StringBuffer object and reserves for 16 additional characters without reallocation.


NOTE

All most all the methods from String class are available in StringBuffer class.
int length()
int capacity()
char charAt(int index)
void setChatAt(int index,char ch)
StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)
StringBuffer insert(int index,String str)
StringBuffer reverse()
StringBuffer delete(int startindex,int endindex)
StringBuffer deleteCharAt(int loc)
StringBuffer replace(int start, int end, String str)
String subString(int startIndex)
String subString(int startIndex,int endIndex)


StringBuffer and StringBuilder class
=>StringBuffer is used to store character strings that will be changed(String objects cannot be changed). It automatically expands as needed.
=>StringBuilder was added in Java 5.
=>It is identical in all respects to StringBuffer except that it is not synchronized, which means that if multiple threads are accessing it at the same time, there could be trouble.
=>For single-threaded programs, the most common case, avoiding the overhead of synchronization makes the StringBuilder very slightly faster.


Comparing Strings and Portion Of Strings:



            The String Class has a number of methods for comparing strings and portions of Strings.

boolean endsWith(String suffix)/boolean startsWith(String prefix) : Returns true if this string ends with or begins with the substring specified as an argument to the method.

- boolean startsWith(String prefix, int offset) : Considers the string beginning at the index offset, and returns true if it begins with the substring specified as an argument.

- int compareTo(String anotherString) : Compares two strings lexicographically. Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument.

- int compareToIgnoreCase(String str) : Compares two strings lexicographically, ignoring differences in case. Returns an integer indicating whether this string is greater than (result is > 0), equal to (result is = 0), or less than (result is < 0) the argument.

- boolean equals(Object anObject) : Returns true if and only if the argument is a String object that represents the same sequence of characters as this object.

- boolean equalsIgnoreCase(String anotherString) : Returns true if and only if the argument is a String object that represents the same sequence of characters as this object, ignoring differences in case.

- boolean regionMatches(int toffset, String other, int ooffset, int len) : Tests whether the specified region of this string matches the specified region of the String argument. Region is of length len and begins at the index toffset for this string and ooffset for the other string.

- boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) : Tests whether the specified region of this string matches the specified region of the String argument.

Region is of length len and begins at the index toffset for this string and ooffset for the other string.

The boolean argument indicates whether case should be ignored; if true, case is ignored when comparing characters.

- boolean matches(String regex) : Tests whether this string matches the specified regular expression. Regular expressions are discussed in the lesson titled "Regular Expressions."



No comments:

Post a Comment