I want to do UI InputField in which you’d enter minimum and maximum amounts, for example 3-7 and then trough script split them into two different integers.
It doesn’t have to be fast by code, but I don’t want it to be too messy either.
I want to do UI InputField in which you’d enter minimum and maximum amounts, for example 3-7 and then trough script split them into two different integers.
It doesn’t have to be fast by code, but I don’t want it to be too messy either.
Something like this should work
string str = //contents of the inputfieldstring;
string[] minmax= str.Split('-');
int min= int.Parse (minmax[0]);
int max= int.Parse (minmax[1]);
You should split your string using regular expressions
string input = "3-7";
string pattern = "-"; // Split on hyphens
string[] substrings = Regex.Split(input, pattern);
Then in your substrings object you should have “3” and “7” which you can easily put back into integers (Int32.Parse(substrings[0]) and Int32.Parse(substrings[1])).
Another simple possiblity is to use Split
with convertAll
. You don’t have to extract and convert each element by yourself. Try like this:
string sample = "3-7";
int[] arr = System.Array.ConvertAll(sample.Split('-'), new System.Converter<string, int>(int.Parse));
Verify using:
foreach (var element in arr) {
Debug.Log(element);
}
Now you can use it like:
Debug.Log(arr[0]); // Prints 3
Debug.Log(arr[1]); // Prints 7