……

比起字符数组,Java 的 string 更像一个字符指针

Concatenation

  • 就是一个感觉像是 String Class 中的一个重载,方便了连接字符串与字符串、字符串与其他对象

  • String.format() behave like sprintf in C.

String all = String.join(" / ", "S", "M", "L", "XL");
// all is the string "S / M / L / XL"
 
String repeated = "Java".repeat(3);  // repeated is "JavaJavaJava"

Strings Are Immutable

string 不能改变 (该类没有提供),所以要通过这个 Concatenation 来做到所谓修改

string greeting = "Hello"
greeting = greeting.substring(0, 3) + "p!";  // "Help!"

我们不能直接修改这个字符串,但是我们可以让它完全容纳一个新的字符串。就像让一个变量从容纳3到4,我们修改的不是3这个数字,而是容纳的东西

Testing Equality

s.equals(t);
"Hello".equals(greeting);
"Hello".equalsIgnoreCase("hello");  // 忽视大小写
 
if (greeting.compareTo("Hello") == 0)  // 可以是可以,但何必这么做呢?

不要使用 == 来测试,结合前面说的,这只是在测试两个字符串是否被储存在了同一个地方 (诚然,真的在同一个地方自然是相等的,只是这和初心违背)

Empty and Null Strings

没错,字符串有空的和空指针,可以通过判断方法来感受出区别

if (str.length() == 0) {} if (str.equals("")) {}
if (str == null) {}  // 可以直接判断,什么成分不用我说了吧
if (str != null && str.length() != 0) {} // 要先判断是否空指针,因为对空指针调用方法是不合法的

Code Points and Code Units

  • Code Units. In Java, the char type describes a code unit in the UTF-16 encoding. The supplemetary characters may require several code units.
  • Code Points. Is a code value that is associated with a character in an encoding scheme. the Unicode
String greeting = "你好👋";
// the length of code units & true length
int cuCount = greeting.length();  // is 4  
int cpCount = greeting.codePointCount(0, greeting.length());  // is 3
// get the code unit at argu position
char first = greeting.charAt(0);  // first is '你'
char second = greeting.charAt(1);  // second is '好'
// get the code point
int index = greeting.offsetByCodePoints(0, 2);  // this statement is for counting by code point
int cp = greeting.codePointAt(index);  // the index is base on code unit

When Traverses a String

可以

int cp = sentence.codePointAt(i);
if (Character.isSupplementaryCodePoint(cp)) i += 2; 
else i++;
 
i--;
if (Character.isSurrogate(sentence.charAt(i))) i--;
int cp = sentence.codePointAt(i);

但明显感觉 quite painful,不如:

int[] codePoints = str.codePoints().toArray();
String str = new String(codePoints, 0, codePoints.length-0);

转成数组处理更方便,因为每一个元素都是一个 code point

前面的 methods 中,codePointAt 虽然可以返回一个 code point,但是 index 还是基于 code unit 的,所以 此路不通

StringBuilder Class

Java string is immuable, so if we do need to build up strings from small pieces, use StringBuilder.

StringBuilder builder = new StringBuilder();  // build empty
builder.append(ch);  // appends a single character 
builder.append(str); // appends a string
String completedString = builder.toString();  // to String

以前有 StringBuffer Class,但是相比这个效率低了。但是这个可以多线程,标题单线程,API 都是一样的