zoukankan      html  css  js  c++  java
  • java去掉String里面的空格、换行符等

     1 package com.ynet.utils;
     2 
     3 import java.util.regex.Matcher;
     4 import java.util.regex.Pattern;
     5 
     6 /**
     7  * Created by Arya on 2017/11/3 0003.
     8  */
     9 public class StringUtil {
    10     //去除所有空格
    11     public static String replaceAllBlank(String str) {
    12         String s = "";
    13         if (str!=null) {
    14             Pattern p = Pattern.compile("\\s*|\t|\r|\n");
    15             /*\n 回车(\u000a)
    16             \t 水平制表符(\u0009)
    17             \s 空格(\u0008)
    18             \r 换行(\u000d)*/
    19             Matcher m = p.matcher(str);
    20             s = m.replaceAll("");
    21         }
    22         return s;
    23     }
    24     //去除所有空格,留下一个
    25     public static String replaceBlankLeaveOne(String str) {
    26         String s = "";
    27         if (str!=null) {
    28             Pattern p = Pattern.compile("\\s{2,}|\t|\r|\n");
    29             Matcher m = p.matcher(str);
    30             s = m.replaceAll(" ");
    31         }
    32         return s;
    33     }
    34 
    35     public static void main(String[] args) {
    36         System.out.println(StringUtil.replaceAllBlank("just    do     it!"));
    37         System.out.println(StringUtil.replaceBlankLeaveOne("just    do     it!"));
    38     }
    39 
    40 }

    运行效果:

  • 相关阅读:
    新购服务器流程
    nginx代理证书使用方法
    一键部署lnmp脚本
    mysql主从库配置读写分离以及备份
    Linux入门教程(更新完毕)
    Git 工作流程
    Git远程操作
    常用Git命令
    js数组去重
    Sublime Text设置快捷键让html文件在浏览器打开
  • 原文地址:https://www.cnblogs.com/arrrrrya/p/7779846.html
Copyright © 2011-2022 走看看