Java:String类常用API

字符串的内容比较:

  • 推荐使用String类提供的“equals”比较:只关心内容一样即可
  • 如图:

 演示代码如下:

public class StringEqualsDemo4 {
    public static void main(String[] args) {
        //1.正确登录名和密码
        String okName = "itheima";
        String okPassword = "123456";

        //2.请您输入正确的登录名和密码
        Scanner sc = new Scanner(System.in);
        System.out.println("登录名称:");
        String name = sc.next();
        System.out.println("登陆密码:");
        String password = sc.next();

        //3.判断用户输入的登录名和密码与正确的内容是否相等
        if (okName.equals(name) && okPassword.equals(password)){
            System.out.println("登陆成功~");
        }else {
            System.out.println("用户名或密码错误!");
        }

        //4.忽略大小写比较内容的Api:一般用于比较验证码这样的业务逻辑
        String sysCode = "22AdFh";
        String code1 = "22aDfH";
        System.out.println(sysCode.equals(code1));//false
        System.out.println(sysCode.equalsIgnoreCase(code1));//true
    }
}

1.如果字符串比较应该使用什么方式进行比较,为什么?

  • 使用String提供的equlas方法。
  • 只关心内容一样就返回true。

2.开发中什么时候使用==比较数据

  • 基本数据类型比较时使用。

String常用API如图:

 代码演示如下:

public class StringTest {
    public static void main(String[] args) {
        //1.public int length(): 获取字符串长度
        String name = "我爱你中国love";
        System.out.println(name.length());//9

        //2.public char charAT(int index): 获取某个索引位置处的字符
        char c = name.charAt(1);
        System.out.println(c);//爱

        System.out.println("-----------遍历字符串中的每个字符-----------");
        for (int i = 0; i < name.length(); i++) {
            char ch = name.charAt(i);
            System.out.print(ch);//我爱你中国love
        }
        System.out.println();//换行

        //3.public char[] toCharArray(): 把字符串转换成字符数组
        char[] chars = name.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            System.out.print(ch);//我爱你中国love
        }
        System.out.println();//换行

        //4.public String substring(int beginIndex,int endIndex): 截取内容(包前不包后)
        String name2 = "Java是最厉害的编程语言!";
        String rs = name2.substring(0,9);
        System.out.println(rs);//Java是最厉害的
        String rs1 = name2.substring(4,9);
        System.out.println(rs1);//是最厉害的

        //5.public String substring(int beginIndex): 从当前索引一直截取到末尾
        String rs2 = name2.substring(4);
        System.out.println(rs2);//是最厉害的编程语言!

        //6.public String replace(CharSequence target, CharSequence replacement)
        String name3 = "光头强是最厉害的伐木工,光头强棒棒的!我好爱光头强";
        String rs3 = name3.replace("光头强","***");
        System.out.println(rs3);//***是最厉害的80后,***棒棒的!我好爱***

        //7.public bolean contain(CharSequence s)
        System.out.println(name3.contains("光头强"));//true
        System.out.println(name3.contains("熊大"));//false

        //8.public boolean startsWith(String prefix)
        System.out.println(name3.startsWith("光头强"));//true
        System.out.println(name3.startsWith("光头强是最厉害的"));//true
        System.out.println(name3.startsWith("光头强是最厉害的2"));//false

        //9.public String[] split(String s): 按照某个内容把字符串分割成字符串数组返回。
        String name4 = "王二博,贾奶绿,吴签";
        String[] names = name4.split(",");
        for (int i = 0; i < names.length; i++) {
            System.out.println("选择了:" + names[i]);//选择了:王二博,贾奶绿,吴签
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值