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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse String

Status
Not open for further replies.

timmbo

Programmer
Feb 22, 2001
167
US
Hi All,

I need to parse a string consisting of an account number and a unitID.
Below is the string I need to parse.


1004404285%28S2%29%20%5BSub%202%20Account%5D


What needs to be extracted is:
A) 1004404285
B) S2

How would I extract these two items and assign to their own variables.

TIA,
Tim
 
It depends if the input string is always going to be in this format, if so see example below:

Code:
import java.util.StringTokenizer;

public class parseString
{
	public static void main (String [] args)
	{
		String inputString = "1004404285%28S2%29%20%5BSub%202%20Account%5D";
		String accountNumber = null;
		String unitID = null;

		StringTokenizer sTokenizer = new StringTokenizer(inputString, "%");

		accountNumber = sTokenizer.nextToken();

		String tempUnitID = sTokenizer.nextToken();

		unitID = tempUnitID.substring(2,4);

		System.out.println ("Input String: \t\t" + inputString);
		System.out.println ("Account Number: \t" + accountNumber);
		System.out.println ("unitID: \t\t" + unitID);
	}
}

**** OutPut ***********

Input String: 1004404285%28S2%29%20%5BSub%202%20Account%5D
Account Number: 1004404285
unitID: S2
Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top