目录
(2)、 indexOf(String str.int fromindex)
(3)、public int lastindexOf(str)
(4)、lastIndexOf(String str,int fromIndex)
(2)、startsWith(String prefix,int toffset)
(2)、subString(int beginIndex,int endIndex)
(1)、repiace(CharString target,CharSequence repiacement)
(2)、replaceAll(String regex,String repiacement)
(3)、repiaceFirst(String regex,String repiacement)
(2)、split(String regex,int limit)
(2)、equalsIgnoreCase(String anotherString)
(1)、format(String format,Object…args)
(2)、format(Local 1,String format,Object…args)
(3)、setCharAt(int index,char ch)
(4)、insert(int offset,String str)方法
(6)、delete(int start,int end)方法
3、StringBuffer、StringBuilder、 String 之间的关系
(1)、StringBuffer、StringBuilder、String 互相转换
(2)、StringBuffer、StringBuilder、String的不同之处
一、String类
‘ ’字符 1个
“ ”字符串 多个
1、引用字符串常量
String a =”建团一百周年”;
String a;
a =”建团一百周年”;
2、利用String提供的构造方法
String a = new String(“建团一百周年”);
3、利用字符数组
Char[] ch = {‘建’,‘团’,‘一’,‘百‘,’周‘,’年‘};
4、提取字符数组中的一部分
Char[] ch = {‘举‘,‘办’,‘建’,‘团’,‘一’,‘百‘,’周‘,’年‘,‘晚’,‘会’};
String a = new String(ch,前序号,后序号);
String a = new String(ch, 2,8);
编写一段代码,声明多个字符串变量,用不同的赋值方法给这些字符串变量赋值并输出。
package d5z;
/**
*
* @author Dejon_D
*
*/
public class d5_1 {//创建类
public static void main(String[] args) {//主方法
String a = "时间就是金钱,我的朋友。";//直接引用字符串常量
System.out.println("a = " + a);
String b = new String("我爱清汤小肥羊");//利用构造方法实例化
String c = new String(b);//使用已有字符串变量实例化
System.out.println("b = " + b);
System.out.println("c = " + c);
char[] charArray = {'t','i','m','e'};
String d = new String(charArray);//利用字符数组实例化
System.out.println("d = " + d);
char[] charArray2 = {'时','间','就','是','金','钱'};
//提取字符数组部分内容,从下标为4的元素开始,截取2个字符
String e = new String(charArray2, 4, 2);
System.out.println("e = " + e);
}
}
运行结果:
二、连接字符串
Int a = 1;
Int b = 2;
Int c = a + b;//c =3
B += a;//b=b+3
String a = “建团”;
String b = “一百周年”;
B = a + b;//b =”建团一百周年”
B +=a;// ”建团一百周年”
“建团”+“一百周年”//“建团一百周年”
String a = “建团“;
Int b = 100;
String c = “周年”;
String d = a + b + c; //d = “建团100周年”
1、连接字符串
使用“+”和“+=”拼接字符串。
代码如下:
package d5z;
/**
*
* @author Dejon_D
*
*/
public class d5_2 {//创建类
public static void main(String[] args) {//主方法
String a = "abc";//定义a
String b = "123";//定义b
String c = a + b + "!";//使用“+”拼接字符串
String d = "拼接字符串";
d += c; //使用“+=”拼接字符串
System.out.println("a = " + a);//输出a
System.out.println("b = " + b);//输出b
System.out.println("c = " + c);//输出c
System.out.println("d = " + d);//输出d
}
}
运行结果:
2、连接其他数据类型
在项目中创建类Link,在主方法中创建数值型变量,实现将字符串与整型、浮点型变量相连的结果输出。
代码如下:
package d5z;
public class d5_3 {//创建类
public static void main(String[] args) {//主方法
int booktime = 4; //声明int型变量booktime
float practice = 2.5f; //声明float型变量practice
//将字符串与整型、浮点型变量相连,并将结果输出
System.out.println("我每天花费" + booktime + "小时看书;" + practice + "小时上机练习");
}
}
运行结果:
本例实现的是将字符串常量与整型变量 booktime 和浮点型变量 practice 相连后的结果输出。在
这里 booktime 和 practice 都不是字符串,当它们与字符串相连时会自动调用 toString()方法,将其转换成字符串形式,然后参与连接。
注意:
只要“+”运算符的一个操作数是字符串,编译器就会将另一个操作数转换成字符串形式,所以应谨慎地将其他数据类型与字符串相连,以免出现意想不到的结果。
三、提取字符串信息
长度 字符串名.length();
String a = “建团一百周年”;
Int b = a.length();
获取指定字符 字符串名.charAt(序号;
String a = “建团一百周年“;
Char b = a.charAt(3);//百
获取子串位置
String a = “建团一百周年”;
Int b = a.indexof(“百”);
获取子串位置
String a =”abacadaeaf”;
Int b = a.indextof(“a”); //0
Int c = a.lastIndexof(“a”); //8
Int d = a.indextof(“a”, 3); //0
Int e = a.lastIndexof(“a”, 7); //6
判断首位内容
String a = “建团一百周年”;
Boolean b = a.startswith(“建团”);//判断是否以**开头
Boolean c = a.endswith(“建团”);//判断是否以**结尾
获取字符数组 Char[] ch = 字符串名.toCharArray( );
String a = “建团一百周年”;
Char[] ch = a.toCharArray( );
判断子串是否存在 字符串名.contains(子串);
String a = “建团一百周年”;
Boolean b = a.contains(“百”);
1、获取字符串长度
字符串的length()方法与数组的length虽然都是用来获取长度的,但两者却有些不同。String的length()是类的成员方法,是有括号的;数组的length是一个属性,是没有括号的。
2、获取指定的字符
创建字符串对象,查看字符串索引位置是4的字符。
代码如下:
package d5z;
public class d5_4 {//创建类
public static void main(String[] args) {//主方法
String str = "床前明月光,疑是地上霜。";//创建字符串变量str
char chr = str.charAt(4);//将字符串str中索引位置为4的字符赋值给chr
System.out.println("字符串中索引位置为4的字符是:" + chr);//输出chr
}
}
运行结果:
3、获取子字符串索引位置
(1)、indexOf(String str)
创建字符串对象str,判断str中是否包含子字符串“abc”。
代码如下:
package d5z;
public class d5_5 {//创建类
public static void main(String[] args) {//主方法
String str = "12345abcde";//创建字符串对象
int charIndex = str.indexOf("abc");//获取字符串str中“abc”字符串,赋值给charIndex
if (charIndex != -1) {//判断:index的值不等于-1
//如果index不等于-1,则执行此代码,说明str中存在"abc"字符串
System.out.println("str中存在abc字符串");//输出信息
}else {//如果index等于-1,则执行此代码,说明str中没有"abc"字符串
System.out.println("str中没有abc字符串");//输出信息
}
}
}
运行结果:
(2)、 indexOf(String str.int fromindex)
查找字符串“We are the world”中“r”第一、二、三次出现的索引位置。
代码如下:
package d5z;
public class d5_6 {//创建类
public static void main(String[] args) {//主方法
String str = "We are the world";//创建字符串
int firstIndex = str.indexOf("r");//获取字符串中“r”第一次出现的索引位置
//获取字符串中“r”第二次出现的索引位置,从第一次出现的索引位置之后开始查找
int secondIndex = str.indexOf("r", firstIndex + 1);
//获取字符串中“r”第三次出现的索引位置,从第二次出现的索引位置之后开始查找
int thirdIndex = str.indexOf("r", secondIndex + 1);
//输出三次获取的索引位置
System.out.println("r第一次出现的索引位置是:" + firstIndex);//输出firstIndex
System.out.println("r第二次出现的索引位置是:" + secondIndex);//输出secondIndex
System.out.println("r第三次出现的索引位置是:" + thirdIndex);//输出thirdIndex
}
}
运行结果:
(3)、public int lastindexOf(str)
查找字符串“Let it go !Let it go !”中单词“go”最后出现的位置。
代码如下:
package d5z;
public class d5_7 {//创建类
public static void main(String[] args) {//主方法
String str = "Let it go!Let it go!"; //创建字符串对象
int gIndex = str.lastIndexOf("g"); //返回“g”最后一次出现的位置
int goIndex = str.lastIndexOf("go"); //返回“go”最后一次出现的位置
int oIndex = str.lastIndexOf("o"); //返回“o”最后一次出现的位置
System.out.println("字符串\"Let it go!Let it go! ”中:\n");
System.out.println("\"g”最后一次出现的位置是:"+gIndex);//输出gIndex
System.out.println("\"o”最后一次出现的位置是:"+oIndex);//输出oIndex
System.out.println("\"go”最后一次出现的位置是:"+goIndex);//输出goIndex
}
}
运行结果: