Jump to content

getPlayerObjectFromName


torquish

Recommended Posts

I am trying to get a Player's object from the specified name. What I have currently only works when the name doesn't contain a space. What am I doing wrong?

    private Player getPlayerObjectFromName(String name) {
        List<Player> players = getPlayers().filter(player -> player != null && player.getName().equalsIgnoreCase(name));

        if (players != null) {
            for (Player p : players) {
                if (p.getName().equalsIgnoreCase(name)) {
                    return p;
                }
            }
        }
        return null;
    }

Edit: So I figured out the issue. Player objects use (char) 160 as a space which is why I was never getting a match when testing a name with (char) 32 as its space.

Edited by torquish
Link to comment
Share on other sites

23 minutes ago, torquish said:

I am trying to get a Player's object from the specified name. What I have currently only works when the name doesn't contain a space. What am I doing wrong?


    private Player getPlayerObjectFromName(String name) {
      	return getPlayers().closest(player -> player.getName().replaceAll(“\\h”,  “).equalsIgnoreCase(name));
    }

Edit: So I figured out the issue. Player objects use (char) 160 as a space which is why I was never getting a match when testing a name with (char) 32 as its space.

Can’t check if that works as I’m on phone atm, but the idea is those weird characters should be in java’s horizontal whitespace character class, but not in the general whitespace class \s.

P.S. Replace the double quote characters, I don’t have that on iphone keyboard

  • Like 1
Link to comment
Share on other sites

4 minutes ago, Token said:

Can’t check if that works as I’m on phone atm, but the idea is those weird characters should be in java’s horizontal whitespace character class, but not in the general whitespace class \s.

P.S. Replace the double quote characters, I don’t have that on iphone keyboard

Yes it works. Thanks.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...