How to remove white space in a user's input through an input field in Unity?

36 views Asked by At

I'm new to Unity and C# in general, just started using them less than a month ago so I can make a school project. A little background about my project, I'm making a maths game, the specific problem I'm working on is related to taking in the answer of a question through an input box. Since it's maths, each question has only one specific answer, so I thought I could use a regular conditional statement to compare the stored value of the answer with the user's input, that much is fine.

My problem is that I'm considering if someone puts in an input, they'll leave white space at the start or the end or in the middle of the text (for example, if writing a fraction they could write it as "3 / 4" or " 3/4"), and if my statement compares the stored value with their input, it'll say their answer is wrong because there's some white space so it's not the exact same.

I'm aware of the Trim() method in C# but as far as I'm aware it only removes leading and trailing white spaces, nothing in between. Is there an in-built method that allows me to remove white space between characters as well, or is there a way I can do that myself? Thank you very much in advance!

2

There are 2 answers

0
sashok On

You're right, string.Trim removes the spaces before and after, string.TrimStart before and string.TrimEnd after the string.

" my string ".Trim(); // "my string"
" my string ".TrimStart(); // "my string "
" my string ".TrimEnd(); // " my string"

However, this doesn't remove the spaces in the middle of the string.


The easiest way to remove all the spaces is to replace them by nothing using string.Replace method, where the 1st parameter is an old value, and the 2nd a new one.

public String Replace(String oldValue, String newValue);

Here we find all the space characters (" ") and replace them by nothing (""). This returns a string with no spaces at all.

" my string ".Replace(" ", ""); // "mystring"

You can also replace chars instead of strings using Replace overload, even though it doesn't really matter, as the result stays the same.

public String Replace(char oldChar, char newChar);
" my string ".Replace(' ', '\0'); // "mystring"

Additionally, you can implement this kind of method yourself using a for or foreach loop.

private string RemoveSpaces(string value)
{
    string result = string.Empty;

    foreach (char c in value)
    {
        if (c != ' ')
            result += c;
    }
    return result;
}
0
Haney On

I would probably do something like this:

  • Split the input on whitespace characters (we'll use ' ' as an example)
  • Recombine it with just one space as intended.
// This creates an array where each element is a "word" from
// the original string that was separated by white space
var split = myString.Split(' ', StringSplitOptions.RemoveEmptyEntries);
// This creates a new string with one space between each "word"
// that was in the array above
var newString = string.Join(' ', split);

Note that this solution is not optimal and does a lot of allocating etc. but for a video game in Unity that should be fine. You may also want to make the white space detection more robust as there are MANY different white space characters beyond the normal "space" character.