教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java去除字符串空格几种做法

java去除字符串空格几种做法

发布时间:2017-12-08   编辑:jiaochengji.com
教程集为您提供java去除字符串空格几种做法等资源,欢迎您收藏本站,我们将为您提供最新的java去除字符串空格几种做法资源
分享一篇关于java去除字符串空格方法,这是我自己写的项目中一步步找到的,最后有自己需要的,有需要的朋友可以参考一下哈。

超初我用的只是最简单的

网上搜索的都是什么replace(" ","")  感觉很莫名其妙

你使用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('copy2084')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2084>String str = "A B C D E";
str = str.replaceAll(" ", "");

这样就删除空格了,你可能是用了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('copy4399')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4399>

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

public static void replaceBlank()
{
   Pattern p = Pattern.compile(“\s*|t|r|n”);
   String str="I am a, I am Hello ok, n new line ffdsa!";
   System.out.println("before:" str);
   Matcher m = p.matcher(str);
   String after = m.replaceAll("");
   System.out.println("after:" after);
}

public static void main(String[] args) {
     replaceBlank();
   }


上面这代码可以字符串中的空格、回车、换行符、制表符 

我再google一下,这个正好是我要的,因为我不想删除除空格外的内容有只。

<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('copy6722')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6722>

package com.sharell.Info;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DelSpace {
 public static void main(String[] args){
  String str = "  wo   shi  zhong guo     ren    ";
  sameResult(str);
 }
 private static void sameResult(String str) {
  System.out.println(delByPattern(str));
  System.out.println(delByRegex(str));
  System.out.println(delBySB(str));
  System.out.println(delByArr(str));
 }
 public static String delByPattern(String str){
  Pattern p=Pattern.compile(" {2,}");
     Matcher m=p.matcher(str);
     String result=m.replaceAll(" ");
  return result;
 }
 private static String delByRegex(String str) {
  String[] arr = str.split(" ");
  String result = "";
  for(int i = 0;i<arr.length;i ){
   result =arr[i] " ";
  }
  if(!str.endsWith(" ")){
   result=result.substring(0,result.length()-1);
  }
  return result;
 }
 public static String delBySB(String str){
 StringBuffer sb = new StringBuffer(str);
 for(int index = 0;index<sb.length();index ){
  if(index<sb.length()-1&&sb.charAt(index)==' '&&sb.charAt(index 1)==' '){
   sb.deleteCharAt(index 1);
   index--;
  }
 }
 return sb.toString();
    }
 private static String delByArr(String str) {
  char[] arr = str.toCharArray();
  String result = "";
  ArrayList<Character> al = new ArrayList<Character>();
  for(int i=0;i<arr.length;i ){
   if(i<(arr.length-1)&&arr[i]==' '&&arr[i 1]==' '){
    continue;
   }
   else{
    al.add(arr[i]);
   }
  }
  al.trimToSize();
  for(int i=0;i<al.size();i ){
   result =al.get(i);
  }
  return result;
 }
}

您可能感兴趣的文章:
js 禁止文本框输入空格的代码
js去掉空格的代码
js去掉字符串的空格或换行符(附相关正则介绍)
java中替换去除字符串中的空格/回车/换行符/制表符
php5 字符串处理函数汇总
JAVA中去掉字符串空格各种方法详解
python与java用途区别有哪些
java删除字符串中的空格、回车、换行符、制表符程序
php 字符串函数教程与实例代码
python如何判断字符串不为空格

[关闭]
~ ~