String 类复习笔记 {#String 类复习笔记}
String 类概述 {#String 类概述}
java.lang.String
类代表字符串。Java 程序中所有的字符串文字(例如"abc"
)都可以被看作是实现此类的实例。类String
中包括用于检查各个字符串的方法,比如用于比较 字符串,搜索 字符串,提取 子字符串以及创建具有翻译为 大写 或小写的所有字符的字符串的副本。
常用构造方法 {#常用构造方法}
String()
创建一个空的字符串对象String(String original)
根据字符串来创建一个字符串对象String(char[] value)
通过字符数组来创建字符串对象String(byte[] bytes)
通过字节数组来构造新的字符串对象String(byte[] bytes, int offset, int length)
通过字节数组一部分来构造新的字符串对象
代码示例 {#代码示例}
/**
* Software:IntelliJ IDEA 2018.2.4 x64
* Author: MoBai·杰
* Date: 2020/5/3 14:19
* ClassName:Demo4
* 类描述:String 类入门
*/
public class Demo4 {
public static void main(String[] args) {
// String() 创建一个空的字符串对象
String str1 = new String();
System.out.println("str1 =" + str1);// str1 =
<span class="token comment">// String(String original) 根据字符串来创建一个字符串对象</span>
<span class="token class-name">String</span> str2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"HelloWorld"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"str2 ="</span> <span class="token operator">+</span> str2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// str2 = HelloWorld</span>
<span class="token comment">// String(char[] value) 通过字符数组来创建字符串对象</span>
<span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> chars <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token char">'a'</span><span class="token punctuation">,</span> <span class="token char">'b'</span><span class="token punctuation">,</span> <span class="token char">'c'</span><span class="token punctuation">,</span> <span class="token char">'d'</span><span class="token punctuation">,</span> <span class="token char">'e'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>chars<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"str3 ="</span> <span class="token operator">+</span> str3<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// str3 = abcde</span>
<span class="token comment">// String(byte[] bytes) 通过字节数组来构造新的字符串对象</span>
<span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">97</span><span class="token punctuation">,</span> <span class="token number">98</span><span class="token punctuation">,</span> <span class="token number">99</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">101</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str4 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>bytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"str4 ="</span> <span class="token operator">+</span> str4<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// str4 = abcde</span>
<span class="token comment">// String(byte[] bytes, int offset, int length)通过字节数组一部分来构造新的字符串对象</span>
<span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">97</span><span class="token punctuation">,</span> <span class="token number">98</span><span class="token punctuation">,</span> <span class="token number">99</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">101</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> str5 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"str5 ="</span> <span class="token operator">+</span> str5<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// str5 = bc</span>
<span class="token punctuation">}</span>
}
String 特点 {#String 特点}
- 字符串不变: 字符串的值在创建后不能被更改
public static void main(String[] args) {
String s1 = "abc";
s1 += "d";
// 内存中有 "abc","abcd" 两个对象,s1 从指向 "abc",改变指向,指向了 "abcd"。
System.out.println("s1 =" + s1); // s1 = abcd
}
- 因为 String 对象是不可变的, 所以它们可以被共享
String s1 = "abc";
String s2 = "abc";
// 内存中只有一个 "abc" 对象被创建,同时被 s1 和 s2 共享。
System.out.println("输出结果等于:" + s1.equals(s2)); // 输出结果等于: true
常用方法 {#常用方法}
判断功能的方法 {#判断功能的方法}
-
public boolean equals (Object anObject)
:将此字符串与指定对象进行比较。 -
public boolean equalsIgnoreCase (String anotherString)
:将此字符串与指定对象进行比较,忽略大小写。
代码案例 {#代码案例}
public static void main(String[] args) {
// 创建字符串对象
String s1 = "hello";
String s2 = "hello";
String s3 = "HELLO";
// public boolean equals (Object anObject):将此字符串与指定对象进行比较。
System.out.println("结果等于:" + s1.equals(s2)); // 结果等于: true
System.out.println("结果等于:" + s2.equals(s3)); // 结果等于: false
// public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小写。
System.out.println("结果等于:" + s1.equalsIgnoreCase(s2)); // 结果等于: true
System.out.println("结果等于:" + s2.equalsIgnoreCase(s3)); // 结果等于: true
}
获取功能的方法 {#获取功能的方法}
public int length ()
:返回此字符串的长度。public String concat (String str)
:将指定的字符串连接到该字符串的末尾。public char charAt (int index)
:返回指定索引处的 char 值。public int indexOf (String str)
:返回指定子字符串第一次出现在该字符串内的索引。public String substring (int beginIndex)
:返回一个子字符串,从 beginIndex 开始截取字符串到字符串结尾。public String substring (int beginIndex, int endIndex)
:返回一个子字符串,从 beginIndex 到 endIndex 截取字符串。含 beginIndex,不含 endIndex。
代码示例 {#代码示例 -1}
public static void main(String[] args) {
// 创建字符串对象
String string = "HelloWorld";
// public int length () :返回此字符串的长度。
System.out.println("字符串长度是 =" + string.length()); // 字符串长度是 = 10
// public String concat (String str) :将指定的字符串连接到该字符串的末尾。
System.out.println("string =" + string.concat("+ MoBai")); // string = HelloWorld+ MoBai
// public char charAt (int index):返回指定索引处的 char 值。
System.out.println("指定索引 =" + string.charAt(5)); // 指定索引 = W
// public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引, 没有返回 -1
System.out.println("第一次出现的索引位置在 =" + string.indexOf("W")); // 第一次出现的索引位置在 = 5
System.out.println("第一次出现的索引位置在 =" + string.indexOf("Ak")); // 第一次出现的索引位置在 = -1
// public String substring (int beginIndex, int endIndex) :返回一个子字符串,从 beginIndex 到 endIndex 截取字符串。含 beginIndex,不含 endIndex。
System.out.println("截取后的字符串为:" + string.substring(0, string.length())); // 截取后的字符串为:HelloWorld
System.out.println("截取后的字符串为:" + string.substring(3, 8)); // 截取后的字符串为:loWor
}
转换功能的方法 {#转换功能的方法}
public char[] toCharArray ()
:将此字符串转换为新的字符数组。public byte[] getBytes ()
:使用平台的默认字符集将该 String 编码转换为新的字节数组。public String toLowerCase()
:使用默认语言环境的规则将此 String 所有字符转换为小写。public String toUpperCase()
:将此 String 所有字符转换为大写,使用默认语言环境的规则。public String replace (CharSequence target, CharSequence replacement)
:将与 target 匹配的字符串使用 replacement 字符串替换。
代码示例 {#代码示例 -2}
public static void main(String[] args) {
// 创建字符串对象
String s = "abcde";
<span class="token comment">// char[] toCharArray(): 把字符串转换为字符数组</span>
<span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> chs <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> chs<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>chs<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-----------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// byte[] getBytes (): 把字符串转换为字节数组</span>
<span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> bytes<span class="token punctuation">.</span>length<span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>bytes<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-----------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 替换字母 it 为大写 IT</span>
<span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token string">"itcast itheima"</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> replace <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"it"</span><span class="token punctuation">,</span> <span class="token string">"IT"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>replace<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// ITcast ITheima</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"-----------"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
}
CharSequence 是一个接口,也是一种引用类型。作为参数类型,可以把 String 对象传递到方法中。
分割功能的方法 {#分割功能的方法}
public String[] split(String regex)
:将此字符串按照给定的 regex(规则)拆分为字符串数组。
代码示例 {#代码示例 -3}
public static void main(String[] args) {
// 创建字符串对象
String s = "aa,bb,cc";
String[] strArray = s.split(","); // ["aa","bb","cc"]
for(int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]); // aa bb cc
}
}
String 类练习 {#String 类练习}
需求: 键盘录入一个字符,统计字符串中大小写字母及数字字符个数
public class StringDemoTest {
public static void main(String[] args) {
<span class="token comment">// 创建 scanner 类接收键盘录入</span>
<span class="token class-name">Scanner</span> scanner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"请输入一个字符串数据:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">String</span> s <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">nextLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 定义三个统计变量, 初始化 '0'</span>
<span class="token keyword">int</span> bigCount <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> smallCount <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> numberCount <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token comment">// 遍历字符串,得到每一个字符</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> x <span class="token operator"><</span> s<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> x<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">char</span> ch <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 拿字符进行判断</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>ch <span class="token operator">>=</span> <span class="token char">'A'</span> <span class="token operator">&&</span> ch <span class="token operator"><=</span> <span class="token char">'Z'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
bigCount<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>ch <span class="token operator">>=</span> <span class="token char">'a'</span> <span class="token operator">&&</span> ch <span class="token operator"><=</span> <span class="token char">'z'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
smallCount<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>ch <span class="token operator">>=</span> <span class="token char">'0'</span> <span class="token operator">&&</span> ch <span class="token operator"><=</span> <span class="token char">'9'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
numberCount<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"该字符"</span> <span class="token operator">+</span> ch <span class="token operator">+</span> <span class="token string">"非法"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// 输出结果</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"大写字符:"</span> <span class="token operator">+</span> bigCount <span class="token operator">+</span> <span class="token string">"个"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"小写字符:"</span> <span class="token operator">+</span> smallCount <span class="token operator">+</span> <span class="token string">"个"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"数字字符:"</span> <span class="token operator">+</span> numberCount <span class="token operator">+</span> <span class="token string">"个"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}