《Java从入门到精通(入土)》字符串 中

  • 字符串操作

  1.获取子字符串

  格式:str.substring(int index);

     str.substring(beginIndex,endIndex);

  2.去除空格

    trim()方法去除前导空格和尾部空格。

    格式:str.trim();

  

  

  3.字符串替换

  格式:str.replace(oldchar,newchar);

  

  

  4.判断字符串的开始和结尾

  格式:str.startsWith(String prefix);

    str.endsWith(String prefix);

    ps:返回是boolean值

  5.判断字符串是否相等

    不能直接用==,这样比较的是比较两个字符串地址是否相同。

    格式:str.equals(string otherstr);

       str.equalsIgnoreCase(string otherstr);//忽略大小写比较相等

    

  6.按字典顺序比较两个字符串

  格式:str.compareTo(String otherstr);

  按字典顺序String对象在参数字符串前,返回-1;string对象在参数字符串之后返回1;相等返回0。

  

  7.字母大小写转换

  格式:str.toLowerCase();//转换成小写

        str.toUpperCase();//转换成大写

    

  

  8.字符串分割 

    格式:str.split(string sign);

       str.split(sign,limit);//limit是分割次数

  

猜你喜欢

转载自www.cnblogs.com/xzzheng/p/11070952.html