我正在做一个项目,我想像下面这样拆分一个字符串
DEV~Hi there is Sunday Tommorrow=1
我想拆分这个字符串,因为我只需要波浪号 (~) 和等号 (=) 之间的文本。我也不需要 ~ 和 = 符号。
我试过下面的代码,但不知道如何用波浪线分割字符串
obj.Code.Trim().Split('=')[1].Trim()
使用上面的代码,我只能将字符串从 = 拆分为符号,但我也想删除左侧的 ~ 符号和文本。
谁能提出解决方案?
最佳答案
假设您的文本只有一个波浪号和一个等号,您可以尝试以下字符串拆分:
string input = "DEV~Hi there is Sunday Tommorrow=1";
var match = Regex.Split(input, @"[~=]")[1];
Console.WriteLine(match); // Hi there is Sunday Tommorrow
另一种方法,可能更稳健一点,是使用正则表达式替换:
string input = "DEV~Hi there is Sunday Tommorrow=1";
string match = Regex.Replace(input, @"^.*~(.*)=.*$", "$1");
Console.WriteLine(match); // Hi there is Sunday Tommorrow
关于c# - 如何使用c#拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67413272/