I want to split a string in 2 characters without any delimiter, but Regex split is not working properly
here is my code:-
String str="splitstring";
System.out.println("Split.."+str.trim().split("(?<=\\G.{2})").length);
System.out.println("Split.."+str.trim().split("(?<=\\G.{2})")[0]);
System.out.println("Split.."+str.trim().split("(?<=\\G.{2})")[1]);
output:-
Split..2
Split..sp
Split..litstring
Looks like it's a bug with the result threshold limit in your Java environment. Try to workaround it by providing the limit explicitly:
String str="splitstring";
int partsCount = (str.length() + 1) / 2;
System.out.println("Split.."+str.trim().split("(?<=\\G.{2})", partsCount).length);
System.out.println("Split.."+str.trim().split("(?<=\\G.{2})", partsCount)[0]);
System.out.println("Split.."+str.trim().split("(?<=\\G.{2})", partsCount)[1]);
我假设你想把字符串分成两个相等的一半,子串..那么这可能会有所帮助!!
System.out.println("Split.."+str.trim().split("(?=\\D)")[0]);
Why do you use a regex for this?
You can do something like this:
String str = "splitstring";
System.out.println("String-length: " + str.length());
for (int i = 0; i < str.length(); i += 2) { // Increments of 2
System.out.print("Split.." + str.charAt(i));
if (i != str.length() - 1) {
System.out.print(str.charAt(i + 1));
}
System.out.println();
}
Output:
String-length: 11
Split..sp
Split..li
Split..ts
Split..tr
Split..in
Split..g
EDIT: If you insist of using the regex though, here it is:
final String str = "splitstring";
System.out.println(Arrays.toString(
str.split("(?<=\\G.{2})")
));
Output remains the same as above. Seems like there is indeed a bug or something, since the regex is the same as in your question.. Can't really say for sure though, since it's not really my expertise.
EDIT 2: Jon Skeet has an alternative method that is more efficient than mine above :
Well, it's fairly easy to do this by brute force:
public static List<String> splitEqually(String text, int size) { // Give the list the right capacity to start with. You could use an array // instead if you wanted. List<String> ret = new ArrayList<String>((text.length() + size - 1) / size); for (int start = 0; start < text.length(); start += size) { ret.add(text.substring(start, Math.min(text.length(), start + size))); } return ret; }
I don't think it's really worth using a regex for this.
EDIT: My reasoning for not using a regex:
- This doesn't use any of the real pattern matching of regexes. It's just counting.
- I suspect the above will be more efficient, although in most cases it won't matter
- If you need to use variable sizes in different places, you've either got repetition or a helper function to build the regex itself based on a parameter - ick.
- The regex provided in another answer firstly didn't compile (invalid escaping), and then didn't work. My code worked first time. That's more a testament to the usability of regexes vs plain code, IMO.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.