Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a excel file with a column populated with lots of values. I want to extract only the name from the field, but the problem is though the excel format is the same, the values differ from company to company... as one company's excel column looks as follow

Name S/N-ABC000ED0
Name Surname S/N-ABE00CD00


and the other company

ABC/Name/00
CDF/Name/10


I need to account for both types of format in my application, But I Have no idea how to write this method

C#
public static string CleanUp(string name)
{
....
....

return name
}


I just want to retrieve the NAME part and the Surname (if there is one). But with the format difference, I'm kinda stuck
Posted
Updated 26-Aug-15 10:03am
v2

1 solution

If these are your only two formats then it's easy:

C#
public static string CleanUp(string name)
{
    string result;

    string[] parts = name.Split('/');

    if(parts.Count == 2)
       result = parts[0].TrimRight('S'); //get rid of that 'S'
    else if (parts.Count == 3)
       result = parts[1];
    else
       result = "unable to parse";
 
    //The result will be a name or "unable to parse".  You will still have to parse for surname etc
    return result 
}
 
Share this answer
 
v3
Comments
Richard Deeming 26-Aug-15 9:30am    
You'd probably want to remove the trailing " S" from the end of the string for the first format. :)
Andy Lanng 26-Aug-15 9:33am    
Well I figured that the "Name [Surname] S part would go into a separate parser, but hmm, I think you're right :/

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