I would recommend that you make these emails separated by something essential that they cannot enter the name of the mail, for example, two asterisks ** or two dollars $$ , and then it’s banal to split lines into this separator via .split("\\*\\*") and fold it into a sheet. There are fewer hemorrhoids on your head and you don’t have to turn the brain in regular intervals and other nonsense.
The split method returns a new array. The string is beaten by the delimiter specified by the first argument.
Example
String Str = new String("Youremail@gmail.com$$Anotheremail@gmail.com"); List<String> emailList = new ArrayList<String>(Arrays.asList(Str.split("\\$\\$"))); System.out.println("Emails:" ); for (String email: emailList){ System.out.println(email); }
will output:
Emails: Youremail@gmail.com Anotheremail@gmail.com
Here is an option in case it is known that all mailboxes will be on a specific domain. So we will know that they will all end on @somedomain2level.domain1level
Accordingly, you can set this parameter to search in the string, and in the loop by this parameter find each mail by moving the cursor to the next position to search
String string = "Youremail@gmail.comAnotheremail@gmail.comSomeemail@gmail.com"; int prevPos = 0, nextPos; List<String> emailList = new ArrayList<>(); final String SEARCH_STR = "@gmail.com"; final int SEARCH_STR_LENGTH = SEARCH_STR.length(); /**** start code *****/ while (true) { nextPos = string.indexOf(SEARCH_STR, prevPos); if (nextPos == -1) break; emailList.add(string.substring(prevPos, nextPos + SEARCH_STR_LENGTH)); prevPos = nextPos + SEARCH_STR_LENGTH; } /**** end code *****/ for (String test : emailList) { System.out.println(test); }
https://ideone.com/OkRYll