vb.net - Vb 2010 how to split (explode) a string -


based on code provided explode string two. code working if searchqurry contains 2 phrases.

private sub searchtxt_previewkeydown(byval sender object, byval e system.windows.forms.previewkeydowneventargs) handles searchtxt.previewkeydown dim searchqurry string = searchtxt.text dim mystring system.string mystring = searchqurry dim outputarray() system.string outputarray = split(mystring, " ", -1) ''msgbox(outputarray(0)) - working  msgbox(outputarray(1)) ' error - index outside bounds of array.  end sub 

an array 0 based (every type of collection is), outputarray(1) tries access second item not first. if contains 1 exception.

instead want: outputarray(0)

if don't know if contains two, check it:

dim first string = outputarray(0) dim second string = nothing if outputarray.length > 1      second = outputarray(1) end if 

as aside, recommend use .net methods, string.split:

dim outputarray string() = mystring.split() ' splits white-spaces, tabs or newlines 

or, if want split spaces:

 dim outputarray string() = mystring.split({" "}, stringsplitoptions.none)  

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -