Skip to content

Commit

Permalink
add StringUtils.split() function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZQ001010 committed Jul 7, 2021
1 parent e3e2896 commit 2566f85
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,25 @@ public static String deleteWhitespace(String str) {
public static boolean equalsIgnoreCase(String str1, String str2) {
return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
}

/**
* Splits the provided text into an array with a maximum length,
* separators specified.
* If separatorChars is empty, divide by blank.
* @param str the String to parse, may be null
* @return an array of parsed Strings
*/
@SuppressWarnings("checkstyle:WhitespaceAround")
public static String[] split(final String str, String separatorChars) {
if (str == null) {
return null;
}
if (str.length() == 0) {
return new String[0];
}
if (separatorChars == null){
separatorChars = " +";
}
return str.split(separatorChars);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,13 @@ public void testEqualsIgnoreCase() {
Assert.assertTrue(StringUtils.equalsIgnoreCase("abc", "abc"));
Assert.assertTrue(StringUtils.equalsIgnoreCase("abc", "ABC"));
}

@Test
public void testSplit() {
Assert.assertNull(StringUtils.split(null, ","));
Assert.assertArrayEquals(new String[0], StringUtils.split("", ","));
Assert.assertArrayEquals(new String[]{"ab", "cd", "ef"}, StringUtils.split("ab cd ef", null));
Assert.assertArrayEquals(new String[]{"ab", "cd", "ef"}, StringUtils.split("ab cd ef", null));
Assert.assertArrayEquals(new String[]{"ab", "cd", "ef"}, StringUtils.split("ab:cd:ef", ":"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public ServiceInfo listInstance(String namespaceId, String serviceName, Subscrib
checkIfDisabled(service);

List<com.alibaba.nacos.naming.core.Instance> srvedIps = service
.srvIPs(Arrays.asList(cluster.split(",")));
.srvIPs(Arrays.asList(StringUtils.split(cluster, StringUtils.COMMA)));

// filter ips using selector:
if (service.getSelector() != null && StringUtils.isNotBlank(clientIP)) {
Expand Down

0 comments on commit 2566f85

Please sign in to comment.