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

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

C#
     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


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

Regards,
— Manfred
 
Share this answer
 
v3
Comments
Akinmade Bond 17-Oct-12 10:16am    
I don't get your pattern. Unrecognised escape sequence.
Matt T Heffron 17-Oct-12 16:59pm    
He forgot the leading @ on the string:
String pattern = @"^(Mr\.|Mr\s|Mrs\s)";
Manfred Rudolf Bihy 17-Oct-12 22:10pm    
Thanks for catching that one! :)
fjdiewornncalwe 17-Oct-12 17:05pm    
+5. Very nice.
Maciej Los 18-Oct-12 17:18pm    
Agree, my 5!
you can use something like below code snippet :

C#
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();
}


In VB.Net

VB
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


[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.]
 
Share this answer
 
v5
Comments
DileepkumarReddy 17-Oct-12 7:45am    
but sometimes Mr will be in capitals as MR.what to do then
Bhushan Shah1988 19-Oct-12 3:00am    
than try to check it with converting in into UPPER Case.
I'd probably use a static array of prefixes and loop through them:
C#
// 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
 
Share this answer
 
In VB.NET you can use following command to split a sentence in to words :

dim NAME as string="Mr.Dileep Kumar Reddy"

Use replace command to remove dot from NAME like

NAME = replace(NAME ,"."," ")
Dim w_ords() As String = Split(dr("NAME"))

This way u can separate the prefix i.e. salutation.
Please check array w_ords(0).
 
Share this answer
 
v2
Comments
Akinmade Bond 17-Oct-12 10:32am    
What?

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