0
\$\begingroup\$

I tried removing the Unicode Whitespaces in the string using words[len].Trim() but still there's still an extra character.

Every letter represents the coordinate of where the letter is. As you can see there's an extra coordinate/value and it is always located at the end of the string. (dont mind the '# tries') Can you help me Identify what is the extra invisible unicode and what to do in order for it to be removed?

enter image description here

private string[] FisherShuffle(string[] words) {
    int len = words.Length;
    while(len > 0) {
        float index = Mathf.Floor(Random.Range(0f, 1f) * len);
        len--;
        var temp = words[len].Trim();
        words[len] = words[(int)index];
        words[(int)index] = temp;
    }
    return words;
}

UPDATE: I have found out that the ASCII value is 13. I also have tried using TrimEnd('\n','\n') & words[len].TrimEnd(System.Environment.NewLine.ToCharArray()) to no avail

private string[] FisherShuffle(string[] words) {
    int len = words.Length;
    int index;
    string temp;
    while(len > 0) {
        index = (int) Mathf.Floor(Random.Range(0f, len));
        len--;
        //temp = words[len].TrimEnd('\n','\r');
        temp = words[len].TrimEnd(System.Environment.NewLine.ToCharArray());
        words[len] = words[index];
        words[index] = temp;
    }
    return words;
}
\$\endgroup\$
4
  • \$\begingroup\$ You know, instead of float index = Mathf.Floor(Random.Range(0f, 1f) * len) and later casting to int, you could just write int index = Random.Range(0, len) \$\endgroup\$
    – DMGregory
    Commented Nov 23, 2018 at 15:02
  • \$\begingroup\$ Have you tried walking over the characters in the string and printing (int)temp[i] for each one, to learn exactly what character code it is? \$\endgroup\$
    – DMGregory
    Commented Nov 23, 2018 at 15:04
  • \$\begingroup\$ @DMGregory I haven't tried it. Good Idea but I got like 613 words stored in the text passed on parameter. Do you have recommendations for improving functions speed? \$\endgroup\$
    – Shulz
    Commented Nov 24, 2018 at 3:28
  • \$\begingroup\$ @DMGregory Found out that value of ASCII is 13 I also have tried using TrimEnd('\r','\n') but to no avail. \$\endgroup\$
    – Shulz
    Commented Nov 24, 2018 at 7:18

1 Answer 1

1
\$\begingroup\$

The documentation for String.Trim() tells us

White-space characters are defined by the Unicode standard. The Trim() method removes any leading and trailing characters that produce a return value of true when they are passed to the Char.IsWhiteSpace method.

One option is to use the overload String.Trim(Char[]) to specify characters that you aren't expecting. To debug, you could iterate over the string printing each character and the corresponding result of Char.IsWhiteSpace.

Please consider refactor as well.

private string[] FisherShuffle(string[] words) {
    int len = words.Length;
    int i;
    string tmp;

    while(len > 0) {
        i = (int) Random.Range(0, (float) len);
        len--;

        tmp = words[len].Trim();
        words[len] = words[i];
        words[i] = tmp;
    }
    return words;
}
\$\endgroup\$
2
  • \$\begingroup\$ If I would use String.Trim(Char[]) for every letter in the string I don't have to do, String.Trim() ? From what I have read in other posts that String Trim() would any whitespaces from the beginning and end of string. \$\endgroup\$
    – Shulz
    Commented Nov 24, 2018 at 2:11
  • \$\begingroup\$ Please read the documentation which is linked above or google "C# String Trim", etc. \$\endgroup\$ Commented Nov 24, 2018 at 3:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .