如何动态分割字符串 [英] How to split a string dynamically

查看:106
本文介绍了如何动态分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个客户名称集合,为
Dileep Kumar Reddy先生
Dileep Varma先生
鲁贝纳·纳阿兹夫人

像这样.我想将Prefix与它们分开.这是

Dileep Kumar Reddy
迪列普·瓦玛
鲁贝纳·纳兹(Rubeena Naaz)

我该怎么做...帮助我

Hi all,
I have a collection of client names as
Mr.Dileep Kumar Reddy
Mr Dileep Varma
Mrs Rubeena Naaz

something like this.I want to seperate Prefix from them.that is

Dileep Kumar Reddy
Dileep Varma
Rubeena Naaz

How can i do this...help me out

推荐答案

此代码在C#中,因为我不太了解VB.NET:

This code is in C# since I don''t know VB.NET that well:

     List<String> inputItems = new List<String>(){"Mr.Dileep Kumar Reddy", "Mr Dileep 
Varma", "Mrs Rubeena Naaz" };
     List<String> outputItems = new List<String>();
     String pattern = @"^(Mr\.|Mr\s|Mrs\s)";
     String replacement = "";
     Regex rgx = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
     foreach(String input in inputItems)
     {
         String result = rgx.Replace(input, replacement);
         outputItems.Add(result);
         Console.WriteLine("Original String: {0}", input);
         Console.WriteLine("Replacement String: {0}", result);
     }
     // Do something with your outputItems list here



我希望您了解得足够多,可以将其转换为VB.NET.如果您有任何疑问,可以给我留言.

问候,

— Manfred



I hope you understand enough to translate this to VB.NET. If you have any questions you can leave me a comment.

Regards,

— Manfred


您可以使用以下代码段:

you can use something like below code snippet :

string strName = "Mr.Dileep Kumar Reddy";
if (strName.StartsWith("Mrs.", StringComparison.CurrentCultureIgnoreCase))
{
    strName = strName.Substring("Mrs.".Length).Trim();
}
else if (strName.StartsWith("Mrs", StringComparison.CurrentCultureIgnoreCase))
{
    strName = strName.Substring("Mrs".Length).Trim();
}
else if (strName.StartsWith("Mr.", StringComparison.CurrentCultureIgnoreCase))
{
    strName = strName.Substring("Mr.".Length).Trim();
}
else if (strName.StartsWith("Mr", StringComparison.CurrentCultureIgnoreCase))
{
    strName = strName.Substring("Mr".Length).Trim();
}



在VB.Net



In VB.Net

Dim strName As String = "Mr.Dileep Kumar Reddy"
If strName.StartsWith("Mrs.", StringComparison.CurrentCultureIgnoreCase) Then
    strName = strName.Substring("Mrs.".Length).Trim()
ElseIf strName.StartsWith("Mrs", StringComparison.CurrentCultureIgnoreCase) Then
    strName = strName.Substring("Mrs".Length).Trim()
ElseIf strName.StartsWith("Mr.", StringComparison.CurrentCultureIgnoreCase) Then
    strName = strName.Substring("Mr.".Length).Trim()
ElseIf strName.StartsWith("Mr", StringComparison.CurrentCultureIgnoreCase) Then
    strName = strName.Substring("Mr".Length).Trim()
End If



[编辑(Matt T Heffron):您必须首先检查作为其他字符串前缀的字符串. (即"Mrs.","Mrs","Mr.","Mr").否则,"Mr"将从"Rubeena Naaz夫人"中删除,留下"s Rubeena Naaz"!
实际上,我可能会使用解决方案4中显示的技术.]



[Edit (Matt T Heffron): You must first check for the strings that are prefixes of other strings. (I.e., "Mrs.", "Mrs", "Mr.", "Mr") or else the "Mr" will get removed from "Mrs Rubeena Naaz" leaving "s Rubeena Naaz" !
Actually, I''d probably use the technique shown in my Solution 4.]


我可能会使用静态前缀数组并循环遍历它们:
I''d probably use a static array of prefixes and loop through them:
// Always order so values that include others as prefix are checked first.
private static readonly string[] StrippablePrefixes = { "Mrs.", "Mrs", "Mr.", "Mr" };
// This would be at the class level.
// It could include other titles: e.g., Dr., Miss, Ms., ...

// To strip any prefix that might be present:
foreach (string prefix in StrippablePrefixes)
{
  if (strName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
  {
    strName = strName.Substring(prefix.Length);
  }
}
strName = strName.Trim();	// Do this here so it happens even if no prefix is removed


这篇关于如何动态分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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