java 字符串

java 字符串

斷言

  • 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");

連接

相關文章
相關標籤/搜索