java 字符串

2020-01-01  本文已影响0人  hatlonely

java 字符串

断言

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');

字符串操作

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) 来构造一个字符串

StringBuilder sb = new StringBuilder();
sb.append("hello");
sb.append(" ");
sb.append("world");
sb.append(" ");
sb.append(123);
assertEquals(sb.toString(), "hello world 123");

链接

上一篇下一篇

猜你喜欢

热点阅读