C# 根据字符类型确定子字符串的端点

C# 根据字符类型确定子字符串的端点,c#,C#,对不起,我很难为这篇文章找到一个合适的标题。如果标题不清楚,我道歉 我有很多字符串遵循这样的格式 "Application has been dispatched to the client 5 business day(s) ago. Signed application has not been received" 我想把这个子串增加到数字5 所以结果是 "Application has been dispatched to the client" 但是,我并不总是知道数值前有多少个

对不起,我很难为这篇文章找到一个合适的标题。如果标题不清楚,我道歉

我有很多字符串遵循这样的格式

"Application has been dispatched to the client 5  business day(s) ago. Signed application has not been received"
我想把这个子串增加到数字5

所以结果是

 "Application has been dispatched to the client"
但是,我并不总是知道数值前有多少个字符

某些字符串具有不同的消息,但结构相似。总有一个数字

另一个例子

"Client signed the application 13 day(s) ago."
现在在这个例子中,我想得到以下输出

"Client signed the application"
基本上,我需要一种方法来获取所有内容,直到得到数值

我该怎么做

希望这是明确的,提前感谢

干杯

您可以使用split()字符串将字符串值转换为数组

例如:

var str = "Client signed the application 13 day(s) ago"; 
var res = str.Split(' '); // split when space found
Console.WriteLine(res[0]); // output value when array index at 0
因此,输出应为:

Client 
在此之后,您只需循环数组中的数据,当找到可以转换为整数的字符串数据时,必须停止循环

        int number, index = 0;

        bool result = false; 

        while (result == false)
        {
            Console.WriteLine(res[index]);
            index++;
            result = Int32.TryParse(res[index], out number);
        }
最后,输出应该是

Client 
signed 
the 
application
希望我的回答能帮助你

老实说,你可以使用split()字符串将字符串值转换成数组

例如:

var str = "Client signed the application 13 day(s) ago"; 
var res = str.Split(' '); // split when space found
Console.WriteLine(res[0]); // output value when array index at 0
因此,输出应为:

Client 
在此之后,您只需循环数组中的数据,当找到可以转换为整数的字符串数据时,必须停止循环

        int number, index = 0;

        bool result = false; 

        while (result == false)
        {
            Console.WriteLine(res[index]);
            index++;
            result = Int32.TryParse(res[index], out number);
        }
最后,输出应该是

Client 
signed 
the 
application

希望我的回答能帮助你

听起来你需要一些正则表达式。因为您希望知道数字,所以应该可以:

var regex = new Regex(@" \d+");
var result = input.Substring(0, regex.Match(input).Index));
(假设
input
是您的字符串)。这将从开始到第一个数字(不包括空格)的子字符串


请注意,如果字符串没有数字,则结果将为空。如果希望它在该实例中返回整个字符串,可以使用
If
语句测试匹配索引是否为零。

听起来需要一些正则表达式。因为您希望知道数字,所以应该可以:

var regex = new Regex(@" \d+");
var result = input.Substring(0, regex.Match(input).Index));
(假设
input
是您的字符串)。这将从开始到第一个数字(不包括空格)的子字符串


请注意,如果字符串没有数字,则结果将为空。如果希望它在该实例中返回整个字符串,可以使用
If
语句测试匹配索引是否为零。

我喜欢函数式编程,所以我想先用F#解决这个问题。以下是我的想法:

let findNumber (s : string) =
    let rec loop i =
        if i >= s.Length then -1
        elif Core.char.IsDigit s.[i] then i
        else loop (i + 1)
    loop 0

let truncateAtNumber s =
    match findNumber s with
    | -1 -> s
    | p -> (s.Substring (0, p)).Trim ()
又短又干净。然而,令我惊讶的是,C#翻译实际上更短了(编辑:现在更短了):

static string TruncateAtNumber(此字符串为s)
{
对于(int i=0;i
我喜欢函数式编程,所以我想先用F#解决这个问题。以下是我的想法:

let findNumber (s : string) =
    let rec loop i =
        if i >= s.Length then -1
        elif Core.char.IsDigit s.[i] then i
        else loop (i + 1)
    loop 0

let truncateAtNumber s =
    match findNumber s with
    | -1 -> s
    | p -> (s.Substring (0, p)).Trim ()
又短又干净。然而,令我惊讶的是,C#翻译实际上更短了(编辑:现在更短了):

static string TruncateAtNumber(此字符串为s)
{
对于(int i=0;i
\D
匹配任何非数字字符,
*
匹配它0次或更多次

或者再短一点:

string result = Regex.Split(str, @"\d")[0];
\D
匹配任何非数字字符,
*
匹配它0次或更多次

或者再短一点:

string result = Regex.Split(str, @"\d")[0];

这个问题的答案应该有助于你继续前进。。这个问题的答案应该有助于你前进1.非常感谢你的贡献。我选择正则表达式是因为它的紧凑性。但是你的方法也非常有效。非常感谢你的贡献。我选择正则表达式是因为它的紧凑性。但是你的方法也非常有效。非常感谢你的贡献!由于其紧凑性,我不得不选择另一个答案。但是感谢你的努力!非常感谢你的贡献!由于其紧凑性,我不得不选择另一个答案。但是感谢你的努力!