简体   繁体   English

拆分字符串忽略空格

[英]Splitting a string ignoring whitespace

I am splitting a string with a comma as a delimeter using String.split() function in java.我正在使用 java 中的String.split()函数将带有逗号的字符串拆分为分隔符。

Input输入

1,1,87 gandhi road,600005

Output:输出:

1
1
87

The code stops at whitespace.代码在空格处停止。 How do I get it to work ?我如何让它工作?

We'll need to see your code before we can begin troubleshooting.我们需要先查看您的代码,然后才能开始进行故障排除。 However, the following code should work just fine:但是,以下代码应该可以正常工作:

String address = "1,1,87 gandhi road,600005";

String[] stringArray = address.split(",");

for(String str : stringArray)
{
    // Do something with str.
}

This works for me:这对我有用:

String original = "1,1,87 gandhi road,600005";
String[] s = original.split(",");
for (String t : s) {
    System.out.println(t);
}

Output:输出:

1 1
1 1
87 gandhi road甘地路87号
600005 600005

Check the following code:检查以下代码:

public class Main {
    public static void main(String[] args) {
        String[] str = "1,1,87 gandhi road,600005".split(",");
        for (String s : str) {
            System.out.println(s);
        }
    }
}

Output:输出:

1 1
1 1
87 gandhi road甘地路87号
600005 600005

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 拆分空格分隔的字符串,忽略单引号内的空格 - splitting a whitespace separated string, ignoring whitespace within single quotes 为'whitespace`&`和`拆分字符串 - Splitting of string for `whitespace` & `and` 在空格后分割字符串,然后引用它 - Splitting String after whitespace and then referencing it 拆分并分配带有空格的字符串作为分隔符 - Splitting and assigning a string with whitespace as the delimeter 将字符串截断至 n 个字符,忽略空格 - Truncate a String upto n characters ignoring whiteSpace 用于在保留空格的情况下拆分字符串的正则表达式 - Regular expression for splitting a String while preserving whitespace 在编辑距离内查找长字符串中的单词而忽略空格 - Finding words in long string within edit distance ignoring whitespace Java:当单词之间有可变数量的空格时,按空格分割字符串? - Java: Splitting a string by whitespace when there is a variable number of whitespaces between words? 在不中断单词或不忽略任何字符的情况下拆分字符串 - Splitting string without breaking words or ignoring any characters Java在忽略括号之间的任何定界符的同时拆分字符串 - Java splitting a string while ignoring any delimiters between brackets
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM