Java解析包含大量空格的字符串 [英] Java parsing a string with lots of whitespace

查看:95
本文介绍了Java解析包含大量空格的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个空格的字符串,但是当我使用标记器时,它会在所有这些空格处将其分开.我需要令牌来包含这些空格.如何利用 StringTokenizer 返回带有我要拆分的令牌的值?

I have a string with multiple spaces, but when I use the tokenizer it breaks it apart at all of those spaces. I need the tokens to contain those spaces. How can I utilize the StringTokenizer to return the values with the tokens I am splitting on?

推荐答案

您会在 StringTokenizer 的文档中注意到,建议不要将其用于任何新代码,并且String.split(regex) 就是你想要的

You'll note in the docs for the StringTokenizer that it is recommended it shouldn't be used for any new code, and that String.split(regex) is what you want

String foo = "this is      some  data      in   a string";
String[] bar = foo.split("\\s+");

编辑添加:或者,如果您有比简单拆分更大的需求,则使用 PatternMatcher 类来实现更复杂的正则表达式匹配和提取.

Edit to add: Or, if you have greater needs than a simple split, then use the Pattern and Matcher classes for more complex regular expression matching and extracting.

再次如果你想保留你的空间,实际上了解一些正则表达式真的很有帮助:

Edit again: If you want to preserve your space, actually knowing a bit about regular expressions really helps:

String[] bar = foo.split("\\b+");

这将在单词边界上拆分,将每个单词之间的空格保留为 String;

This will split on word boundaries, preserving the space between each word as a String;

public static void main( String[] args )
{
    String foo = "this is      some  data      in   a string";
    String[] bar = foo.split("\\b");
    for (String s : bar)
    {
        System.out.print(s);
        if (s.matches("^\\s+$"))
        {
            System.out.println("\t<< " + s.length() + " spaces");
        }
        else
        {
            System.out.println();
        }
    }
}

输出:

this
        << 1 spaces
is
        << 6 spaces
some
        << 2 spaces
data
        << 6 spaces
in
        << 3 spaces
a
        << 1 spaces
string

这篇关于Java解析包含大量空格的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆