Search code examples
javasplitpasswords

Splitting String which can contains delimeter


Now I have:

for (String userPass : splitted) {
        String user = userPass.split("=")[0];
        String pass = userPass.split("=")[1];
        config.put(user, pass);
    }

which works for file which contains e.g

service1.password=dsjahdsahjk!sdafds

but as the second part is password it can also be:

service1.password=das-=asdwe=12f=

then my idea will fail miserably. What is the best approach to ensure that I am splitting by = but not this one which is inside password?


Solution

  • you can just try the following and add an additional parameter limit to your split method:

    String pass = userPass.split("=", 2)[1];