测试String类常用方法

package zuoye;
/**
 *     测试String类常用方法
 * @author 110
 *
 */
public class practice15 {
    public static void main(String[] args) {
        /*String s1 = "core java";
        String s2 = "Core java";
        System.out.println(s1.charAt(3));//提取下标为3的字符
        System.out.println(s2.length());//字符串的长度
        
        System.out.println(s1.equals(s2));//比较两个字符串是否相等
        System.out.println(s1.equalsIgnoreCase(s2));//比较两个字符串(忽略大小写)
        
        System.out.println(s1.indexOf("java"));//字符串s1中是否包含java
        System.out.println(s1.indexOf("apple"));//字符串s1中是否包含apple
        
        String s = s1.replace(' ', '$');//将s1中的空格替换成&;s1没有任何变化,s1是不可变字符串
        System.out.println("result is "+s);
        System.out.println("##############################");*/
        
        String s = "";
        String s1 = "How are you?";
        System.out.println(s1.startsWith("How"));//是否以How开头
        System.out.println(s1.endsWith("you"));//是否以you结尾
        
        
        s=s1.substring(4);//提取字符串:从下标为4的开始到字符串结尾为止
        System.out.println(s);
        s=s1.substring(4,7);//提取子字符串:下标【4,7)不包括7
        System.out.println(s);
        s=s1.toLowerCase();//转小写
        System.out.println(s);
        s=s1.toUpperCase();//转大写
        System.out.println(s);
        String s2 = " How old are you!! ";
        s=s2.trim();//去除字符串首尾的空格。注意:中间的空格不能去掉
        System.out.println(s);
        System.out.println(s2);
    }
}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值