Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Splitting Strings or InputVariables

Status
Not open for further replies.

dnguyen

Programmer
Apr 16, 2001
11
AU
Hi All,

Is it possible to "GRAB" certain, parts of a string, ie. break a string into two parts.

EXAMPLE. input variable, PIN, which has a length of 10 chars. I would like to break this variable up, into two parts.

* Part 1, holds the first 9 chars
* Part 2, the last char

Thanks very much in advance.

Cheers,
David.
 
The Comment:
It would be in your best interest to get familar with Javadocs because these types of answers are easily found.

The Answer:
What you want is substring. Here is except from the Javadocs for one version of substring (it is an overloaded method):
Code:
substring

public String substring(int beginIndex,
                        int endIndex)


Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Examples:


 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"
 
Parameters:

beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.

Returns:

the specified substring.

Throws:

IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
Wushutwist
 
Thanks Wushutwist, I realised that after I posted the thread.

Thanks for your assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top