zoukankan      html  css  js  c++  java
  • String的static方法

    //String concat(String str) 拼接字符串

    String concat_str0 = "abc";
    String concat_str1 = "bcd";
    String concat = concat_str0.concat(concat_str1);
    System.out.println("拼接字符串concat:"+concat);//"abc"+"bcd"
    System.out.println("------------------------------");

    //boolean contains(CharSequence s) 判断一个字符串中是否包含某一个小串

    String contains_str0 = "abcdefg";
    String contains_str1 = "cde";
    boolean contains = contains_str0.contains(contains_str1);
    System.out.println("判断一个字符串中是否包含某一个小串contains:"+contains);
    System.out.println("------------------------------");

    //boolean startsWith(String prefix) 判断个字符串是否以另一个字符串开头

    String startsWith_str0 = "abcdefg";
    String startsWith_str1 = "ab";
    boolean startsWith = startsWith_str0.startsWith(startsWith_str1);
    System.out.println("判断个字符串是否以另一个字符串开头startsWith:"+startsWith);
    System.out.println("------------------------------");

    //boolean endsWith(String suffix) 判断个字符串是否以另一个字符串结尾的呢

    //boolean equalsIgnoreCase(String anotherString) 判断 和另一个字符串的内容是否相同(不区分大小写的比较);

    String equalsIgnoreCase_str0 = "abcdefg";
    String equalsIgnoreCase_str1 = "Abcdefg";
    boolean equalsIgnoreCase = equalsIgnoreCase_str0.equalsIgnoreCase(equalsIgnoreCase_str1);
    System.out.println(" 判断 和另一个字符串的内容是否相同(不区分大小写的比较)equalsIgnoreCase:"+equalsIgnoreCase);
    System.out.println("------------------------------");

    //byte[] getBytes()把一个字符变成一个字节数组

    String getBytes_str0 = "abc";
    byte[] bytes = getBytes_str0.getBytes();
    System.out.print("[");
    for(int i = 0; i < bytes.length; ++i)
    {
    System.out.print(bytes[i]+" ");
    }
    System.out.println("]");
    System.out.println("------------------------------");

    //int indexOf(int ch) 在字符串中查找 某个字符第一次出现的索引。

    String indexOf_first_ch0 = "abcdef";
    int indexOf_first_ch = indexOf_first_ch0.indexOf('d');
    System.out.println("在字符串中查找 某个字符第一次出现的索引indexOf:"+indexOf_first_ch);
    System.out.println("------------------------------");

    //int indexOf(int ch, int fromIndex) 从某个位置开始来算, 某个字符第一次出现的索引

    String indexOf_first_from_ch0 = "abcdef";
    int indexOf_first_from_ch = indexOf_first_from_ch0.indexOf('d', 4);
    System.out.println("从某个位置开始来算, 某个字符第一次出现的索引indexOf:"+indexOf_first_from_ch);
    System.out.println("------------------------------");

    //int indexOf(String str) 在字符串中查找 某个小字符串第一次出现的索引。

    String indexOf_first_str0 = "abcdefg";
    int indexOf_first_str = indexOf_first_str0.indexOf("ef");
    System.out.println("在字符串中查找 某个小字符串第一次出现的索引indexOf:"+indexOf_first_str);
    System.out.println("------------------------------");

    //int indexOf(int ch, int fromIndex) 从某个位置开始来算, 某个字符串第一次出现的索引

    String indexOf_first_from_str0 = "abcdef";
    int indexOf_first_from_str = indexOf_first_from_str0.indexOf('d', 4);
    System.out.println("从某个位置开始来算, 某个字符串第一次出现的索引indexOf:"+indexOf_first_from_str);
    System.out.println("------------------------------");

    //int lastIndexOf(int ch) 字符串中查找(倒着查找) 某个小字符串第一次出现的索引。

    String lastIndexOf_str0 = "abcdefg";
    int lastIndexOf = lastIndexOf_str0.lastIndexOf('d');
    System.out.println(" 字符串中查找(倒着查找) 某个小字符串第一次出现的索引lastIndexOf:"+lastIndexOf);
    System.out.println("------------------------------");

    //String replaceAll(String regex, String replacement) 在一个字符串中, 用一个新的小串,把所有的老小串 替换掉,

    String replaceAll_str0 = "abfgcdeffghijfgk";
    String replaceAll = replaceAll_str0.replace("fg", "oo");
    System.out.println("在一个字符串中,用一个新的小串,把所有的老小串 替换掉replaceAll:"+ replaceAll);
    System.out.println("------------------------------");

    //String replaceFirst(String regex, String replacement) 在一个字符串中, 用一个新的小串,第一个出现的老小串 替换掉,

    String replaceFirst_str0 = "abcdfgegfghijk";
    String replaceFirst = replaceFirst_str0.replaceFirst("fg","oo");
    System.out.println("在一个字符串中, 用一个新的小串,第一个出现的老小串 替换掉replaceFirst:"+ replaceFirst);
    System.out.println("------------------------------");

    //String[] split(String regex) 切割字符串

    String split_str0 = "ab,cd,ef,gh";
    String[] split = split_str0.split(",");
    System.out.print("切割字符串split:"+"[ ");
    for(int i = 0; i < split.length; ++i){
    System.out.print(split[i]+" ");
    }
    System.out.println(" ]");
    System.out.println("------------------------------");

    //String substring(int beginIndex) 截取字符串

    String substring_str0 = "abcdefg";
    String substring = substring_str0.substring(2);
    System.out.println("截取字符串substring:"+substring);
    System.out.println("------------------------------");

    //String substring(int beginIndex, int endIndex) 截取字符串从指定位置

    String substring_from_str0 = "abcdefg";
    String substring_from = substring_from_str0.substring(2,5);
    System.out.println("截取字符串指定位置substring:"+substring_from);
    System.out.println("------------------------------");

    //char[] toCharArray() 把自已字符串转换为一个字符数组

    String toCharArray_str0 = "abcdefg";
    char[] toCharArray_chars = toCharArray_str0.toCharArray();
    System.out.print("把自已字符串转换为一个字符数组toCharArray:"+"[ ");
    for(int i = 0; i < toCharArray_chars.length; ++i){
    System.out.print(toCharArray_chars[i]+" ");
    }
    System.out.println("]");
    System.out.println("------------------------------");

    //String toUpperCase() 把字符串转大写

    //String toLowerCase() 把字符串转小写

    String str0 = "AbbcDDef";
    String toUpperCase = str0.toUpperCase();
    String toLowerCase = str0.toLowerCase();
    System.out.println("把字符串转大写toUpperCase:"+toUpperCase);
    System.out.println("------------------------------");
    System.out.println("把字符串转小写toLowerCase:"+toLowerCase);
    System.out.println("------------------------------");

    //String trim() 把字符两端的空格去除掉

    String trim_str0 = " a b c d ";
    String trim = trim_str0.trim();
    System.out.println("把字符两端的空格去除掉trim:"+trim);
    System.out.println("------------------------------");

    //static String valueOf(char[] data) 把任意的东西转换为字符串

    char []valueOf_chars = {'a','b'};
    String valueOf = String.valueOf(valueOf_chars);
    System.out.println("把任意的东西转换为字符串valueOf:"+valueOf);
    System.out.println("------------------------------");

  • 相关阅读:
    Spring MVC的常用注解(一)
    Spring MVC接口实例
    MVC模式和Spring MVC初识
    Hbase数据结构和shell操作
    Hbase的安装和配置
    ZooKeeper安装、配置和使用
    hadoop的安装和配置
    VMware Workstation安装CentOS 7和开发环境
    Java基础-内部类
    SSM三大框架整合
  • 原文地址:https://www.cnblogs.com/maomaodesu/p/11866321.html
Copyright © 2011-2022 走看看