有关字符串的一些方法

public static void main(String[] args) {
    //String 常用方法,
    //1.创建String对象
    String a=new String();
    String b="";
    String c=new String("");
    //2.字符串编码转换
    String s="阿弥陀佛";
    try {
        String encodeedS=new String(s.getBytes(),"gb2312");
        System.out.println(encodeedS);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    //3.字符串和char[]
    String cha="123456";//字符串就是char[]
   char[] charArr= cha.toCharArray();//字符串直接转char数组
   String charToString=new String(charArr);
    System.out.println(charToString);
    //4.字符串常用方法
    String str="1".concat(".").concat("1");//
    String str1="1"+"2"+"1"+".";
    String str2="   101.a.".replaceFirst("\\ .","");//替换
    String str3="   101\\a.".replaceAll("\\\\","");
    System.out.println(str3);

    char strCharAt="456".charAt(0); //取数组charAt(0)下标为0的数
    System.out.println(strCharAt);

    //1 左比右大
    // 0  相等
    //  -1  左比右小
    int compareResult="a".compareTo("b");
    System.out.println(compareResult);
    boolean isStartA="a123".startsWith("a1");//从什么字符开始
    boolean isUrl="baidu.com".endsWith(".com");
    String url="http://www.csdn.com";
    boolean hasWWW=url.contains("www");//包含
    if (url.contains("www")&&url.startsWith("http")&&(url.endsWith(".com")||url.endsWith(".cn")||url.endsWith(".net"))){

    }
    if ("ABC123".equalsIgnoreCase("abc123")){
        //忽略大小写比较字符串
    }
    String abc="ABC123".toLowerCase();//字符串转小写
    String upABC="abc123".toUpperCase();//字符串转大写

    if ("acda".indexOf("a")>-1){
        //判断字符在字符串中的位置,从左到右找,找第一个出现的位置
        System.out.println("acda".indexOf("a"));
    }
    if ("acda".lastIndexOf("a")>-1){
        //从右到左找,找第一个出现的位置停止
        System.out.println("acda".lastIndexOf("a"));
    }

    // 判断字符串是否为空
    if (!"123".isEmpty()){
        System.out.println("非空");
    }
    if ("123".length()>0){
        System.out.println("非空");
    }

    //字符串的裁剪拼接
    //substring  重载方法  传截取开始位置
    String aa="在计算机中。所有的数据在储存和运算是都要使用二进制数表示";
    String subResult= aa.substring(aa.indexOf(",")+1);
    //所有的数据在储存和运算是都要使用二进制数表示

    System.out.println(subResult);
    String subResult1= aa.substring(0,aa.indexOf(","));

    System.out.println(subResult1);

    //字符串的拆分
    String lstr="1|2|3|4|5";
    String[] strs= lstr.split("\\|");//按 | 拆分
    System.out.println(Arrays.toString(strs));


    String tri="     sgauhaj      ".trim();//格式化字符串,去掉字符串左右空格
    System.out.println(tri);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值