зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1299201 - StringUtils: Add helper method for joining strings with a separator. r=Grisha
MozReview-Commit-ID: Fzrap4wkeWk --HG-- extra : rebase_source : c0d6495d825ad0a8a80d05a39e38d841d758e115
This commit is contained in:
Родитель
e495b04c9e
Коммит
d6301cd000
|
@ -13,6 +13,7 @@ import org.mozilla.gecko.AppConstants.Versions;
|
|||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class StringUtils {
|
||||
|
@ -271,4 +272,23 @@ public class StringUtils {
|
|||
|
||||
return "\u200E" + text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Joining together a sequence of strings with a separator.
|
||||
*/
|
||||
public static String join(@NonNull String separator, @NonNull List<String> parts) {
|
||||
if (parts.size() == 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append(parts.get(0));
|
||||
|
||||
for (int i = 1; i < parts.size(); i++) {
|
||||
builder.append(separator);
|
||||
builder.append(parts.get(i));
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,9 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
import org.mozilla.gecko.background.testhelpers.TestRunner;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
@ -84,4 +87,21 @@ public class TestStringUtils {
|
|||
final String forcedAgainLtrString = StringUtils.forceLTR(forcedLtrString);
|
||||
assertEquals(5, forcedAgainLtrString.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoin() {
|
||||
assertEquals("", StringUtils.join("", Collections.<String>emptyList()));
|
||||
assertEquals("", StringUtils.join("-", Collections.<String>emptyList()));
|
||||
assertEquals("", StringUtils.join("", Collections.singletonList("")));
|
||||
assertEquals("", StringUtils.join(".", Collections.singletonList("")));
|
||||
|
||||
assertEquals("192.168.0.1", StringUtils.join(".", Arrays.asList("192", "168", "0", "1")));
|
||||
assertEquals("www.mozilla.org", StringUtils.join(".", Arrays.asList("www", "mozilla", "org")));
|
||||
|
||||
assertEquals("hello", StringUtils.join("", Collections.singletonList("hello")));
|
||||
assertEquals("helloworld", StringUtils.join("", Arrays.asList("hello", "world")));
|
||||
assertEquals("hello world", StringUtils.join(" ", Arrays.asList("hello", "world")));
|
||||
|
||||
assertEquals("m::o::z::i::l::l::a", StringUtils.join("::", Arrays.asList("m", "o", "z", "i", "l", "l", "a")));
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче