java--String与StringBuffer/StringBuilder常用方法

String类常用方法

/**
 * 功能描述
 *
 * @author yunweixiaocai
 * @since 2021-04-20
 */
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.util.Arrays;

import org.w3c.dom.ls.LSOutput;

class JavaStudy {
    static String str = "   I am a good 男孩 ";
    static String str2 = "I am a good boy";
    public static void main(String[] args) {
        System.out.println("*****************常用方法*******************");
        System.out.println(str.length());
        System.out.println(str.charAt(10));
        System.out.println(str.isEmpty());
        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
        System.out.println(str.trim());
        System.out.println(str.trim().equals(str2));
        System.out.println(str.concat(str2));
        System.out.println(str.compareTo("I"));
        System.out.println(str.substring(10));
        System.out.println(str.substring(10,14));
        System.out.println("*****************判断*******************");
        System.out.println(str.endsWith("boy "));
        System.out.println(str.startsWith("   I"));
        System.out.println(str.startsWith("am",5));
        System.out.println(str.contains("good"));
        System.out.println(str.indexOf(str2));
        System.out.println(str.indexOf('o'));
        System.out.println(str.indexOf('o', 2));
        System.out.println(str.lastIndexOf('o'));
        System.out.println("******************替换与切片******************");
        System.out.println(str.replace("good", "bad"));
        System.out.println(str.replace('o', 'e'));
        System.out.println(str.replaceAll("a.", "bb"));
        System.out.println(str.replaceFirst("a.", "bb"));
        String[] strs = str.split(" ");
        for (String s : strs) {
            System.out.println(s);
        }
        System.out.println("******************类型转换******************");
        System.out.println(Integer.parseInt("1234"));
        System.out.println(Boolean.parseBoolean("true"));
        System.out.println(String.valueOf(2134));
        System.out.println(String.valueOf(false).getClass());
        System.out.println(String.valueOf(new char[]{'g', 'o', 'o', 'd'}));
        System.out.println(new String(new char[]{'g', 'o', 'o', 'd'}));
        char[] chars = "good".toCharArray();
        for (char c : chars){
            System.out.println(c);
        }

        try {
            System.out.println(Arrays.toString(str.getBytes("gbk")));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        byte[] bytes = new byte[]{32, 32, 32, 73, 32, 97, 109, 32, 97, 32, 103, 111, 111, 100, 32, 98, 111, 121, 32};
        System.out.println(new String(bytes));

    }
}

输出:

*****************常用方法*******************
19
g
false
   i am a good boy 
   I AM A GOOD BOY 
I am a good boy
true
   I am a good boy I am a good boy
-41
good boy 
good
*****************判断*******************
true
true
true
true
3
11
11
16
******************替换与切片******************
   I am a bad boy 
   I am a geed bey 
   I bb bbgood boy 
   I bb a good boy 



I
am
a
good
boy
******************转换******************
1234
true
2134
class java.lang.String
good
good
g
o
o
d
[32, 32, 32, 73, 32, 97, 109, 32, 97, 32, 103, 111, 111, 100, 32, 98, 111, 121, 32]
   I am a good boy 

StringBuffer/StringBuilder通用方法

class JavaStudy {
    static StringBuffer stringBuffer = new StringBuffer("I am a good boy");
    static String str2 = ", are you?";
    public static void main(String[] args) {
        System.out.println("*****************常用方法*********************");
        stringBuffer.append(str2);
        System.out.println(stringBuffer);
        stringBuffer.delete(7,12);
        System.out.println(stringBuffer);
        stringBuffer.replace(7,10,"girl");
        System.out.println(stringBuffer);
        stringBuffer.insert(0, "Hey, ");
        System.out.println(stringBuffer);
        stringBuffer.reverse();
        System.out.println(stringBuffer);
        stringBuffer.reverse();
        stringBuffer.setCharAt(3,'!');
        System.out.println(stringBuffer);
        System.out.println("*****************方法链*********************");
        StringBuilder sb = new StringBuilder("I am a good boy");
        sb.append(str2).delete(7,12).replace(7,10,"girl").insert(0, "Hey, ").reverse().reverse().setCharAt(3,'!');
        System.out.println(sb);
        System.out.println("*****************无修改操作*********************");
        System.out.println(stringBuffer.substring(0, 11));
        System.out.println(stringBuffer.length());
        System.out.println(stringBuffer.charAt(3));
    }
}

输出:

*****************常用方法*********************
I am a good boy, are you?
I am a boy, are you?
I am a girl, are you?
Hey, I am a girl, are you?
?uoy era ,lrig a ma I ,yeH
Hey! I am a girl, are you?
*****************方法链*********************
Hey! I am a girl, are you?
*****************无修改操作*********************
Hey! I am a
26
!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

运维小菜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值