public class RemoveSpacesFromString {
public static void main(String[] args) {
// TODO Auto-generated method stub
String newString;
String str = "prashant is good" ;
int i;
char[] strArray = str.toCharArray();
StringBuffer sb = new StringBuffer();
for(i = 0; i<strArray.length; i++)
{
if(strArray[i]!= ' ' && strArray[i]!= '\t')
{
sb.append(strArray[i]);
}
}
System.out.println(sb);
/*newString = str.replaceAll(" " , "");
System.out.println(newString);*/
}
}
27
将每组文本分隔为自己的子字符串,然后连接这些子字符串 . 恩 .
public Address(String street, String city, String state, String zip ) {
this.street = street;
this.city = city;
//Now checking to make sure that state has no spaces...
int position = state.indexOf(" ");
if(position >=0) {
//now putting state back together if it has spaces...
state = state.substring(0, position) + state.substring(position + 1);
}
使用静态方法“ StringUtils.deleteWhitespace(String str) " on your input string & it will return you a string after removing all the white spaces from it. I tried your example string " name=john age=13 year=2001 " & it returned me exactly the string that you wanted - " name=johnage=13year=2001 ” . 希望这可以帮助 .
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RemovingSpace {
/**
* @param args
* Removing Space Using Matcher
*/
public static void main(String[] args) {
String str= "jld fdkjg jfdg ";
String pattern="[\\s]";
String replace="";
Pattern p= Pattern.compile(pattern);
Matcher m=p.matcher(str);
str=m.replaceAll(replace);
System.out.println(str);
}
}
1
你应该用
s.replaceAll("\\s+", "");
代替:
s.replaceAll("\\s", "");
这样,它将在每个字符串之间使用多个空格 . 上述正则表达式中的符号表示“一个或多个\”
2
可以使用Character Class中的isWhitespace函数删除空格 .
public static void main(String[] args) {
String withSpace = "Remove white space from line";
StringBuilder removeSpace = new StringBuilder();
for (int i = 0; i<withSpace.length();i++){
if(!Character.isWhitespace(withSpace.charAt(i))){
removeSpace=removeSpace.append(withSpace.charAt(i));
}
}
System.out.println(removeSpace);
}
0
public static String removeWhiteSpaces(String str){
String s = "";
char[] arr = str.toCharArray();
for (int i = 0; i < arr.length; i++) {
int temp = arr[i];
if(temp!=32 && temp!=9){ // 32 ASCII for space and 9 is for Tab
s+=arr[i];
}
}
return s;
}
29 回答
如果您还需要删除不可破坏的空格,可以像这样升级代码:
使用
mysz.replaceAll("\\s+","");
replaceAll("\\s", "")
怎么样?请参阅here .处理字符串操作的一种方法是来自Apache commons的StringUtils .
你可以找到here . commons-lang包括更多,并得到很好的支持 .
\W
表示"non word character" . 空格字符的模式是\s
. 这在Pattern javadoc中有详细记载 .st.replaceAll("\\s+","")
删除所有空格和不可见字符(例如,tab,\n
) .st.replaceAll("\\s+","")
和st.replaceAll("\\s","")
产生相同的结果 .第二个正则表达式比第一个正则表达式快20%,但随着连续空格数量的增加,第一个正则表达式优于第二个正则表达式 .
如果不直接使用,请将值分配给变量:
如果您更喜欢实用程序类到正则表达式,那么Spring Framework中的StringUtils中有一个方法trimAllWhitespace(String) .
你已经从Gursel Koca那里得到了正确答案,但我相信这不是你真正想做的事情 . 如何解析键值呢?
您可以在不使用replaceAll()或Java中的任何预定义方法的情况下实现此目的 . 这种方式是首选: -
将每组文本分隔为自己的子字符串,然后连接这些子字符串 . 恩 .
使用apache string util类最好避免NullPointerException
Output
你想要的代码是
这将删除所有空白区域 .
首先是空间,第二是空间 .
然后就完成了 .
还有其他空格字符串也存在于字符串中 . 所以我们可能需要从字符串中替换空格字符 .
例如:无休息空间,三维空间,PUNCTUATION空间
这是空格列表http://jkorpela.fi/chars/spaces.html
所以我们需要修改
\ u2004我们为三个空间
s.replaceAll( “[\ u0020 \ u2004]”, “”)
最简单的方法是使用
org.apache.commons.lang3.StringUtils
类的commons-lang3
库,例如“commons-lang3-3.1.jar
” .使用静态方法“
StringUtils.deleteWhitespace(String str)
" on your input string & it will return you a string after removing all the white spaces from it. I tried your example string "name=john age=13 year=2001
" & it returned me exactly the string that you wanted - "name=johnage=13year=2001
” . 希望这可以帮助 .要删除示例中的空格,这是另一种方法:
这样做是将它转换为一个数组,其中空格是分隔符,然后它将数组中的项组合在一起而没有空格 .
它工作得很好,很容易理解 .
你可以这么简单地做到
这个问题最正确的答案是:
我刚从其他答案中修改了这段代码 . 我发布它是因为除了正是所要求的问题之外,它还表明 the result is returned as a new string, the original string is not modified 作为一些暗示的答案 .
(有经验的Java开发人员可能会说“当然,你实际上无法修改字符串”,但这个问题的目标受众可能不知道这一点 . )
试试这个
在java中我们可以做以下操作:
为此,您需要将以下包导入您的程序:
我希望它会对你有所帮助 .
使用Pattern和Matcher它更具动态性 .
你应该用
代替:
这样,它将在每个字符串之间使用多个空格 . 上述正则表达式中的符号表示“一个或多个\”
可以使用Character Class中的isWhitespace函数删除空格 .
这可能有所帮助 .
有很多方法可以解决这个问题 . 你可以使用拆分功能或替换字符串的功能 .
有关更多信息,请参阅smilliar问题http://techno-terminal.blogspot.in/2015/10/how-to-remove-spaces-from-given-string.html
//它适用于任何空格*不要忘记刺痛b中的空间
\w
=任何单词字符\W
=任何不是单词字符的东西(包括标点符号等)\s
=任何空格字符(包括空格,制表符等)\S
=任何不是空格字符的东西(包括字母和数字,以及标点符号等)(编辑:正如所指出的,如果希望
\s
到达正则表达式引擎,则需要转义反斜杠,从而产生\\s
. )