startsWith(x)
: 前綴判斷endsWith(x)
: 後綴判斷contains(x)
: 包含判斷equalsIgnoreCase(x)
: 忽略大小寫的判斷相等assertTrue("stay hungry, stay foolish".startsWith("stay")); assertTrue("stay hungry, stay foolish".endsWith("foolish")); assertTrue("stay hungry, stay foolish".contains("hungry")); assertTrue("hello world".equalsIgnoreCase("Hello world"));
assertEquals("01234567890123456789".indexOf('6'), 6); assertEquals("01234567890123456789".lastIndexOf('6'), 16); assertEquals("01234567890123456789".indexOf("678"), 6); assertEquals("01234567890123456789".lastIndexOf("678"), 16); assertEquals("01234567890123456789".indexOf("abcd"), -1); assertEquals("01234567890123456789".charAt(6), '6');
substring
: 子字符串trim
: 去掉先後空白split
: 字符串分割成字符串數組join
: 字符串合併assertEquals("hello" + " " + "java", "hello java"); assertEquals("HELLO".toLowerCase(), "hello"); assertEquals("world".toUpperCase(), "WORLD"); assertEquals(" hello world ".trim(), "hello world"); assertEquals("0123456789".substring(4), "456789"); assertEquals("0123456789".substring(3, 6), "345"); assertEquals("stay hungry, stay foolish".replace("stay", "keep"), "keep hungry, keep foolish"); assertArrayEquals("java golang swift".split(" "), new String[]{"java", "golang", "swift"}); assertEquals(String.join("|", new String[]{"java", "golang", "swift"}), "java|golang|swift");
assertEquals(Integer.parseInt("123456"), 123456); assertEquals(Double.parseDouble("123.456"), 123.456, 0.00001); assertEquals(Integer.toString(123456), "123456"); assertEquals(Integer.toHexString(123456), "1e240"); assertEquals(Double.toString(123.456), "123.456");
java 的 String
是不可變的,能夠經過 StringBuilder
(多線程場景下可使用 StringBuffer
) 來構造一個字符串java
StringBuilder sb = new StringBuilder(); sb.append("hello"); sb.append(" "); sb.append("world"); sb.append(" "); sb.append(123); assertEquals(sb.toString(), "hello world 123");