Character Extraction in java
The String class provides a number of ways in which characters can be extracted from a String object. Several are examined here. Although the characters that comprise a String within a String object can not be indexed as if they were a character array,Many of the String methods employ an index into the String for there operation. Like the Arrays the String indexes begin at zero.
charAt() Methid:To Extract a Single character from a String,you can refer directly to an individual character via the charAt() method. It has this general form
Here, where is the index of the character that you want to obtain. The value of where must be nonnegative and specify a location within the String. charAt() returns the character at the specified location For Example:
getChars() Method: If you need to Extract more then one character at a ttime you can use the getChars() method. It has this General form:
Here, sourceStart specifies the index of the beginning of the substring, and sourceEnd specifies an index that is one part the end of the desired substring. Thus, the substring character is specified by target. the index within target at which the substring will be copied is passed in targetStart. Care must be taken to assure that the target array is large enough to hold the number of character in the specified substring.
The Following program demonstrates getChars().
Here is the output of the program:
Demo
getBytes() Method: There is an alternative to getChars() that stores the characters in an array of bytes. this method is called getBytes(), and it used the default character-to-byte conversions provided by the platform. Here is its simplest form:
Other Forms of getBytes() are also available. getBytes() is most useful when you are exporting a String value into an environment that does not support 16-bit Unicode characters. for example, most Internet protocols and text file formats use 8-bit ASCII for all text interchange.
toCharArray() method: if you want to convert all the characters in a String object into a character array, the easiest way is to call toCharArray(). It returns an array of characters for the entire String. It has this general form:
This function is provided as a convenience, since it is possible to use getChars() to achiev the same result.
charAt() Methid:To Extract a Single character from a String,you can refer directly to an individual character via the charAt() method. It has this general form
Here, where is the index of the character that you want to obtain. The value of where must be nonnegative and specify a location within the String. charAt() returns the character at the specified location For Example:
getChars() Method: If you need to Extract more then one character at a ttime you can use the getChars() method. It has this General form:
Here, sourceStart specifies the index of the beginning of the substring, and sourceEnd specifies an index that is one part the end of the desired substring. Thus, the substring character is specified by target. the index within target at which the substring will be copied is passed in targetStart. Care must be taken to assure that the target array is large enough to hold the number of character in the specified substring.
The Following program demonstrates getChars().
Here is the output of the program:
Demo
getBytes() Method: There is an alternative to getChars() that stores the characters in an array of bytes. this method is called getBytes(), and it used the default character-to-byte conversions provided by the platform. Here is its simplest form:
Other Forms of getBytes() are also available. getBytes() is most useful when you are exporting a String value into an environment that does not support 16-bit Unicode characters. for example, most Internet protocols and text file formats use 8-bit ASCII for all text interchange.
toCharArray() method: if you want to convert all the characters in a String object into a character array, the easiest way is to call toCharArray(). It returns an array of characters for the entire String. It has this general form:
This function is provided as a convenience, since it is possible to use getChars() to achiev the same result.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment
Please Write a Message for Programming or something Related issues.