1.创建字符串
以打印"hello world!"为例
伪代码如下:
String a="hello";
String b="world";
System.out.println(a+" "+b);
2.拼接字符串
以打印诗句:"床前明月光,疑是地上霜"为例
伪代码如下:
String a="床前明月光";
String b="疑是地上霜";
System.out.println(a+","+b);
3.计算字符串的长度
1).语法:str.length();
注: 其中str是字符串对象
例如:求出"we are students"字符串的长度
伪代码如下:
String a="we are students";
int size=a.length();
System.out.println(size);
此外还有一种等效的求出字符串长度的方法,在4中会提到
4.字符串查找
1)查找首次出现索引
语法:str.indexOf("value");
2)查找最后出现的索引
语法:str.;lastIndexOf("value");
同3例,求出字符"s"首次与最后一次出现的索引
伪代码如下:
String a="we are students";
int firstindex;
firstindex=a.indexOf("s");
System.out.println(firstindex);
int lastindex;
lastindex=a.lastIndexOf("s");
System.out.println(lastindex);
此外,如果lastIndexOf()方法中的参数是空字符串""(没有空格),那么就与length()方法完全等效。
还是以字符串"we are students"为例,观察这两种方法的结果
String a="we are students";
int length1=a.length();
int length2=a.lastIndexOf("");
System.out.println(length1);
System.out.println(length2);
结果均是15
15
15
5.查找指定索引处的字符
1).语法:str.charAt(int index)
str:字符串
index:要查找的索引
2).例如判断某文字是否只出现了一次
伪代码如下:
String a="we are students";
int i;
int count=0;
for(i=0;i<a.length()-1;i++)
{
a.charAt(i);
if(a.charAt(i)=='w')
{
count++;
}
}
if(count==1)
{
System.out.println("只出现一次");
}
else
{
System.out.println("出现了不止一次");
}
此外,还可以用此方法来获取最中间的字符
if(i%2==0)
{
System.out.println(a.charAt(i/2));
}
else
{
System.out.println(a.charAt(((i-1)/2)));
}
6.获取子字符串
1).语法1:str.substring(int beginindex);
str:目标字符串
beginindex:开始索引
2).语法2:str.substring(int beginindex,int lastindex);
str:目标字符串
beginindex:开始索引
lastindex:结束索引
以截取字符串"we are students"为例,伪代码如下:
String a="we are students";
String aSon1=a.substring(3);
System.out.println(aSon1);
String aSon2=a.substring(7);
System.out.println(aSon2);
String aSon3=a.substring(3,7);
System.out.println(aSon3);
结果如下:
are students
students
are
7.去掉首位空格
1)语法:str.trim();
例如去除字符串" we are students "的首位空格,并与去除前进行对照,伪代码如下:
String a=" we are students ";
System.out.println("原字符串是"+a);
String b=a.trim();
System.out.println("现字符串是:"+b);
System.out.println("***********************");
int size1=a.length();
int size2=b.length();
System.out.println("原字符串的长度是:"+size1);
System.out.println("现字符串的长度是:"+size2);
结果是:
原字符串是 we are students
现字符串是:we are students
***********************
原字符串的长度是:19
现字符串的长度是:15
8.字符串替换
语法:str.replace(String a,String b);
str:原字符串
a:要替换字符
b:替换目标字符串
例如:利用该方法修改病句
String a="we is students";
System.out.println(a);
String b=a.replace("is","are");
System.out.println(b);
结果如下:
we is students
we are students
9.字符串开始与结尾的关键字判断
1).开头判断
语法:str.startsWith(String prefix);
str:目标字符串
prefix:判断前缀
2).结尾判断
语法:str.endsWith(String suffix);
str:目标字符串
suffix:判断后缀
例如以下这个伪代码就模拟了手机的骚扰电话的开头号码或末尾号码识别:
String phoneNumber1="00213878";
String phoneNumber2="10213878";
boolean x=phoneNumber1.startsWith("00");
boolean y=phoneNumber2.startsWith("00");
if(x==true) {
System.out.println("第一个电话号码是骚扰电话,执行拦截");
}
else
System.out.println("第一个电话号码不是骚扰电话");
if(y==false) {
System.out.println("第二个电话号码不是骚扰电话是");
}
else
{
System.out.println("第二个电话号码是骚扰电话,执行拦截");
}
结果如下:
第一个电话号码是骚扰电话,执行拦截
第二个电话号码不是骚扰电话是