Modifying a String
String objects are immutable, whenever you want to modify a String, you must either copy it into a StringBuffer or StringBuilder, or use a String Method that constructs a new copy of the string with your modifications complete. A sampling of these methods are described here.
Substring() Method You can extract a substring using substring(). It has two forms. The first is
Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string .
The second form of substring() allows you to specify both the beginning and ending index of the substring:
Here, startIndex specifies the beginning index, and endIndex specifies the stopping point. the string returned contains all characters from the beginning index, up to, but not including, the ending index.
The following program use substring() to replace all instances of one substring with another within a string:
The Output from this program is shown here.
This is a test. this is, too.
Thwas is a test. this is, too.
Thwas was a test. this is, too.
Thwas was a test. thwas is, too.
Thwas was a test. thwas was, too.
concat() Method:You can concatenate two string using concat() method, shown here:
This method creates a new object that contains the invoking string with the contents of str appended to the concat() performs the same function as +. for Example
puts the string "onetwo" into s2. It generates the same result as the following sequence:
replace() Method: The replace() method has two forms. The first replaces all occurrences of one character in the invoking string with another character. It has the following general form:
Here, originalch specifies the character to be replaced by the character specified by replacement. The resulting string is returned. for example,
puts the String "Hewwo" into s.
The second form of replace() replaces one character sequence with another. It has this general form:
trim() Method:The trim() Method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. It has this general form:
Here is an example:
This puts the string "hello world" into s.
The trim() method is quite useful when you process user commands. for example, the following program prompts the user for the name of a state and the display that state's capital. It user trim() to remove any leading or trailing whitespace that may have inadvertently been entered by the user.
Substring() Method You can extract a substring using substring(). It has two forms. The first is
Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the invoking string .
The second form of substring() allows you to specify both the beginning and ending index of the substring:
Here, startIndex specifies the beginning index, and endIndex specifies the stopping point. the string returned contains all characters from the beginning index, up to, but not including, the ending index.
The following program use substring() to replace all instances of one substring with another within a string:
The Output from this program is shown here.
This is a test. this is, too.
Thwas is a test. this is, too.
Thwas was a test. this is, too.
Thwas was a test. thwas is, too.
Thwas was a test. thwas was, too.
concat() Method:You can concatenate two string using concat() method, shown here:
This method creates a new object that contains the invoking string with the contents of str appended to the concat() performs the same function as +. for Example
puts the string "onetwo" into s2. It generates the same result as the following sequence:
replace() Method: The replace() method has two forms. The first replaces all occurrences of one character in the invoking string with another character. It has the following general form:
Here, originalch specifies the character to be replaced by the character specified by replacement. The resulting string is returned. for example,
puts the String "Hewwo" into s.
The second form of replace() replaces one character sequence with another. It has this general form:
trim() Method:The trim() Method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. It has this general form:
Here is an example:
This puts the string "hello world" into s.
The trim() method is quite useful when you process user commands. for example, the following program prompts the user for the name of a state and the display that state's capital. It user trim() to remove any leading or trailing whitespace that may have inadvertently been entered by the user.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment
Please Write a Message for Programming or something Related issues.