• java String 常用方法


    String方法

    class CeShi{
        public static void main(String[] args) {
            //toCharArray
            char chararraryone[]="qwwdwqfva".toCharArray();//变成数组
            print(chararraryone);
            System.out.println("
    ");
            //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]+"	");
            }
        }
        public static void print(int temp[]){
            for (int i=0;i<temp.length;i++){
                System.out.print(temp[i]+"	");
            }
        }
        public static void print(String temp[]){
            for(int i=0;i<temp.length;i++){
                System.out.print(temp[i]+"	");
            }
            System.out.println("
    ");
            System.out.println("length:	"+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
  • 相关阅读:
    ASP.NET WEB API构建基于REST风格
    .net平台下深拷贝和浅拷贝
    Visual Studio 必备神器
    sql注入
    proxy
    职场人生
    JSch
    何时用继承,何时用组合
    视频码率,帧率和分辨率的联系与差别
    超过响应缓冲区限制
  • 原文地址:https://www.cnblogs.com/sogeisetsu/p/11838338.html
Copyright © 2020-2023  润新知