2014年7月13日 星期日

[RESOLVED] finding a specific letter in a string


if (lastName.Contains("V") == true)



I only want to find last names where the last name is like this:

Randy M. Smith V




But it is also finding last names like this:

Joe B. Vance.



How do I only specifically find the suffix in the last name?



I suppose that you could split your string into a series of strings by spaces and check if the last section (your suffix or last name) contained your character using the String.Split() method : 


var yourName = "Randy M. Smith V";

// This will separate each section of your name into an array
var nameSections = yourName.Split(' '); // yields { "Randy", "M.", "Smith", "V" }

// The last section will either be a suffix (if present) or the last name, so you can check if the last
// section contains your string by combining LINQ's Last() method with the String.Contains() method
var doesLastSectionContainV = nameSections.Last().Contains("V");

So you could write a method to handle this :


public bool DoesLastSectionContain(string yourName, string s)
{
// Handles null checking
if(yourName != null && s != null) { return false; }
// If the last section of your string contains your character, then return accordingly
return yourName.Split(' ').Last().Contains(s);
}

which would be used as :


if(DoesLastSectionContain("Randy M. Smith V","V"))
{
// If it contains it, this section will execute
}



Hi,


That is ? You want V to be at the end ? If yes:


if (lastName.EndsWith("V"))


Not directly related but:

- there is not need to use ==true as this expression already returns a boolean

- be aware also that it's always easier to gather data rather than to "split" them ie it is generally better to have "Joe" as a firstName, "B." as the Middle initial and "Vance" as the last name rather than to have "Joe B. Vance" in a single column and then
have to figure out what is where (especially if you have name such as Jean Roger Martin or whatever). Is Jean the first name or Jean Roger and its last name is Martin etc... You have just no way to be sure.



I agree with Patrick regarding the seperating the different components of the names (if appliciable).


It will be much easier to handle things like searching, filtering, sorting and any other common types of data behaviors if you can use all of the name values independently. Additionally, if these are stored on a single object, you could write a DisplayName
property that would combine them all to allow you to access a complete name.



        if ((lastName + " ").Contains("V ") == true)
{

}

or


        if (lastName.Trim().Split(' ').Last() == "V")
{

}



You can also use substring method to get the last character from your lastname string and then get the details accordingly.


Sample Code:


                //Your Last Name
string lastname = "Randy M. Smith V";
//Check if the lastname last character is 'V' or not
if(lastname.Substring(lastname.Length-1, 1) == "V")
{
//If so then get the name

}
else
{
//if not show the error message

}



沒有留言:

張貼留言