有关字符串的一些方法

来源:互联网 发布:阳光网络伴我成长 编辑:程序博客网 时间:2024/04/25 08:07
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);
原创粉丝点击