Search code examples
androidstringsplit

How to Split a string by Comma And Received them Inside Different EditText in Android?


I have Two EditText(id=et_tnum,et_pass). I Received a String like 12345,mari@123 inside EditText1(et_tnum) . I want to Split them by Comma and After Comma i should Receive Remainder string into EditText2(et_pass). Here 12345,mari@123 is Account Number & Password Respectively.


Solution

  • String CurrentString = "12345,mari@123";
    String[] separated = CurrentString.split(",");
    //If this Doesn't work please try as below
    //String[] separated = CurrentString.split("\\,");
    separated[0]; // this will contain "12345"
    separated[1]; // this will contain "mari@123"
    

    Also look at this post:

    There are other ways to do it. For instance, you can use the StringTokenizer class (from java.util):

    StringTokenizer tokens = new StringTokenizer(CurrentString, ",");
    String first = tokens.nextToken();// this will contain "12345"
    String second = tokens.nextToken();// this will contain "mari@123"
    // in the case above I assumed the string has always that syntax (foo: bar)
    // but you may want to check if there are tokens or not using the hasMoreTokens method