简体   繁体   中英

How to split a string of numbers on the white space character and convert to integers

I'm working on some homework and I need to get an input from the user which is a single line of numbers separated by spaces. I want to split this string and get the individual numbers out so that I can insert them into a Binary Search Tree.

I tried the split function and was able to rid of the white space but I'm not sure how to "collect" the individual numbers.

        string data;
        string[] newdata = { };
        Console.WriteLine("Please enter a list of integers with spaces 
                           between each number.\n");
        data = Console.ReadLine();
        newdata = data.Split(null);

        Console.WriteLine(String.Join(Environment.NewLine, newdata));

I want to somehow collect the elements from newdata string array and convert them into integers but I'm having a tough time figuring out how to do that.

Well, you could use Linq .Select method combined with .Split method:

List<int> newData = data.Split(' ').Select(int.Parse).ToList();

If you want user to be able to enter empty spaces we need to trim the resulting strings after split. For that we can use another overload of string.Split method that accepts StringSplitOptions :

List<int> newData = data.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();

Finally if you want to allow user to enter incorrect data at times and still get collection of valid ints you could use int.TryParse and filter out values that were parsed incorrectly:

List<int> newData = data.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
                        .Select(s => int.TryParse(s.Trim(), out var n) ? (int?)n : null)
                        .Where(x => x != null)
                        .Select(i => i.Value)
                        .ToList();

OK as code:

var words = data.Split();
int i;
List<int> integers = new List<int>();
foreach(var s in words)
{
  if (int.TryParse(s, out i)) {integers.Add(i);}
}
// now you have a list of integers
// if using decimal, use decimal instead of integer

Some smart LINQ answers are already provided, here is my extended step by step solution which also allows to ignore invalid numbers:

//Read input string
Console.Write("Input numbers separated by space: ");
string inputString = Console.ReadLine();
//Split by spaces
string[] splittedInput = inputString.Split(' ');
//Create a list to store all valid numbers
List<int> validNumbers = new List<int>();
//Iterate all splitted parts
foreach (string input in splittedInput)
{
    //Try to parse the splitted part
    if (int.TryParse(input, out int number) == true)
    {
        //Add the valid number
        validNumbers.Add(number);
    }
}
//Print all valid numbers
Console.WriteLine(string.Join(", ", validNumbers));

You can do as follows.

var numbers = Console.ReadLine();
var listOfNumbers = numbers.Split(new[]{" "},StringSplitOptions.RemoveEmptyEntries)
                           .Select(x=> Int32.Parse(x));

The above lines split the user input based on "whitespace", removing any empty entries in between, and then converts the string numbers to integers.

The StringSplitOptions.RemoveEmptyEntries ensures that empty entries are removed. An example of empty entry would be an string where two delimiters occur next to each other. For example, "2 3 4 5" , there are two whitespaces between 2 and 3,which means, when you are spliting the string with whitespace as delimiter, you end up with an empty element in array. This is eliminated by usage of StringSplitOptions.RemoveEmptyEntries

Depending on whether you are expecting Integers or Decimals, you can use Int32.Parse or Double.Parse (or float/decimal etc)

Furthermore, you can include checks to ensure you have a valid number, otherwise throw an exception. You can alter the query as follows.

var listOfNumbers = numbers.Split(new[]{" "},StringSplitOptions.RemoveEmptyEntries)
                   .Select(x=> 
                    {
                        Console.WriteLine(x);
                        if(Int32.TryParse(x,out var number))
                            return number;
                        else
                            throw new Exception("Element is not a number");
                    });

This ensures all the element in the list are valid numbers, otherwise throw an exception.

保留空格并使用空格“data.split('');”进行“拆分”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM