C# 如何在某个字符后获取子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14149875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a substring after certain character
提问by john Gu
i need to extract the company name from an email inside my asp.net mvc web application:-
for exmaple if i have an email address = [email protected]
我需要从我的 asp.net mvc web 应用程序中的电子邮件中提取公司名称:- 例如,如果我有 email address = [email protected]
to get Mycompanyname
with first letter capital?
BR
获得Mycompanyname
首字母大写?BR
采纳答案by Sergey Berezovskiy
string address = "[email protected]";
string name = address.Split('@')[1].Split('.')[0];
name = name.Substring(0,1).ToUpper() + name.Substring(1); // Mycompanyname
Another option to get name is regular expression:
获取名称的另一个选项是正则表达式:
var name = Regex.Match(address, @"@([\w-]+).").Groups[1].Value
回答by FrostyFire
To get rid of the @ and everything before that, you would use something like this in your particular case:
要摆脱 @ 和之前的所有内容,您可以在特定情况下使用类似的内容:
string test = "[email protected]";
test = test.Substring(test.IndexOf('@')+1, test.IndexOf(".") -(test.IndexOf('@')+1));
MessageBox.Show(test);
And thisexplains how to make the first letter a capital, which you would use after you strip out the @ and .com parts.
而这解释了如何使第一个字母大写,你带出来的@和.com部分之后,你会使用。
回答by Esteban Elverdin
Just another variant
只是另一个变种
var name = new MailAddress("[email protected]").Host.Split('.').First();
name = name.First().ToString().ToUpper() + String.Join("", name.Skip(1));
回答by KMC
string email = "[email protected]";
int startIndex = email.IndexOf( "@" );
int endIndex = email.IndexOf( ".", startIndex );
string domain = email.SubString( startIndex + 1, endIndex );
string domain = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(domain);
That will return Mycompanyname
这将返回Mycompanyname