java String 常用方法

String方法

class CeShi{
    public static void main(String[] args) {
        //toCharArray
        char chararraryone[]="qwwdwqfva".toCharArray();//变成数组
        print(chararraryone);
        System.out.println("\n");
        //charAt
        //返回所在位置
        char a="swfesgegfag".charAt(2);//f
        System.out.println(a);
        //equalsIgnoreCase
        //比较,忽略大小写
        System.out.println("qdeqwfqf".equalsIgnoreCase("QDEQWFQF"));//true
        //compareTo
//        比较
        System.out.println("qe".compareTo("QE"));//32
        //        lastIndexOf
        //查找存在的位置
        System.out.println("qqewfsacefa".indexOf("e"));//2
        //不存在会返回负一
        //contains
        //查看是否存在
        boolean isb="wjdksalvehello".contains("hello");//true
        System.out.println(isb);
        //startsWith
        boolean isc ="%%%%%lkjicfiesjfv******".startsWith("%%%%%");//true
        System.out.println(isc);
        //endswith
        //同上
        //substring
        //切片
        System.out.println("qwewqrewtewt".substring(1,4));//左开右闭//wew
        System.out.println("dsnfgrwuhgiorwgio".substring(1));//从1到最后//snfgrwuhgiorwgio
        //split
//        分割
        String stingarraryone[]="hello my wwe dcvr".split(" ");
        print(stingarraryone);
        //trim
        //去除两边空格
        System.out.println("  wqewq wqfeq wqrewt    ".trim());//wqewq wqfeq wqrewt
        //toUpperCase,toLowerCase
        //全部大小写
        System.out.println("dsadwdec".toUpperCase());//DSADWDEC
        //replaceAll
        //替换
        System.out.println("aaasdwasfdewfhello".replace("hello","啥"));//aaasdwasfdewf啥
        int [] ari=new int []{1,2,3,4,5,555,432,234,12};
        //排序
        java.util.Arrays.sort(ari);
        print(ari);
        int str[]=new int[3];
        System.out.println("");
        //替换
        System.arraycopy(ari,2,str,0,2);
        print(str);
    }
    //以下是定义的方法,用来打印数组。
    public static void print(char temp[]){
        for(int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
    public static void print(int temp[]){
        for (int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
    }
    public static void print(String temp[]){
        for(int i=0;i<temp.length;i++){
            System.out.print(temp[i]+"\t");
        }
        System.out.println("\n");
        System.out.println("length:\t"+temp.length);
    }
    public static boolean isNum(char temp[]){//判断数字
        for(int i=0;i<temp.length;i++){
            if(temp[i]>'9'||temp[i]<'0'){
                return false;
            }
        }
        return true;
    }
    //定义一个方法首字母大写
    public static String initcap(String temp){
        temp=temp.trim();
        temp=temp.substring(0,1).toUpperCase()+temp.substring(1);
        return temp;
    }
}
  • toCharArray
  • replaceAll
  • toUpperCase,toLowerCase
  • trim
  • split
  • substring
  • endswith,startsWith
  • contains
  • lastIndexOf
  • compareTo
  • equalsIgnoreCase
  • charAt
posted @ 2019-11-11 20:47  sogeisetsu  阅读(221)  评论(0编辑  收藏  举报