• java 基础之String


    String

    charAT():提取指定字符
    codePointAt(): 提取缩印字符串转为数字
    codePointBefore(): 提取索引字符串的前一个字符转为数字
    codePointCount(a,b): 返回a,b 之间的Unicode 的个数
    a.compareTo(b) :比较a,b的大小 a>b:1 a=b:0 a<b:-1 返回int类型 (注:只能是string比较)
    compareToIgnoreCase(): 忽略大小写比较
    concat :字符串拼接:(不能指定位置)
    contains(String ):判断是否包含指定字符。 返回bool值 注:只能与string匹配
    String.copyValueOf(arry) ; :char数组转String
    String.copyValueOf(arry,1,1); 第一个offset 第二个count
    boolean endsWith(string) : 判断结尾字符串
    boolean startsWith(string offset) offset可有可无: 判断字符串开端
    boolean equals(object) : 万物皆可比 但方法只能String用
    boolean equalsIngoreCase(String) : 只能与String 比较
    String str = String.format()

    (b:boolean    s:string   c:char  x:16   o:8   d:10进制   e:科学计数    %%:% )

        public static void main(String[] args) {
            String strCom = "i like java";
            String strCom2 = "happy every day ";
            int strLower = strCom.codePointAt(5);
            int strLowerb = strCom.codePointBefore(5);
            int strLowerCount = strCom.codePointCount(2,5);
            System.out.println(strLower);
            System.out.println(strLowerb);
            System.out.println(strLowerCount);
            System.out.println((char)101);
            System.out.println((char)107);
            int  n1 = strCom.compareTo(strCom2);
            System.out.println(n1);
    //        cha,int汇报错
    //        int c1 =1 ,c2 =3;
    //        System.out.println(c1.compareTo(c2));
            System.out.println(strCom.concat(strCom2));
            System.out.println(strCom.contains("java1"));
            char[] arry = {'i','o','k'};
            String str = String.copyValueOf(arry,1,1);
            System.out.println(str);
            boolean b2 = strCom.equals(n1);
            System.out.println(b2);
            boolean b3 = strCom.equalsIgnoreCase(strCom2);
            String formatDemo = String.format("hello %s,my name is %s,my age is %d","world",             注意写法:  %d表示数字
                    "huluwa",19);
            System.out.println(formatDemo);
            String str1 = String.format("%d",400/2);
            String str2 = String.format("%b",3>4);
            System.out.println(str1);
            System.out.println(str2);
    
    }
    

     String  str=   String.format(local i , String format,args);   %te  tb/tB  tA/ta  tc  tY tj   tm  td  ty

     Date date = new Date();
            Locale form = Locale.CHINA;
            String year = String.format(form,"%tY",date);
            String month  = String.format(form,"%tb",date);
            String day = String.format(form,"%td",date);
    
            System.out.println(year+month+day);
    

    getBytes()      str1 = strCom.getBytes("utf-8"); 注意要catch以下  否则会报错               str1 = strCom.getBytes(Charset.defaultCharset());   虚拟机默认的byte序列

            String strCom = "java";
            byte[]  str= strCom.getBytes();
            System.out.println(str);
    
            for (int i = 0;i<str.length;i++)
            {
                System.out.println(str[i]);
            }
        }

    getChars(str第一个索引  strend索引   数组   数组偏移量)

         String strCom = "l Like Java";
            char[] dst = new char[10];
            strCom.getChars(0,10,dst,0);
            for (int i=0;i<dst.length;i++)
            {
                System.out.println(dst[i]);
            }

     hashCode()

          String str = "i happy ";
            int n = str.hashCode();
            System.out.println(n);
            String str1 = " mima123456";
            int n1 = str1.hashCode();
            String str2 = "123456";
            int n2 = str2.hashCode();
            System.out.println(n1);
            System.out.println(n2);

    indexof(  ,offset )   可以有偏移量

        int index = str.indexOf("ha");
            int index1 = str.indexOf(5);
            int index2 = str.indexOf('a');
            System.out.println(index+"+"+index1+"+"+index2);

    string   intern()     类似python  数据池

      String str1 = new String("I LIKE JAVA");
            String str2 = new String("I LIKE JAVA");
            System.out.println(str1.equals(str2));
            System.out.println(str1==str2);
            System.out.println(str1.intern()==str2.intern());
            System.out.println(str1+'+'+str2);

    boolean     isEmpty ()                                  int     lastIndexOf():可以设置formIndex                       .length()

    matches():正则匹配

    offsetByCodePoints();获取索引便宜后指定代码的索引

     String str = "I LIKE JAVA";
            int i = str.offsetByCodePoints(2,3);
            System.out.println(i);
            
            //会报错 索引超出范围
    //        int i = str.offsetByCodePoints(9,3);
    //        System.out.println(i);

    boolean   regionMatches(bool :是否忽略大小写 , 偏移量   ,otherstring  ,otherstroffset   ,len ):测试两个字符串区域是否相等       比较指定两个区域之间字符串是否相当等

       String str = "I LIKE JAVA";
            int i = str.offsetByCodePoints(2,3);
            System.out.println(i);
            String st2 = "hello  world   happy every day ";
            String st3 = "hello  world   happy  day ";
    
           boolean b1 =  str.regionMatches(true,0,st2,0,10);
           boolean b2 =  st2.regionMatches(true,0,st3,0,10);
            System.out.println(b1);
            System.out.println(b2);
        }

    repalce   repalceAll()

    String  str = "i like java everyday 666";
    String str1 = str.replace('a','6');
    String str2 = str.replace("ava","AVA");
    System.out.println(str1);
    System.out.println(str2);


    String str4 = str.replace(" ","WOW!");
    System.out.println(str4);

    string[]    split()       :  注意返回的是      string[ ]    不是char[]

            String str = "公司名称:明日科技!公司所正在城市:长春市。公司电话:84972266";
            String[] liststr = str.split("!|。");     注意 :如果是.需要//.
            String[] liststr1 = str.split(":");
            for (int i=0;i<liststr.length;i++)
            {
                System.out.println(liststr[i]);
    
            }
    
            System.out.println("-----------------------");
            for (int i=0;i<liststr1.length;i++)
            {
                System.out.println(liststr1[i]);
    
            }

       

  • 相关阅读:
    Python基础篇【第十一篇】:正则表达式之计算器
    Python基础篇【第十篇】:正则表达式
    Python基础篇【第九篇】:剖析装饰器
    Python基础篇【第八篇】:剖析递归函数
    Python基础篇【第七篇】:文件操作
    Python基础篇【第六篇】:函数补充
    Python基础篇【第五篇】:sed和函数
    TCP/IP详解之:Ping程序、Traceroute程序
    TCP/IP详解之:ICMP协议
    TCP/IP详解之:ARP协议 和 RARP协议
  • 原文地址:https://www.cnblogs.com/qj696/p/13778701.html
Copyright © 2020-2023  润新知