Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中拆分混合字符串#_C# - Fatal编程技术网

C# 如何在c中拆分混合字符串#

C# 如何在c中拆分混合字符串#,c#,C#,下面是示例输入和输出。 我有如下字符串。 我想将字符串的最后一个数字增加1 AVAP001P001输出AVAP001P002 CD009输出CD010这里有一个您可以使用的快速解决方案。您可能想让它更健壮,但我继续添加了适用的注释来描述正在做的事情 static void Main(string[] args) { var s = "CBC004DS009"; // get the very last index of the character that is not a n

下面是示例输入和输出。 我有如下字符串。 我想将字符串的最后一个数字增加1

AVAP001P001输出AVAP001P002


CD009输出CD010

这里有一个您可以使用的快速解决方案。您可能想让它更健壮,但我继续添加了适用的注释来描述正在做的事情

static void Main(string[] args)
{
    var s = "CBC004DS009";

    // get the very last index of the character that is not a number
    var lastNonNumeric = s.LastOrDefault(c => !char.IsDigit(c)); 
    if (lastNonNumeric != '\x0000')
    {
        var numericStart = s.LastIndexOf(lastNonNumeric);

        // grab the number chunk from the string based on the last character found
        var numericValueString = s.Substring(numericStart + 1, s.Length - numericStart - 1);

        // convert that number so we can increment accordingly
        if (int.TryParse(numericValueString, out var newValue))
        {
            newValue += 1;

            // create the new string without the number chunk at the end
            var newString = s.Substring(0, s.Length - numericValueString.Length);

            // append the newly increment number to the end of the string, and pad
            // accordingly based on the original number scheme
            newString += newValue.ToString().PadLeft(numericValueString.Length, '0');

            Console.WriteLine(newString);
        }
    }
}

请向我们展示您尝试过的内容、不起作用的内容以及预期的输出。只有当您有一个模式,可以使用该模式解析字符串的一部分、转换为int、增加并用旧版本替换它时,才有可能。如果你不知道怎么做,我可以回答。我已经创建了两个子字符串,并将第二个子字符串转换成整数,再加上1,如果CD009输出是CD0010,那么输出是错误的,而预期的应该是CD010What的最后一位?数字还是数字?如果是数字9,该怎么办?0? 你能补充一下问题中样品的具体和完整的预期结果吗?还有更多的样本和规则。@PrashantVerma增加一个数字和改变字符串的其他部分是不同的。您的输入与预期不可理解。已经有人要求它显示代码和预期输出,如果您不能,我们可以如何帮助您?感谢d.moncada,它成功了me@PrashantVerma谢谢,如果可以,请接受答案。