Link to home
Create AccountLog in
Avatar of cookiejar
cookiejarFlag for United States of America

asked on

parse a comma delimited string and store into variables

How would I write code to parse a comma delimited string and store in  two different variables?
The string will contain two items.  
For example, input_parameter may contain 'Egate','Dell'

public void parse (String input_parameter)
}
  parse input_parameter
  store first item of input_parameter  in var1;
  store second item of input_parameter in var2;


}
Avatar of mccarl
mccarl
Flag of Australia image

Check out the Javadoc for the String.split() method. Or for an alternative approach, use the String.indexOf() and String.subString() methods.
splits String to array, that could have many items, in your case two.
if theSrting = "item1,item2,item3,item4" , then array will have size 4.

==============================================================

public class TestSplit {

    public String[] parse(String theString) {
        return theString.split(",");
    }

    public static void main(String[] args) {

        TestSplit ts = new TestSplit();

        String v1, v2;
        String [] arrStr = ts.parse("Egate,Dell");
        v1 = arrStr[0]; v2 = arrStr[1];
        System.out.println("v1 = " + v1 + " v2 = " + v2);
    }
}
ASKER CERTIFIED SOLUTION
Avatar of Tamil Arasi
Tamil Arasi
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer