Optimal way to obtain substrings?
Is there a more optimal way to obtain subtrings which are separated by ',' and ende开发者_JAVA技巧d by '.' than this way?
if (paramsText != null)
{
while (paramsText.contains(","))
{
params.add(paramsText.substring(0, paramsText.indexOf(",")));
paramsText = paramsText.substring(
paramsText.indexOf(",") + 1, paramsText.length());
}
params.add(paramsText);
}
Since you aren't actually checking for a "." you can do this:
String[] params = paramsText.split(",");
Perhaps something like this would suit you:
String str = "There is, a more optimal, way to obtain. subtrings which,"
+ "are separated by, and ended by. than this way.";
String[] substrings = str.split("\\s*[.,]\\s*");
for (String substr : substrings)
System.out.println("\"" + substr + "\"");
Output:
"There is"
"a more optimal"
"way to obtain"
"subtrings which"
"are separated by"
"and ended by"
"than this way"
精彩评论