求助:如何基于数字序列末尾4位数字拆分字符串(非空格拆分)
Hey there! Let's figure out how to split that string exactly the way you need it. You want to split something like 1-23-4567 Apple Sauce using the trailing 4-digit number as the split point, and avoid splitting on spaces entirely. Here are two straightforward approaches in C# that do just that:
Approach 1: Use Regular Expressions (Clean and Concise)
Regex is perfect here because it can easily locate the last occurrence of 4 consecutive digits in your string, which is exactly what you need for the split point.
using System; using System.Text.RegularExpressions; class StringSplitExample { static void Main() { string input = "1-23-4567 Apple Sauce"; // Match the LAST set of 4 consecutive digits (no more 4-digit sets after it) Match trailingFourDigits = Regex.Match(input, @"\d{4}(?!.*\d{4})"); if (trailingFourDigits.Success) { // Split the string right after the 4-digit sequence string firstPart = input.Substring(0, trailingFourDigits.Index + 4); // Trim leading whitespace from the second part (optional, since we aren't splitting on spaces) string secondPart = input.Substring(trailingFourDigits.Index + 4).TrimStart(); Console.WriteLine($"First segment: {firstPart}"); Console.WriteLine($"Second segment: {secondPart}"); } else { Console.WriteLine("No trailing 4-digit sequence found in the string."); } } }
How this works:
\d{4}: Matches any 4 consecutive digits.(?!.*\d{4}): A negative lookahead that ensures there are no other 4-digit sequences after the matched one—so we only target the last set.- We use
Substringto split the string at the end of the matched 4-digit sequence, avoiding any reliance on spaces.
Approach 2: Manual Traversal (No Regex Needed)
If you prefer to avoid regex, you can loop backward through the string to find the trailing 4-digit sequence manually:
using System; class StringSplitExample { static void Main() { string input = "1-23-4567 Apple Sauce"; int splitPosition = -1; // Start checking from the position where a 4-digit sequence could end at the string's end for (int i = input.Length - 4; i >= 0; i--) { // Check if current position + next 3 are all digits if (char.IsDigit(input[i]) && char.IsDigit(input[i+1]) && char.IsDigit(input[i+2]) && char.IsDigit(input[i+3])) { splitPosition = i + 4; // Split right after the 4-digit sequence break; } } if (splitPosition != -1) { string firstPart = input.Substring(0, splitPosition); string secondPart = input.Substring(splitPosition).TrimStart(); Console.WriteLine($"First segment: {firstPart}"); Console.WriteLine($"Second segment: {secondPart}"); } else { Console.WriteLine("No trailing 4-digit sequence found in the string."); } } }
How this works:
- We start checking from the position 4 characters from the end of the string and move backward.
- For each position, we verify if it and the next 3 characters are all digits—this finds our trailing 4-digit sequence.
- We then split the string at the end of that sequence, just like with the regex approach.
Both methods avoid splitting on spaces entirely and rely solely on the trailing 4-digit sequence to determine the split point.
内容的提问来源于stack exchange,提问作者Bob

