0

On my webpage, I have a textarea running at the server, prompting users to add serial numbers. I wan to use the user input to populate an array, a vector, or a stack (in general, a container) in my c# codefile.

I have my text area with attributes: id="serialno_textbox" and runat="server". In my codefile, I currently have a string called serialno = serialno_textbox.Value. I have called a stack called serial_numbers but not initialized them.

<textarea id="serialno_textbox" rows="10" cols="50" runat="server">Enter Serial Numbers separated by spaces</textarea>
string serialno = serialno_textbox.Value;
Stack<string> serial_numbers;

if the user inputs something such as

serialnumber1 serialnumber2 serialnumber3

which is essentially just strings of numbers and letters separated by spaces, then I which the outcome to be that the stack's contents are

Stack<string> serial_numbers = {serialnumber1 serialnumber2 serialnumber3}
  • 1
    [`String.Split()`](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.8) the value on `' '`. The result will be a `string[]`, which you can then use to create the `Stack`: `var serial_numbers = new Stack(stringArray)`. – Tyler Roper Jun 12 '19 at 18:06
  • Possible duplicate of [Convert comma separated list of integers into an array](https://stackoverflow.com/questions/19761210/convert-comma-separated-list-of-integers-into-an-array). Just substitute "space" for "comma". Or [Best way to specify whitespace in a String.Split operation](https://stackoverflow.com/q/6111298/215552) for less thinking :). – Heretic Monkey Jun 12 '19 at 18:19

1 Answers1

2

Yes, but the HTML aspect isn't really important. If you want to take a string of space-separated strings, you can use string#Split and split on the space character (' '); like so:

var split = "first second third".Split(' ');
// ["first", "second", "third"]

Now that you have an array (an IEnumerable), you can simply pass it into the Stack constructor: like so:

var stack = new Stack<string>(split);
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75