zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    java 设计模式之———单例模式
    java 中的 23 种开发模式(转)
    Java 简单的 socket 编程入门实战
    蓝桥杯比赛java 练习《立方变自身》
    蓝桥杯比赛关于 BFS 算法总结方法以及套路分析
    蓝桥杯比赛javaB组练习《生日蜡烛》
    C语言中调用运行python程序
    解决:执行python脚本,提示错误:/usr/bin/python^M: 解释器错误: 没有那个文件或目录。
    webRTC中回声消除(AEC)模块编译时aec_rdft.c文件报错:
    VMware下Linux虚拟机访问本地Win共享文件夹
  • 原文地址:https://www.cnblogs.com/sogeisetsu/p/11838338.html
Copyright © 2011-2022 走看看