JAVA字符串类介绍

1.String类

1.1 String类定义和使用

日常工作中,我们经常会用到字符串,“hellow world”,"hellow JAVA"等等,在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。
创建字符串:
String s = “Hello World”;
String s = new String(“Hello World”);

1.2 String类构造方法

方法声明:功能描述
1.String():创建一个内容为空的字符串
2.String(String value):根据指定的字符串内容创建对象
3.String(char[] value):根据指定的字符数组创建对象
4.String(byte[] bytes):根据指定的字节数组创建对象

1.3 String类常用方法

1.length():字符串数
例:
String str=“hellow world”;
System.out.println(str.length());
输出结果为:12

2.equals():比较存储在两个字符串对象的内容是否一致

String str ="abc";
        String  str1="abc";
        if(str.equals(str1)){
            System.out.println("一致");
        }else {
            System.out.println("不一致");
        }

字符串比较的其他方法
使用equalsIgnoreCase()方法:忽略大小写
使用toLowerCase()方法:转化成小写
使用toUpperCase()方法:转化成大写

3.concat()方法:字符串连接

        String str ="abc";
        String  str1="abcd";
        System.out.println(str.concat(str1));

运行结果:abcabcd
字符串连接也可以使用"+"

        String str ="abc";
        String  str1="abcd";
        System.out.println(str+str1);

运行结果:abcabcd

4.indexOf():搜索第一个出现的字符串value,如果没有找到,返回-1
5. lastIndexOf():搜索最后一个出现的字符串value,如果没有找到,返回-1
6. substring(int index):提取从位置索引开始的字符串部分
7. trim():返回一个前后不含任何空格的调用字符串的副本

练习:输出"hello world hellow java hellow bigdata"中hellow出现的次数
思路:分别对字符串中的数据元素和特定字符串进行比较,得出数量
代码如下:

String str="hellow world hellow java hellow bigdata";
        int count =0;//初始化数量
        StringTokenizer stt = new StringTokenizer(str);//对字符串进行分割
        System.out.println("分离出"+stt.countTokens()+"个单词");
        while (stt.hasMoreTokens()){//循环比较分割好的字符串
             if(stt.nextToken().equals("hellow")){
                count++;//相同则数量加一次
            }
        }
        System.out.println("一共出现了"+count+"次hellow");

分割字符串的方法也可以用如下方法:

  String[] s = str.split(" ");//以空格符分割字符串成数组
        for (String s1 : s//遍历数组与字符串“hellow”比较
        ) {
            if (s1.equals("hellow")) {
                count++;
            }
        }

2.StringBuffer类

2.1StringBuffer概念

StringBuffer与String基本相同,但它对字符串频繁修改(如字符串连接)时,使用StringBuffer类可以大大提高程序执行效率,在使用 StringBuffer 类时,每次都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,所以如果需要对字符串进行修改推荐使用 StringBuffer。

在这里插入图片描述

2.2StringBuffer类申明

StringBuffer类申明:
StringBuffer s = new StringBuffer();初始化一个字符串缓冲区
StringBuffer s= new StringBuffer(“aaa”);初始化字符串“aaa”
StringBuffer s= new StringBuffer(int size);初始化指定大小

2.3StringBuffer使用

StringBuffer常用方法:
1.s.toString(); //转化为String类型
2.s.append(“"); //追加字符串
3.s.insert (1, "
”); //插入字符串

例:

String str ="1234567890";//1234567890   实现:1,234,567,890
        StringBuffer sb2 = new StringBuffer(str);//新建StringBuffer类
        for (int i=sb2.length()-3;i>0;i-=3) {
            sb2.insert(i,",");//插入","字符串
        }
        System.out.println(sb2.toString());

3.StringBuffer delete(int start,int end):删除指定位置的内容
4.StringBuffer replace(int start, int end, String str):把end位置替换为str
5.StringBuffer reverse() :将此字符序列用其反转形式取代

3.StringBuilder

3.1概念

与StringBuffer相似也是一个可变的字符序列,此类设计用作StringBuffer替代品,用于单个线程使用字符串缓冲区的位置(通常情况下)。建议使用此类优先于StringBuffer因为在大多数实现中它会更快。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值