Nameismy
Nameismy

Reputation: 152

Split string input from textbox and convert it to array

I am trying to get the input from a textbox and convert it to string in order to find the longest word using:

string longest = stringArray.OrderByDescending(s => s.Length).First();

For instance I have input in a text box "My sisters lives in UK" I want to have it in an array like ["My" , "sister", "lives", "in", "UK"] in order to use the code above to find the longest string. Thanks

Upvotes: 0

Views: 15366

Answers (3)

Sajeetharan
Sajeetharan

Reputation: 222582

string s= "This is test";
string[] words = s.Split(' ');
var sorted=words.OrderBy(n => n.Length);
var longest = sorted.LastOrDefault();

Upvotes: 1

Justin Harvey
Justin Harvey

Reputation: 14672

string[] stringArray = textBox1.Text.Split(new char[]{' '},  StringSplitOptions.RemoveEmptyEntries);

Upvotes: 0

CodingIntrigue
CodingIntrigue

Reputation: 78525

You just need to split the textbox value by a space:

string[] stringArray = textBox.Text.Split(' ');
string longest = stringArray.OrderByDescending(s => s.Length).First();

Upvotes: 2

Related Questions