Click here to Skip to main content
15,885,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"))
{

}
Posted
Updated 20-Jul-16 5:47am
Comments
Karthik_Mahalingam 20-Jul-16 7:03am    
what will be the value from database?
Dnyaneshwar Sable 20-Jul-16 7:13am    
Value will be like : MP-MS-BL-FO-L2000-W294

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.[^]
 
Share this answer
 
Comments
Dnyaneshwar Sable 20-Jul-16 7:14am    
can u plz show me how ?
Patrice T 20-Jul-16 7:16am    
See Solution 2
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:
C#
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);
    }
 
Share this answer
 
C#
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"
 
Share this answer
 
Simple:

C#
string UpdateValueL(string orgStr, int newVal)
{
    return Regex.Replace(orgStr, @"(?<=-L)([0-9]*)(?=-W)", newVal.ToString());
}


Or if you never have two "code blocks" with the same prefix (ex L, W, RD, et cetera), make it able to be used on any "code block".

C#
string UpdateValue(string orgStr, string codeBlockPrefix, int newVal)
{
    // Note: codeBlockPrefix contains the code itself excluding the dash (-) seperator.
    // Examples: "L", "W", "RD"    
    return Regex.Replace(orgStr, @"(?<=-" + codeBlockPrefix + @")([0-9]*)", newVal.ToString());
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900