Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends ,i want to get a substring from a string after the 2nd occurrence of a particular character.
for example
i have strings like this
sw-100-133
sw-100-300
sw-100-200
sw-1000-200
sw-10-200
now i want to get strings only having 'sw-100' or by starting by 'sw-100' exactly (not like sw-1000). how can i get it .
thanks in advance friends
Posted

Try:
C#
string s = "sw-100-133";
if (s.StartsWith("sw-100-"))
   {
   ...
   }
Or, you could use a regex:
C#
Regex myRegex = new Regex(@"sw\-\d\d\d[^\d].*");
if (myRegex.IsMatch((s))
   {
   ...
   }
The second solution is more flexible, but may be harder to maintain.
 
Share this answer
 
Hi,
Try this:
C#
string input = "sw-100-133";
Match match1 = Regex.Match(input, @"sw-100-.*", RegexOptions.IgnoreCase);
if (match1.Success)
Console.WriteLine(match1.Value);

Thanks
 
Share this answer
 

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