简体   繁体   中英

Separate a single string into two

I am having a String like test,test1 . I need to take out these two string separately. How will I read test and test1 without comma?

You can use String#split() :

String[] parts = yourString.split(",");

parts[0] will then contain "test" and parts[1] will contain "test1" .

You can use split function OR StringTokenizer function defined in java.lang.String class.

Example 1 :

 String s = "test1,test2";
 String[] strArr = s.split(",");

The above array will be having two elements with values test1 and test2.

Example 2 :

StringTokenizer token = new StringTokenizer("test1,test2",",");

        while(token.hasMoreElements()){
            System.out.println(token.nextElement());
        }

Regards, Gunjan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM