10.1 String类
10.1.1 声明字符串
字符串必须包含在一对双引号(“”)之内。例如:
"23.23"、"ABCDE"、"你好"
String str;
String: 指定该变量为字符串变量类型;
str:任意有效的标识符,表示字符串变量的名称。
声明字符串变量s,代码如下:
String s;
10.1.2 创建字符串
1. String(char a[])
2. String(char a[],int offset,int length)
3. String(char[] value)
例如:
package tenbao;
public class Test {
public static void main(String[] args) {
//字符串变量的声明
String s;
//字符串变量初始化
// s = "你好";
s = new String("你好");
System.out.println(s);
//字符串变量初始化 传入字符类型数组
char[] ch = {'g','o','o','d'};
String st = new String(ch);
System.out.println(ch);
//字符串变量的初始化 传入字符串类型数组,初始索引号,个数
char[] chr = {'s','t','u','d','e','n','t'};
String str = new String(chr,2,4);
System.out.println(str);
//字符串相连接
System.out.println(s + st);
//字符串与其他相连接
System.out.println(1.6 + str + 3);
}
}
10.2 连接字符串
10.2.1 连接多个字符串
使用 “+” 运算符可实现连接多个字符串的功能。
例题10.1:
package tenbao;
public class join {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1 = new String("春色绿千里");
String s2 = new String("马蹄香万家");
String s = s1 + "\n" + s2;
System.out.println(s);
}
}
10.2.2 连接其他数据类型
例题10.2:
package tenbao;
public class Link {
public static void main(String[] args) {
// TODO Auto-generated method stub
int booktime = 4;
float practice = 2.5f;
//将字符串与整数类型、浮点类型相连、并将结果输出
System.out.println("我每天花费" + booktime + "小时看书:" + practice + "小时上机练习");
}
}
10.3 获取字符串信息
10.3.1 获取字符串长度
使用String类的length()方法可获取声明字符串对象的长度。
获取字符串长度,代码:
String str = "We are student";
int size = str.length();
10.3.2 字符串查找
1.indexOf(String s)
该方法用于返回参数字符串 s 在指定字符串中首次出现的位置。如果没有检索到字符串 s ,该方法返回 -1
查找字符 a 在字符串 str 中的索引位置,代码:
String str = "We are student";
int size = str.indexOf('a');
2.lostindexOf(String str)
该方法用于返回参数字符串 s 在指定字符串中最后一次出现的位置。如果没有检索到字符串 s ,该方法返回 -1
说明:
如果 lostindexOf()方法中的参数是空字符串 "" (注意没有空格),则返回的结果与调用 length()
方法的返回值相同。
例题10.3:
package tenbao;
public class Test {
public static void main(String[] args) {
String str = "We are student";
int size = str.lastIndexOf("");
System.out.println("空字符串在字符串 str 中的索引位置是:" + size);
System.out.println("字符串 str 的长度为" + str.length());
}
}
10.3.3 获取指定索引位置的字符
使用 charAt(int index)方法可指定索引处的字符返回。语句如下:
str.charAt(int index)
例题10.4:
package tenbao;
public class Ref {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "hello world";
char mychar = str.charAt(6);
System.out.println("字符串 str 中索引位置是 6 的字符为:" + mychar);
}
}
1
0.4 字符串操作
10.4.1 获取子字符串
1. substring(int beginIndex)
该方法返回的是从指定的索引位置开始截取直到该字符串结尾的字串。语法如下:
str.substring(int beginIndex)
2. substring(int beginIndex,int endIndex)
该方法返回的是从字符串某一索引位置开始到某一位置结尾的字串。语法如下:
str.substring(int beginIndex,int engIndex)
例题10.5:
package tenbao;
public class Subs {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "《将进酒》:李白(唐)";
String substr = str.substring(6,8);
System.out.println("《将进酒》的作者是" + substr);
}
}
10.4.2 去除空格
trim() 方法返回字符串的副本,忽略前导空格和尾部空格。语法如下:
str.trim()
例题10.6:
package tenbao;
public class Blak {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = " java class ";
System.out.println("字符串原来的长度:" + str.length());
//将 str 去掉前导和尾部的空格后的长度输出
System.out.println("字符串现在的长度:" + str.trim().length());
}
}
10.4.3 字符串替换
replace()方法可实现将指定的字符或字符串替换成新的字符或字符串。语法如下:
str.replace(CharSequence target,CharSequence replacment)
例题10.7:
package tenbao;
public class NewStr {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "address";
String newstr = str.replace("a","A");
System.out.println(newstr);
}
}
10.4.4 判断字符串的开始与结尾
1.startsWith() 方法
该方法用于判断当前字符串对象的前缀是否为参数指定的字符串。语法如下:
startsWith(String prefix)
2.endsWith() 方法
该方法用于判断当前字符串是否以给予的字符串结尾。语法如下
str.endsWith(String suffix)
例题10.3:
package tenbao;
public class Test2 {
public static void main(String[] args) {
//判断字符串开头
String num1 = "22045612";
boolean b1 = num1.startsWith("22");
System.out.println(b1);
//判断字符串结尾
String num2 = "21304578";
boolean b2 = num2.endsWith("78");
System.out.println(b2);
}
}