zerodowned

Moderator
I found this: http://stackoverflow.com/questions/6754609/match-partial-string-in-liststring-in-another-liststring

But I'm wondering if there's an easier way to check if a string is a partial match to player's name
rather than having to create a list every time it's called upon.

What I'm wanting to do is add a partial match to a custom PM system. So that gives you an idea of how often
this function would be used.

Code:
usedCSS = List<string>
CSS = List<string>

foreach (var str in usedCSS)
        {
            // FirstOrDefault finds first match or returns default (null for string) if not found.
            var match = CSS.FirstOrDefault(s => s.StartsWith(str));

            if (match != null)

                // get the match. 
                Response.Write(match + "<br />");
            }
            else
            {
                Response.Write("Could not find: " + str + "<br />");
            }
        }
 
The code example provided doesn't give much context, same in the link, and doesn't seem to reflect what you want to do.

Can you explain what yo're trying to do (the bigger plan), without using those examples?

Are you trying to match one or multiple player names?
What is the source of the list of players? World.Mobiles?
 
source is World.Mobiles.Values

my main issue is how to use a lambda expression correctly to check if the string name is a partial match to otherPlayer.Name ?

Code:
 foreach (Mobile otherPlayer in World.Mobiles.Values)
            {
                if (otherPlayer is PlayerMobile && otherPlayer != m && otherPlayer.Name != null  )
                {   
                    if( otherPlayer.Name.StartsWith( s => s.otherPlayer.Name(name) ) )
                        _multimatch.Add( otherPlayer );

Cannot convert lambda expression to type 'string' because it is not a delegate type
 
Code:
public static IEnumerable<PlayerMobile> FindPlayersByName( string name )
{
	return World.Mobiles.Values.OfType<PlayerMobile>( )
		 .Where( m => Insensitive.Contains( m.Name, name ) );
}

Code:
foreach( var player in FindPlayersByName( name ) )
{
}

Iterating over World.Mobiles is going to be expensive if it's called too often.
You may want to consider adding a static List<PlayerMobile> to PlayerMobile that contains all instances of PlayerMobile, and use that for your source list instead.

For more ways to handle Player Names, this may be relevant;
https://github.com/Vita-Nex/Core/tree/master/Services/PlayerNames
 
Thank you, that works perfectly, I changed Insensitive.Contains to Insensitive.StartsWith.
Just a personal preference but I can see why Contains would be better in some situations.
 
Back