Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  java  >  正文 java中替换去除字符串中的空格/回车/换行符/制表符

java中替换去除字符串中的空格/回车/换行符/制表符

发布时间:2016-12-02   编辑:www.jquerycn.cn
jquery中文网为您提供java中替换去除字符串中的空格/回车/换行符/制表符等资源,欢迎您收藏本站,我们将为您提供最新的java中替换去除字符串中的空格/回车/换行符/制表符资源
本文章来分享一个利用java中替换去除字符串中的空格/回车/换行符/制表符,有需要的朋友可参考参考。

用String对象的方法replaceAll就可以了!
replaceAll(String regex, String replacement)          
使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。

示例代码:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5832')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5832>

public class T3 {
public static void main(String args[])

String str="aa bb cc";  System.out.println(str.replaceAll(" ", ""));
}}

去掉一个字符串首尾的空格

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5710')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5710>

public class Format {

 public static String trim(String s) {
  int i = s.length();// 字符串最后一个字符的位置
  int j = 0;// 字符串第一个字符
  int k = 0;// 中间变量
  char[] arrayOfChar = s.toCharArray();// 将字符串转换成字符数组
  while ((j < i) && (arrayOfChar[(k j)] <= ' '))
   j;// 确定字符串前面的空格数
  while ((j < i) && (arrayOfChar[(k i - 1)] <= ' '))
   --i;// 确定字符串后面的空格数
  return (((j > 0) || (i < s.length())) ? s.substring(j, i) : s);// 返回去除空格后的字符串
 }

 public static void main(String[] args) {
  String s = trim("  hello  ");
  System.out.println(s);
 }
}

下面再分享一个替换字符串中的所有空格

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6009')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6009>

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @author lei
 * 2011-9-2
 */
public class StringUtils {

 public static String replaceBlank(String str) {
  String dest = "";
  if (str!=null) {
   Pattern p = Pattern.compile("\s*|t|r|n");
   Matcher m = p.matcher(str);
   dest = m.replaceAll("");
  }
  return dest;
 }
 public static void main(String[] args) {
  System.out.println(StringUtils.replaceBlank("just do it!"));
 }
 /*-----------------------------------

 笨方法:String s = "你要去除的字符串";

         1.去除空格:s = s.replace('\s','');

         2.去除回车:s = s.replace('n','');

 这样也可以把空格和回车去掉,其他也可以照这样做。

 注:n 回车(u000a)
 t 水平制表符(u0009)
 s 空格(u0008)
 r 换行(u000d)*/
}

您可能感兴趣的文章:
java中替换去除字符串中的空格/回车/换行符/制表符
php去除字符串换行符实例解析
java删除字符串中的空格、回车、换行符、制表符程序
js去掉字符串的空格或换行符(附相关正则介绍)
php字符串函数有哪些
php压缩html(清除换行符,清除制表符,去掉注释标记)
js 禁止文本框输入空格的代码
php字符串中去除左边第一逗号的方法
js去掉空格的代码
XML Schema 字符串数据类型

[关闭]