第二次出现特定字符后从字符串中获取子字符串 [英] getting a substring from a string after the 2nd occurrence of a particular character

查看:95
本文介绍了第二次出现特定字符后从字符串中获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,我想从第二次出现某个特定字符后,从字符串中获取一个子字符串.
例如
我有这样的琴弦
sw-100-133
sw-100-300
sw-100-200
sw-1000-200
sw-10-200
现在我想获得仅具有"sw-100"或完全由"sw-100"开头的字符串(不像sw-1000).我怎么得到它.
在此先感谢朋友

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

推荐答案

尝试:
string s = "sw-100-133";
if (s.StartsWith("sw-100-"))
   {
   ...
   }

或者,您可以使用正则表达式:

Or, you could use a regex:

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.



试试这个:
Hi,
Try this:
string input = "sw-100-133";
Match match1 = Regex.Match(input, @"sw-100-.*", RegexOptions.IgnoreCase);
if (match1.Success)
Console.WriteLine(match1.Value);


谢谢


这篇关于第二次出现特定字符后从字符串中获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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