|
| |||||||||
| Tags: integer, java, primtive datatype, programming language, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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"); Code: String name = "Some Name"; It is tempting to compare two strings using the == operator as in: Code: if (str1 == str2) A string comparison is done as follows: Code: if (str1.equals(str2)) Code: str1.compareTo(str2); Note: Do not use the operators >,> =, <, <= |
|
#2
| |||
| |||
| 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); Code: String wd = message + " and " + "this is it"; 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!"; Code: message = "new text"; Code: Public String substring(int beg, int end) For example Code: message.substring txt = (0, 6); returns the string "new". Code: Public String substring(int start) For example Code: txt = "first" + "new string". substring (8); returns the string "first case". The length of a string is obtained using the method length (). For example Code: ex.length () returns 11. 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..) |
|
#3
| |||
| |||
| 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() Code: Public StringBuffer(int length) Code: Public StringBuffer(String 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"); Here's an example for it Code: st.insert(9, "HTML and "); 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. |
|
#4
| |||
| |||
| 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) Code: Public StringTokenizer(String s, String delim) Code: Public StringTokenizer(String s) 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()); |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "The String Class in Java" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Converting java class to java bean class | Ucchal | Software Development | 6 | 09-08-2010 11:24 AM |
| Separating a string in java | Aaliya Seth | Software Development | 5 | 10-02-2010 04:27 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 06:07 PM |
| Java Programming Language Basics: Reflection Basics and Class Class | mayuri_gunjan | Guides & Tutorials | 6 | 29-08-2005 01:04 AM |