String类重要方法

1、split

public String[] split​(String regex)

作用:通过在给定正则表达式的匹配项周围拆分此字符串计算的字符串数组

例:

public class Demo1 {
    public static void main(String[] args) {
        String s = "I am a beautiful boy";
        String [] ss = s.split(" ");
        for(String str:ss){
            System.out.println(str);
        }
    }
}

代码运行截图:
在这里插入图片描述

2、valueOf

public static String valueOf​(Object obj)

作用:返回 Object参数的字符串表示形式。

例:

public class Demo1 {
    public static void main(String[] args) {
        double x = 0.21212;
        String s = String.valueOf(x);
        System.out.println(s);
    }

程序输出结果是:0.21212


public static String valueOf​(char[] data)

作用:返回char数组参数的字符串表示形式。

例:

public class Demo1 {
    public static void main(String[] args) {
        char[] c={'a','b','c'};
        String s = String.valueOf(c);
        System.out.println(s);

    }
}

程序输出结果是:abc


public static String valueOf​(char[] data, int offset, int count)

作用:返回char数组参数的特定子数组的字符串表示形式。
data - 字符数组。
offset - 子阵列的初始偏移量。
count - 子阵列的长度。

例:

public class Demo1 {
    public static void main(String[] args) {
        char[] c={'a','b','c','d','e'};
        String s = String.valueOf(c,1,3);
        System.out.println(s);

    }

程序输出结果是:bcd


3、trim

public String trim()

作用:返回一个字符串,其值为此字符串,删除了所有前导和尾随空格

trim与strip的作用差不多。

例:

public class Demo1 {
    public static void main(String[] args) {
        String s = "     iddsd      ";
        System.out.println("没有使用trim时输出的s:");
        System.out.println(s);
        s = s.trim();
        System.out.println("使用trim后输出的s:");
        System.out.println(s);

    }
}

在这里插入图片描述

4、toCharArray

public char[] toCharArray()

作用:将此字符串转换为新的字符数组

例:

String s = "good good study, day day up";
char[] c = s.toCharArray();

5、repeat

public String repeat​(int count)

作用:返回一个字符串,其值为此字符串的串联重复count次。

例:

public static void main(String[] args) {
        String s = "good ";
        s =s.repeat(5);
        System.out.println(s);

    }

在这里插入图片描述

6、replace

public String replace​(char oldChar, char newChar)

作用:返回从替换所有出现的导致一个字符串oldChar在此字符串newChar 。

例:

 String s = "good good study";
        s = s.replace('g','G');
        System.out.println(s);

在这里插入图片描述

public String replaceAll​(String regex, String replacement)

作用:替换给定的字符串。
regex - 要与此字符串匹配的正则表达式
replacement - 要替换每个匹配项的字符串

例:

 String s = "good good study";
        s = s.replaceAll("good","GOOD");
        System.out.println(s);

在这里插入图片描述

7、indexOf

public int indexOf​(int ch)

作用:返回指定字符第一次出现的字符串中的索引

public int lastIndexOf​(String str)

作用:返回指定子字符串最后一次出现的字符串中的索引

String s = "good good study";
        System.out.println(s.indexOf('d'));
        System.out.println(s.lastIndexOf('d'));

在这里插入图片描述

String s = "good good study";
 System.out.println(s.indexOf("good"));
  System.out.println(s.lastIndexOf("good"));

在这里插入图片描述

8、charAt

public char charAt​(int index)

作用:返回指定索引处的char值

String s = "好好学习";
//输出学
System.out.println(s.charAt(2));

9、compareTo

public int compareTo​(String anotherString)

按字典顺序比较两个字符串。 比较基于字符串中每个字符的Unicode值。

String s = "好好学习";
        System.out.println(s.compareTo("好好学习"));
        System.out.println(s.compareTo("好好学"));
        System.out.println(s.compareTo("好好学习啊"));
        System.out.println(s.compareTo("天天向上"));

在这里插入图片描述

10、concat

public String concat​(String str)

作用:将指定的字符串连接到此字符串的末尾

String s = "好好学习";
        s = s.concat(",天天向上");
        System.out.println(s);

在这里插入图片描述

11、contains

public boolean contains​(CharSequence s)

作用:如果此字符串包含 s ,则返回true,否则 s false

String s = "好好学习";
        s = s.concat(",天天向上");
        System.out.println(s.contains("好好"));

结果为true

12、endsWith

public boolean endsWith​(String suffix)

作用:测试此字符串是否以指定的后缀结尾。

startsWith:测试此字符串是否以指定的前缀开头。

String s = "好好学习";
        s = s.concat(",天天向上");
        System.out.println(s.startsWith("好"));
        System.out.println(s.endsWith(" "));

在这里插入图片描述

13、equals、maches

public boolean equals​(Object anObject)

作用:将此字符串与指定的对象进行比较

public boolean matches​(String regex)

作用:判断此字符串是否与给定的字符串匹配。

14、substring

public String substring​(int beginIndex)

作用:返回子字符串以指定索引处的字符开头,并延伸到此字符串的末尾。

String s = "好好学习天天向上";
        s = s.substring(4) ;
        System.out.println(s);

在这里插入图片描述
public String substring​(int beginIndex, int endIndex)

作用:返回一个字符串,该字符串是此字符串的子字符串。 子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1处的字符。 因此子串的长度是endIndex-beginIndex 。

beginIndex - 起始索引,包括在内。
endIndex - 结束索引,不包括。

String s = "好好学习天天向上";
        System.out.println(s.substring(0,4));

在这里插入图片描述

15、isBlack、isEmpty

public boolean isBlank()

作用:如果字符串为空或仅包含 white space个代码点,则返回 true ,否则 false 。

public boolean isEmpty()

作用:如果 length()是 0 返回true,否则 false

String s = "       ";
        System.out.println(s.isBlank());
        System.out.println(s.isEmpty());
        System.out.println(s.length());

在这里插入图片描述

String s = "";
        System.out.println(s.isBlank());
        System.out.println(s.isEmpty());
        System.out.println(s.length());

在这里插入图片描述

16、toLowerCase、toUpperCase

public String toLowerCase()

作用:使用默认语言环境的规则将此String所有字符转换为小写。

String s = "GOOD GOOD STUDY";
        System.out.println(s.toLowerCase());

在这里插入图片描述
public String toUpperCase()

作用:使用默认语言环境的规则将此String所有字符转换为大写

String s = "good good study";
        System.out.println(s.toUpperCase());

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值