如何在C#中查找字符串之前和之后的字符串 [英] How to find string after and before character in C#

查看:305
本文介绍了如何在C#中查找字符串之前和之后的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个字符串MP-MS-BL-FO-L2000-W294

i想要替换-L之后出现的数字,直到-W294 ..

For Ex。

-L2000将是-L2500

-L2350将是-L2360



价值来自数据库所以它字符串将是动态的。

我不想使用替换方法因为我的数字动态变化。



先谢谢,



我的尝试:



if(Pcode_Code.Contains( - L) ))

{



}

解决方案

查看RegEx (正则表达式)。



3步骤

- 从-L9999-中提取9999

- 使用循环表获取新值

- 用新值替换旧值



perlre - perldoc.perl.org [ ^ ]

Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]


它并不复杂,除非我们无法准确地告诉你该做什么,因为我们不知道你对新值的规则究竟是什么,你的例子显示了两种不同的可能规则。

但实质上很简单:

找到要替换的数据,生成新值,替换。

您可以使用子字符串,但正则表达式会这样做可能更容易:

 私人正则表达式getContent = 正则表达式( @ (?< =  -  L)。+?(?=  -  W) ); 
private void MyButton_Click( object sender,EventArgs e)
{
string input = MP-MS-BL-FO-L2000-W294\" ;
匹配m = getContent.Match(输入);
string data = m.Value;
// 修改数据
data = XX + data + YY;
//
string output = getContent.Replace(输入,数据);
}


  string  input =   MP-MS-BL-FO-L2000-W294; 
int start = input.IndexOf( -L)+ 2;
int end = input.IndexOf( -W);
var existingValue = input.Substring(start,end - start); // 2000
string valueToReplace = 2500;
input = input.Replace(existingValue,valueToReplace); // MP-MS-BL-FO-L2500-W294


If i having a string "MP-MS-BL-FO-L2000-W294"
i would like to replace digit which cames after -L and till -W294 ..
For Ex.
-L2000 will be -L2500
-L2350 will be -L2360

Value comes from Database so it string will be Dynamic.
I dont want to use Replace method because my digits changes dynamically.

Thanks in Advance,

What I have tried:

if (Pcode_Code.Contains("-L"))
{

}

解决方案

Have a look at RegEx (Regular Expressions).

in 3 steps
- extract the 9999 from "-L9999-"
- use a loopup table for the new value
- replace old value with new one

perlre - perldoc.perl.org[^]
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]


It's not that complex, except we can't tell you exactly what to do because we have no idea what your rules for the new value actually are, and your examples show two different possible rules.
But the essence is simple:
Find the data to replace, generate new value, replace.
You could use substring for this, but a Regex will do it probably easier:

private Regex getContent = new Regex(@"(?<=-L).+?(?=-W)");
private void MyButton_Click(object sender, EventArgs e)
    {
    string input = "MP-MS-BL-FO-L2000-W294";
    Match m = getContent.Match(input);
    string data = m.Value;
    // Modify your data
    data = "XX" + data + "YY";
    //
    string output = getContent.Replace(input, data);
    }


string input = "MP-MS-BL-FO-L2000-W294";
       int start = input.IndexOf("-L")+2;
       int end = input.IndexOf("-W");
       var existingValue  = input.Substring(start, end - start); //2000"
       string valueToReplace = "2500";
       input = input.Replace(existingValue, valueToReplace); //"MP-MS-BL-FO-L2500-W294"


这篇关于如何在C#中查找字符串之前和之后的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆