java 将字符串拆分为几个两个字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4788596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split string into several two character strings
提问by Nikola Despotoski
I have this string 8S8Q4D1SKCQC2C4S6H4C6DJS2S1C6C
我有这个字符串 8S8Q4D1SKCQC2C4S6H4C6DJS2S1C6C
How can I split this string into substrings that consist of 2 characters per substring? I'm confused because I cannot find a delimiter to split them.
如何将此字符串拆分为每个子字符串包含 2 个字符的子字符串?我很困惑,因为我找不到分隔它们的分隔符。
回答by KitsuneYMG
System.out.println(Arrays.toString(
"8S8Q4D1SKCQC2C4S6H4C6DJS2S1C6C".split("(?<=\G.{2})")
));
回答by Mark
Use the substring(int beginIndex, int endIndex) methodof the String class.
使用String 类的substring(int beginIndex, int endIndex) 方法。
回答by nybbler
You will have to do a split like this on your own. You could use something like substring in the following way:
您将不得不自己进行这样的拆分。您可以通过以下方式使用类似 substring 的内容:
public String[] customSplit(String src, int size) {
Vector<String> vec = new Vector<String>();
for(int i = 0; i < src.length(); i += size) {
vec.add(src.substring(i, (i+size > src.length()) ? src.length() : i+size);
}
return vec.toArray();
}
回答by Phil
for (int i = 0; i < a[2].Length; i++)
{
string ss = (8S8Q4D1SKCQC2C4S6H4C6DJS2S1C6C).Substring(i, 2);
Console.Write(ss);
i++;
}