Java字符串的常见用法
1. 声明字符串
字符串关键字 String str1;
2. 创建字符串
三种实现方法
a.通过引用赋值
str1=”hello java”;
b.String(char a[])方法
char a[]={'G','o','o','d'};
str1=new String(a);
str1 is "Good"
c.String(char a[],int offset, int length)
char a[]={‘G’,’o’,’o’,’d’,’W’,’o’,’r’,’l’,’d’};
str1=new String(a,4,5);
str1 is “World”
3. 连接字符串
简单的加号相连即可
str1=”Good”+”World” ==>str1 is “GoodWorld”
4. 连接其他数据类型
一个字符串通过加号和另外一个非字符串相连的时候,会自动将
非字符串改成字符串。
String str2=”World”+ 5;==>str2 World5
5. 获得字符串长度
str.length()
6. 字符串查找
str.indexOf(String s)
str.LastIndexOf(String s)
以上两种方法,找到字符串s的地址后会返回它的位置,如果没
有找到则返回-1
7. 获得指定索引位置的字符
str.charAt(int index)
可以将指定索引处位置的字符返回
如果index值越界,则会报错
String s1="hello xiaofeng!";
for(int i=0;i<= 20;i++)
{
System.out.println(s1.charAt(i));
}
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException: String index
out of range: 15
at java.lang.String.charAt(String.java:658)
at hellojava.main(hellojava.java:17)
8. 获得子字符串
str.substring(int beginIndex)
str.substring(int beginIndex,int endIndex)
String s1="hello xiaofeng!";
System.out.println(s1.substring(6));
System.out.println(s1.substring(6,10));
结果是
xiaofeng!
xiao
同样,如果index超出边界了,也是会出错的。
9. 去除字符串前后的空格
str.trim()
10. 字符串替换
str.replace(char oldChar,char newChar)
String s1="hello xiaofeng!";
System.out.println(s1.replace("hello","Very Good"));
结果是
Very Good xiaofeng!
11. 判断字符的开始和结束
str.startsWith(String prefix)
str.endsWith(String suffix)
返回布尔类型的值
12. 判断两个字符是否相等
注意,不能简单运用比较运算符”==”,因为比较运算符比较的是
两个字符串地址是否相同。
有两种方法
str.equals(String otherstr)
str.equalsIgnoreCase(String otherstr)
前者区分大小写,后者忽略大小写,返回值都是布尔类型
13. 按照字典顺序比较两个字符串
str.compareTo(String otherstr)
返回值是负数,0,正数,分别代表小,等于,大于三种情况。
String s1="hello xiaofeng!";
String s2="hello morning";
System.out.println(s1.compareTo(s2));
结果是11,正好是x和m之间的差距
14. 字符大小写转换
str.toLowerCase()
str.toUpperCase()
String s1="hello xiaofeng!";
String s2="HAPPY EVERY DAY";
System.out.println(s1.toUpperCase());
System.out.println(s2.toLowerCase());
结果是
HELLO XIAOFENG!
happy every day
15. 字符串分割
str.split(String sign)
str.split(String sign,int limit)
前者是简单按照字符sign分割,后者可以限定分割limit次
String s1="hello,xiaofeng!";
String s2="HAPPY,EVERY DAY";
String[] a=s1.split(",");
String[] b=s2.split(",");
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
for(int j=0;j<b.length;j++){
System.out.println(b[j]);
}
}
结果是
hello
xiaofeng!
HAPPY
EVERY DAY
注意,此处分割符的类型是String,不能用单引号的char类型。即使分隔符只有一个字符,也得用双引号。
16. 格式化字符串
str.format(String format,object…args)
略
17. 字符串生成器
str.append(content)
str.insert(int offset,arg)
str.delete(int start,int end)
StringBuilder builder=new StringBuilder("");
System.out.println(builder.append("It is a sunny day "));
System.out.println(builder.append("Let's go outside"));
System.out.println(builder.insert("It is a sunny day".length(),"if you like,"));
System.out.println(builder.delete("It is a sunny day".length(),"It is a sunny dayif you like,".length()));
结果
It is a sunny day
It is a sunny day Let’s go outside
It is a sunny dayif you like, Let’s go outside
It is a sunny day Let’s go outside
18. 正则表达式
和其他语言类似,使用的方法是str.matches(reg)