We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
从概念上来讲,Java字符串就是Unicode字符序列。Java没有内置的字符串类型,而是在标准库Java类库中提供了预定义类String,每个用双引号括起来的字符串都是String类的一个实力。
String e = ""; String greeting = "Hello";
String类有一个方法:substring,可以从一个字符串中提取出另一个字符串。如:
substring
String greeting = "Hello"; String s = greeting.substring(0, 3); // output: "Hel" String t = greeting.substring(1, 5); // output: "ello"
substring有两个参数beginIndex、endIndex,beginIndex是开始的位置,endIndex是要结束的位置。字符串长度就是endIndex - beginIndex,如果beginIndex是负数,或者endIndex超出了范围会抛出异常IndexOutOfBoundsException;
IndexOutOfBoundsException
和其它语言一样,字符串都是可以拼接的,Java中,拼接可以使用连接符+,如:
+
String h = "Hello"; String w = "World"; String hw = h + " " + w; System.out.println(hw); // output: "Hello World"
还有一个静态方法join方法,可以用特定的字符来连接字符串。
join
String h = "Hello"; String w = "World"; String y = "yankewei"; System.out.println(String.join("/", h, w)); // output: "Hello/World/yankewei"
什么意思呢,就是说,字符串声明之后是不可在原来的基础上进行修改。那么如果我要改变这个字符串的一部分怎么办呢?可以这样:
String h = "Hello"; h = h.substring(0,3) + "!!"; System.out.println(h); // output: "Hel!!"
像其它语言,可以使用类似数组的方式h[3] = '!'去访问字符串中的特定位置的字符,这样就可以改变字符串。Java并没有提供。
h[3] = '!'
可以使用equals方法检测两个字符串是否相等。如:
equals
h.equals(w); // output: false // 还有不区分大小写的方法 "world".equalsIgnoreCase(w); // output: true
想其它语言,可以使用"=="来进行判断,在Java中是万万不能的。在Java中,这个运算符只能够判断两个字符串是否放置在同一个位置,当然,如果是同一个位置,那么肯定相等,但是两个字符串相等,也有可能不在同一个位置,如:
String h = "Hello"; String w = "HelloWorld"; System.out.println(h.substring(0, 5)==(w.substring(0,5))); // output: false System.out.println(h.substring(0, 5).equals(w.substring(0,5))); // output: true
这里指示介绍了字符串的一些Api,还有很多常用的Api,可以查看文档, 了解更多的字符串使用方法。
The text was updated successfully, but these errors were encountered:
No branches or pull requests
从概念上来讲,Java字符串就是Unicode字符序列。Java没有内置的字符串类型,而是在标准库Java类库中提供了预定义类String,每个用双引号括起来的字符串都是String类的一个实力。
1. 子串
String类有一个方法:
substring
,可以从一个字符串中提取出另一个字符串。如:substring
有两个参数beginIndex、endIndex,beginIndex是开始的位置,endIndex是要结束的位置。字符串长度就是endIndex - beginIndex,如果beginIndex是负数,或者endIndex超出了范围会抛出异常IndexOutOfBoundsException
;2. 拼接
和其它语言一样,字符串都是可以拼接的,Java中,拼接可以使用连接符
+
,如:还有一个静态方法
join
方法,可以用特定的字符来连接字符串。3. 不可变字符串
什么意思呢,就是说,字符串声明之后是不可在原来的基础上进行修改。那么如果我要改变这个字符串的一部分怎么办呢?可以这样:
像其它语言,可以使用类似数组的方式
h[3] = '!'
去访问字符串中的特定位置的字符,这样就可以改变字符串。Java并没有提供。4. 检测字符串是否相等
可以使用
equals
方法检测两个字符串是否相等。如:想其它语言,可以使用"=="来进行判断,在Java中是万万不能的。在Java中,这个运算符只能够判断两个字符串是否放置在同一个位置,当然,如果是同一个位置,那么肯定相等,但是两个字符串相等,也有可能不在同一个位置,如:
这里指示介绍了字符串的一些Api,还有很多常用的Api,可以查看文档, 了解更多的字符串使用方法。
The text was updated successfully, but these errors were encountered: