c# - How to find the second word in a string -


when run code, it's purpose obtain first, second, , third word in 3 word sentence , have each of words display in text box. sentence use 'kerry tells stories', @ pic see mean,

the problem however, when run it, third word 'stori', happened 'es' making 'stories'

enter image description here

var tbt = textbox.text; var firstword = tbt.substring(0, tbt.indexof(" "));  var indexword = tbt.indexof(" ");  var indexnumber = indexword +1;  string mystring = indexnumber.tostring();     var secondword = tbt.substring(indexnumber, tbt.indexof(" ")); var indexword2 = tbt.indexof(" ", indexnumber);  var indexnumber2 = indexword2 + 1;  string mystring2 = indexnumber2.tostring();   var thirdword = tbt.substring(indexnumber2, tbt.indexof(" ")); var indexword3 = tbt.indexof(" ", indexnumber2); var indexnumber3 = indexword3 + 1;  string mystring3 = indexnumber3.tostring();   textbox6.text = firstword; textbox7.text = secondword; textbox8.text = thirdword; 

where problem?

you can try splitting sentence via string.split() method:

string test = "kelly tell stories"; string[] split = test.split(' '); //use empty space between word(s) split character for(int i=0; i< split.length; i++) {     console.writeline(split[i]); }  console.readline(); 

this yields 3 string elements in one-dimensional array, each element can accessed via index. 0 = first word, 1 = second word... on.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -