分割字符串 - 笛卡尔方式
给定以下字符串:
"foo bar-baz-zzz"
我想在字符“”和“-”处拆分它,保留它们的值,但获取输入的所有组合。
我想得到一个二维数组,其中包含
{{"foo", "bar", "baz", "zzz"}
,{"foo bar", "baz", "zzz"}
,{"foo", "bar-baz", "zzz"}
,{"foo bar-baz", "zzz"}
,{"foo", "bar", "baz-zzz"}
,{"foo bar", "baz-zzz"}
,{"foo", "bar-baz-zzz"}
,{"foo bar-baz-zzz"}}
Java中有没有内置方法可以这样分割字符串?也许在像 Apache Commons 这样的图书馆中?或者我必须写一堵for循环墙吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个有效的递归解决方案。我使用
List
而不是二维数组来使事情变得更容易。代码有点难看,可能需要整理一下。>
示例输出:
代码:
Here is a recursive solution that works. I used a
List<List<String>>
rather than a 2-dimensional array to make things easier. The code is a bit ugly and could probably be tidied up a little.Sample output:
Code:
这是一个更短的版本,以递归风格编写。我很抱歉只能用 Python 编写它。我喜欢它的简洁;这里肯定有人能够制作 Java 版本。
结果:
Here's a much shorter version, written in a recursive style. I apologize for only being able to write it in Python. I like how concise it is; surely someone here will be able to make a Java version.
and the result:
这是一个将延迟返回分割值列表的类:(
请原谅代码格式 - 这是为了避免嵌套滚动条)。
对于示例调用:
...它将发出:
我想可以改进实现。
Here is a class that will lazily return lists of split values:
(Forgive the code formatting - it is to avoid nested scrollbars).
For the sample invocation:
...it will emit:
I imagine the implementation could be improved upon.
你为什么需要那个?
请注意,对于给定的 N 个标记字符串,您想要获取大约 N*2^N 个字符串的数组。如果不以安全的方式完成,这(可能)会消耗大量内存......
我想您可能需要遍历所有内容,对吧?如果是这样,最好创建一些类来保留原始字符串,并在每次询问时为您提供不同的分割行的方式。这样您将节省大量内存并获得更好的可扩展性。
Why do you need that?
Notice that for a given string of N tokens you want to get an array of ca N*2^N strings. This (can) consume tons of memory if not done in a safe way...
I guess that probably you will need to iterate trough it all, right? If so than its better to create some class that will keep the original string and just give you different ways of splitting a row each time you ask it. This way you will save tons of memory and get better scalability.
没有库方法。
为了实现这一点,您应该通过保留分隔符来标记字符串(在您的情况下使用“-”),然后您应该将分隔符视为与二进制标志相关联,并根据标志的值构建所有组合。
在你的例子中,你有 3 个分隔符:“”、“-”和“-”,所以你有 3 个二进制标志。您最终会在字符串中得到 2^3 = 8 个值。
There is no library method.
To accomplish that, you should tokenize the string (in your case using " -") by preserving the separators, and then you should think of separators as associated to binary flags and build all combination based on the value of the flags.
In your case, you have 3 separators: " ", "-" and "-", so you have 3 binary flags. You will end up with 2^3 = 8 values in the string.