zoukankan      html  css  js  c++  java
  • JAVA中去掉空格

    String str = " ABCD 
    EFG HIJK
    LMN ";
    1. str.trim()
    是去掉首尾空格
     
    2.str.replace(" """); 
    去掉所有空格,包括首尾、中间
     
    3.str.replaceAll(" ",""); 
    去掉所有空格   
     
    4.str = str.replaceAll("\s*""");   
    可以替换大部分空白字符, 不限于空格
    s 可以匹配空格、制表符、换页符等空白字符的其中任意一个
    public static void main(String[] args) {
        String str = " ABCD 
    EFG HIJK
    LMN ";
        System.out.println("str.trim():
    " + str.trim());
        System.out.println("------分割线------");
        System.out.println("str.replace(" ", ""):
    " + str.replace(" ", ""));
        System.out.println("------分割线------");
        System.out.println("str.replaceAll(" ", ""):
    " + str.replaceAll(" ", ""));
        System.out.println("------分割线------");
        System.out.println("str.replaceAll("\\s*", ""):
    " + str.replaceAll("\s*", ""));
        System.out.println("------分割线------");
    }
    
    打印输出:
    str.trim():
    ABCD 
    EFG HIJK
    LMN
    ------分割线------
    str.replace(" ", ""):
    ABCD
    EFGHIJK
    LMN
    ------分割线------
    str.replaceAll(" ", ""):
    ABCD
    EFGHIJK
    LMN
    ------分割线------
    str.replaceAll("\s*", ""):
    ABCDEFGHIJKLMN
    ------分割线------

      

     
    • 添加到短语集
       
      • 没有此单词集:英语 -> 中文(简体)...
         
      • 创建新的单词集...
    • 拷贝
    • 添加到短语集
       
      • 没有此单词集:英语 -> 中文(简体)...
         
      • 创建新的单词集...
    • 拷贝
    • 添加到短语集
       
      • 没有此单词集:英语 -> 中文(简体)...
         
      • 创建新的单词集...
    • 拷贝
    • 添加到短语集
       
      • 没有此单词集:英语 -> 中文(简体)...
         
      • 创建新的单词集...
    • 拷贝
  • 相关阅读:
    BM求递推式模板
    主席树浅谈
    DSU on Tree浅谈
    树链剖分浅谈
    省选模拟八 题解
    提答题 总结
    交互题 总结
    省选模拟七 题解
    省选模拟六 题解
    省选模拟五 题解
  • 原文地址:https://www.cnblogs.com/MrSong97/p/12626117.html
Copyright © 2011-2022 走看看