Go Back   TechArena Community > ARENA > Guides & Tutorials
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 12-03-2010
Member
 
Join Date: Feb 2010
Posts: 136
The String Class in Java

String Class in Java


1. Introduction:

Strings are often used in programming. A string is a sequence or a sequence of characters. In many languages a string is an array of other characters, whereas in Java a string is a object. This is the main difference between Java and other programming languages regarding the Strings.

Java offers 3 main classes related to strings:

- The class String (string not editable)
- The class StringBuffer (string modified as desired)
- The class StringTokenizer (separating a string into several entities)

In most cases it is necessary to use string to create, store and manage strings. The StringBuffer class provides greater flexibility and is used in the same manner as the String class.

2. The String Class:
This class has 11 manufacturers and over 40 methods to examine the character of a string, compare, search, retrieve, etc.. Among these methods, some are described below. You can find other by experimenting with the language.

2.a. Construction of the String:
A string is usually declared as follows:
Code:
	String name = new String("Some Name");
As the strings are a data type widely used, Java allows a more concise statement:
Code:
	String name = "Some Name";
2.b. Comparing strings
It is tempting to compare two strings using the == operator as in:
Code:
	if (str1 == str2)
However, this comparison, although is correct, does not compare whether two strings are equal, but if str1 and str2 point to the same object.

A string comparison is done as follows:
Code:
	if (str1.equals(str2))
There is also the method compareTo :
Code:
	str1.compareTo(str2);
This method returns 0 if both strings are equal, a negative value if str1 is smaller than str2 or a positive value if str2 is smaller than str1.
Note: Do not use the operators >,> =, <, <=
Reply With Quote
  #2  
Old 12-03-2010
Member
 
Join Date: Feb 2010
Posts: 136
Re: The String Class in Java

2.c. Concatenation:
You can use the method concat to concatenate two strings
Code:
	String s = s1.concat(s2);
It is also possible to use a plus sign + to perform a concatenation between two strings
Code:
	String wd = message + " and " + "this is it";
2.d. Extraction of characters:
An object of the String class is immutable. Once it has been defined, its value can not be changed. However, we can assign a new string. If the String is defined as follows:
Code:
	String message  = "Warning!";
It may well change its content:
Code:
	message = "new text";
The string "Warning!" is forgotten and the object message points to the new channel "new text". We can extract a substring using the method substring the String class. There are two versions in it
Code:
	Public String substring(int beg, int end)
Which returns a new string starting at character position beg and goes up to character position end -1.
For example
Code:
message.substring txt = (0, 6); 
returns the string "new".
And the second one
Code:
	Public String substring(int start)
Which returns a new string starting at character position beginning and ending at the end of the initial string.
For example
Code:
txt = "first" + "new string". substring (8); 
returns the string "first case".
2.e. Length of a string:
The length of a string is obtained using the method length ().
For example
Code:
ex.length () returns 11.
If you have a chain str. The method str.charAt (index) returns the specified character chain str. The parameter Index may have a value between 0 and str.length () - 1.

2.f. Miscellaneous Conversions:
Although the contents of a string can not be changed, it is possible to perform conversions by creating a new string. Methods toLowerCase and toUpperCase enable a channel respectively lowercase and uppercase. The method trim yields a new string without spaces at the beginning or end. The method replace (oldChar, newChar) can replace all the characters oldChar a chain of characters newChar.

2.g. Conversion of character and numeric values to strings:
The String class has several methods function valueOf() { [native code] } to convert a character, an array of characters and numeric values to strings. These methods have the same name, but differ in the type parameter supplied to them (char, char [], double, long, int, float, etc..)
Reply With Quote
  #3  
Old 12-03-2010
Member
 
Join Date: Feb 2010
Posts: 136
Re: The String Class in Java

3. The class StringBuffer:
Generally a StringBuffer can be used anywhere a string. It is simply more flexible: you can modify its contents. This class has 3 constructors and over 30 methods, the most used are as follows. Please note that the parameters are not shown here. If you need more information on the paramenters then you can visit the official site of Sun for the same.

- append
- capacity
- charAt
- delete
- insert
- length
- replace
- reverse
- setCharAt
- setLength
- substring


3.a. Construct a StringBuffer:
There are three ways to construct a StringBuffer are:
Code:
	Public StringBuffer()
this constructs an empty string with an initial capacity of 16 characters.
Code:
	Public StringBuffer(int length)
this built a chain of empty capacity indicated by length.
Code:
	Public StringBuffer(String str)
this built a chain receiving the parameter str. The chain capacity is 16 plus the length of str.

3.b. A simple example:
This method append cache to add expressions of type char, char [], double, float, int, long, and String. Example of using the construction method append
Code:
StringBuffer sb = new StringBuffer();
sb.append("This");
sb.append("is");
sb.append("java");
sb.append("for you");
The same goes for the method(s) insert.
Here's an example for it
Code:
	st.insert(9, "HTML and ");
A StringBuffer is changing dynamically. If you increase the length of its contents, the space allocated memory is automatically increased.

3.c. Capacity and Length:
The method capacity returns the capacity, i.e the number of characters a StringBuffer can receive. The method length returns the number of characters actually placed into the StringBuffer. The method setLength is used to specify the length of a StringBuffer. If the parameter value is less than the number of characters in the StringBuffer, it is truncated. If the value of the parameter exceeds the number of characters in the StringBuffer, its content is supplemented with null characters.
Reply With Quote
  #4  
Old 12-03-2010
Member
 
Join Date: Feb 2010
Posts: 136
Re: The String Class in Java

4. The class StringTokenizer:
This class is used to separate a string into several pieces. Firstly, it is possible to specify delimiters while constructing a StringTokenizer. Below mentioned are its constructors.
Code:
	Public StringTokenizer(String s, String delim, boolean tokens)
This built a StringTokenizer for string s with the separators indicated by delim. If token is true, the separator is returned as a piece of string.
Code:
	Public StringTokenizer(String s, String delim)
This built a StringTokenizer for string s with the separators indicated by delim. The separator is not considered as a piece of string.
Code:
	Public StringTokenizer(String s)
This built a StringTokenizer for string s with the default separator (space, newline, tab and return) and the separator is returned as a piece of string.

Here is an example showing how to extract the words in a sentence or sequence of characters.
Code:
	String s = "Modern language = Java";
StringTokenizer strtok = new StringTokenizer(s);
System.out.System.out.println("No. of words:" + strtok.countTokens());
	while (strtok.hasMoreTokens())
System.out.System.out.println(strtok.nextToken());
Reply With Quote
Reply

  TechArena Community > ARENA > Guides & Tutorials
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "The String Class in Java"
Thread Thread Starter Forum Replies Last Post
Definition of a String in Java? cloud101 Software Development 5 28-07-2014 10:14 AM
Converting java class to java bean class Ucchal Software Development 6 09-08-2010 10:24 AM
Java String test Aidan 12 Software Development 3 09-11-2009 02:22 PM
Difference between capacity and size functions of string class DimitrisLiatsos Software Development 5 25-10-2008 05:07 PM
Java Programming Language Basics: Reflection Basics and Class Class mayuri_gunjan Guides & Tutorials 6 29-08-2005 12:04 AM


All times are GMT +5.5. The time now is 10:38 AM.