You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:如何基于数字序列末尾4位数字拆分字符串(非空格拆分)

How to Split a String Based on the Trailing 4-Digit Sequence (Without Splitting on Spaces)

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 Substring to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 09:42:27