• java String类


    // java.lang.String 
            
    // charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
    char charAt = "abc".charAt(1);
    System.out.println(charAt); // b
    
    // compareTo() 方法用于比较
    int compareTo = "abc".compareTo("abc");
    System.out.println(compareTo); // 0
    
    // compareToIgnoreCase() 方法用于按字典顺序比较两个字符串,不考虑大小写。
    int compareToIgnoreCase = "abc".compareToIgnoreCase("ABC");
    System.out.println(compareToIgnoreCase); // 0
    
    // concat() 方法用于将指定的字符串参数连接到字符串上。
    String concat = "abc".concat("def");
    System.out.println(concat); // abcdef
    
    // contentEquals() 方法用于将此字符串与指定的 StringBuffer 比较。
    StringBuffer sb = new StringBuffer("abc");
    boolean contentEquals = "abc".contentEquals(sb);
    System.out.println(contentEquals); // true
    
    // copyValueOf() 复制,返回指定数组中表示该字符序列的字符串
    String copyValueOf = String.copyValueOf(new char[] { 'h', 'e', 'l', 'l', 'o' });
    System.out.println(copyValueOf); // hello
    
    // endsWith() 方法用于测试字符串是否以指定的后缀结束。
    boolean endsWith = "abc".endsWith("bc");
    System.out.println(endsWith); // true
    
    // equals() 方法用于将字符串与指定的对象比较。
    boolean equals = "abc".equals("abc");
    System.out.println(equals); // true
    
    // equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。
    boolean equalsIgnoreCase = "abc".equalsIgnoreCase("ABC");
    System.out.println(equalsIgnoreCase); // true
    
    // getBytes()  将此 String 编码为 byte 序列
    byte[] getBytes = "abc".getBytes();
    
    // getChars() 方法将字符从字符串复制到目标字符数组。
    char[] newChar = new char[2];
    "abc".getChars(0, 2, newChar, 0);
    System.out.print(newChar); // ab
    
    // indexOf() 方法有以下四种形式:
    //public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
    //public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
    //int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
    //int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
    String string = "aaa456ac";
    //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
    System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
    // 从第四个字符位置开始往后继续查找,包含当前位置  
    System.out.println(string.indexOf("a", 3));//indexOf(String str, int fromIndex); 返回结果:6  
    //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
    // 从头开始查找是否存在指定的字符  
    System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7  
    System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7  
    //从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。  
    System.out.println(string.indexOf(97, 3));//indexOf(int ch, int fromIndex); 返回结果:6  
    System.out.println(string.indexOf('a', 3));//indexOf(int ch, int fromIndex); 返回结果:6  
    
    // intern() 方法返回字符串对象的规范化表示形式。
    // http://www.runoob.com/java/java-string-intern.html
    
    // length() 方法用于返回字符串的长度。
    System.out.println("abc".length()); // 3
    
    // matches() 方法用于检测字符串是否匹配给定的正则表达式。
    System.out.println("abc".matches("(.*)b(.*)")); // true
    
    // regionMatches() 方法用于检测两个字符串在一个区域内是否相等。
    System.out.println("abc".regionMatches(1, "bc", 0, 2)); // true
    
    // replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。
    System.out.println("abc".replace('a', 'A')); // Abc
    
    // split() 方法根据匹配给定的正则表达式来拆分字符串。
    String[] split = "a,b,c".split(",");
    System.out.println(ArrayUtils.toString(split)); // {a,b,c}
    
    // startsWith() 方法用于检测字符串是否以指定的前缀开始。
    System.out.println("abc".startsWith("ab")); // true
    
    // subSequence() 方法返回一个新的字符序列,它是此序列的一个子序列。
    System.out.println("abc".subSequence(0, 2)); // ab
    
    // toCharArray() 方法将字符串转换为字符数组。
    char[] charArray = "abc".toCharArray();
    System.out.println(charArray); // abc
    
    // toLowerCase() 方法将字符串转换为小写。
    System.out.println("ABC".toLowerCase()); // abc
    
    // toString() 方法返回此对象本身(它已经是一个字符串)。
    System.out.println("abc".toString()); // abc
    
    // toUpperCase() 方法将字符串小写字符转换为大写。
    System.out.println("abc".toUpperCase()); // ABC
    
    // trim() 方法用于删除字符串的头尾空白符。
    System.out.println(" abc ".trim()); // abc
    
    // valueOf(primitive data type x) 返回给定data type类型x参数的字符串表示形式。
    System.out.println(String.valueOf((int) 123)); // 123
    
    // 参考:http://www.runoob.com/java/java-string.html
    // 中文文档:http://www.runoob.com/manual/jdk1.6/java/lang/String.html
    // 官方文档:https://docs.oracle.com/javase/8/docs/api/
  • 相关阅读:
    解决clickonce不支持administer权限问题
    好好了解一下Cookie(强烈推荐)
    Cookie的存储读取删除修改 (cookie.Expires读取永远是零时间)
    14VUE插槽
    13VUE非父子组件传值
    1VUE学习方法
    11VUE监听原生事件
    10VUE,组件参数校验,组件3
    9,Vue组件2
    8.VUE计数器,基于组件
  • 原文地址:https://www.cnblogs.com/ooo0/p/9278106.html
Copyright © 2020-2023  润新知