VB.NET将文本框中的每个单词添加为列表框中的新项

时间:2015-11-24 21:57:32

标签: vb.net textbox each word

我想将文本框中的每个单词添加为列表框中的新项目并且它有效,但问题是文本框是多行的,因此代码每次都会添加一行中的最后一个单词和下一行中的下一个单词同一个项目。

以下是代码:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim words() As String
    Dim space() As Char = {" "c}
    'split each word by space
    words = TextBox1.Text.Split(words)
    Dim word As String
    For Each word In words
        'Add each word in to Listbox
        ListBox1.Items.Add(word)
    Next
End Sub

我试过了,但它没有用:

Dim words() As String
    Dim space() As Char = {" "c}
    Dim xx As Char = vbNewLine
    'split each word by space
    words = TextBox1.Text.Split(words AndAlso xx)
    Dim word As String
    For Each word In words
        'Add each word in to Listbox
        ListBox1.Items.Add(word)
    Next

Finnaly fiexd:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim words() As String
    Dim space() As Char = {" "c, Microsoft.VisualBasic.vbCr, Microsoft.VisualBasic.vbLf}
    words = TextBox1.Text.Split(space, StringSplitOptions.RemoveEmptyEntries)
    For Each word As String In words
        ListBox1.Items.Add(word)
    Next
End Sub

2 个答案:

答案 0 :(得分:3)

Information:Gradle tasks [:app:generateDebugSources,:app:generateDebugAndroidTestSources, :app:assembleDebug] :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72221Library UP-TO-DATE :app:prepareComAndroidSupportCardviewV72221Library UP-TO-DATE :app:prepareComAndroidSupportMediarouterV72200Library UP-TO-DATE :app:prepareComAndroidSupportMultidex101Library UP-TO-DATE :app:prepareComAndroidSupportRecyclerviewV72221Library UP-TO-DATE :app:prepareComAndroidSupportSupportV42221Library UP-TO-DATE :app:prepareComFacebookAndroidFacebookAndroidSdk420Library UP-TO-DATE :app:prepareComGithubClansFab155Library UP-TO-DATE :app:prepareComGithubKanytuAndroidParallaxRecyclerviewV14Library UP-TO-DATE :app:prepareComGithubKsoichiroAndroidObservablescrollview150Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServices750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAds750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAnalytics750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAppindexing750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAppinvite750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAppstate750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesBase750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesCast750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesDrive750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesFitness750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesGames750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesGcm750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesIdentity750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesLocation750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesMaps750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesNearby750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesPanorama750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesPlus750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesSafetynet750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesWallet750Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesWearable750Library UP-TO-DATE :app:prepareDebugDependencies :app:compileDebugAidl UP-TO-DATE :app:compileDebugRenderscript UP-TO-DATE :app:generateDebugBuildConfig UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets UP-TO-DATE :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources UP-TO-DATE :app:mergeDebugResources UP-TO-DATE :app:processDebugManifest UP-TO-DATE :app:processDebugResources UP-TO-DATE :app:generateDebugSources UP-TO-DATE :app:preDebugAndroidTestBuild UP-TO-DATE :app:prepareDebugAndroidTestDependencies :app:compileDebugAndroidTestAidl UP-TO-DATE :app:processDebugAndroidTestManifest UP-TO-DATE :app:compileDebugAndroidTestRenderscript UP-TO-DATE :app:generateDebugAndroidTestBuildConfig UP-TO-DATE :app:generateDebugAndroidTestAssets UP-TO-DATE :app:mergeDebugAndroidTestAssets UP-TO-DATE :app:generateDebugAndroidTestResValues UP-TO-DATE :app:generateDebugAndroidTestResources UP-TO-DATE :app:mergeDebugAndroidTestResources UP-TO-DATE :app:processDebugAndroidTestResources UP-TO-DATE :app:generateDebugAndroidTestSources UP-TO-DATE :app:processDebugJavaRes UP-TO-DATE :app:compileDebugJava UP-TO-DATE :app:compileDebugNdk UP-TO-DATE :app:compileDebugSources UP-TO-DATE :app:collectDebugMultiDexComponents UP-TO-DATE :app:packageAllDebugClassesForMultiDex UP-TO-DATE :app:shrinkDebugMultiDexComponents UP-TO-DATE :app:createDebugMainDexClassList UP-TO-DATE :app:dexDebug UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: GC overhead limit exceeded Error:Execution failed for task ':app:dexDebug'. >com.android.ide.common.process.ProcessException:org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_80\bin\java.exe''finished with non-zero exit value 3 Information:BUILD FAILED Information:Total time: 4 mins 57.153 secs Information:1 error Information:0 warnings Information:See complete output in console 数组的初始化更改为

space

并且在拆分时添加Dim space() As Char = {" "c, Microsoft.VisualBasic.vbCr, Microsoft.VisualBasic.vbLf} 选项

RemoveEmptyEntries

这将使用回车符和换行符作为分隔符,而words = TextBox1.Text.Split(space, StringSplitOptions.RemoveEmptyEntries) 选项将清除回车符和换行符之间产生的错误空字“结果”以及任何空行的结果文本框的结尾。

答案 1 :(得分:1)

如果您想按空格分割,但是线条返回正在弄乱您,请删除换行符。

words = TextBox1.Text.Replace(chr(10),"").replace(chr(13),"").Split(words)

编辑:

等等,我认为你发布了一些错误的代码。不确定你的工作方式。应该是这样的。

words = TextBox1.Text.Replace(chr(10),"").replace(chr(13),"").Split(" "c)