Java字符串的常见用法

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)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南野栀子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值