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);
Here are some more examples of 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)
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.
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);
Here are some more examples of 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.
No comments:
Post a Comment