Vb.net 如何替换列表框vbnet中的单词或字符?
嗨,所有我需要删除和替换的字或字符列表框与按钮 例如: 第1章 第1章 第1章 输出 性格 性格Vb.net 如何替换列表框vbnet中的单词或字符?,vb.net,Vb.net,嗨,所有我需要删除和替换的字或字符列表框与按钮 例如: 第1章 第1章 第1章 输出 性格 性格 字符将字符串设置为列表框项目后,可以通过多种方式更改字符串: Imports System.Text Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Use StringBuilder, more efficient f
字符将字符串设置为列表框项目后,可以通过多种方式更改字符串:
Imports System.Text
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Use StringBuilder, more efficient for long strings >200
Dim badString As String = "Char1cter"
Dim aStringBuilder As New StringBuilder(badString)
aStringBuilder.Remove(4, 1)
aStringBuilder.Insert(4, "a")
badString = aStringBuilder.ToString()
MsgBox(badString)
'Use Remove/Insert at specific locations
badString = "Char1cter"
badString = badString.Remove(4, 1).Insert(4, "a")
MsgBox(badString)
'Use .Replace, but first test if there is a 1 anywhere in the string
badString = "Char1cter"
If InStr(badString, "1") Then
badString = badString.Replace("1", "a")
MsgBox(badString)
End If
'Use .Replace w/o testing for a 1
badString = "Char1cter"
badString = badString.Replace("1", "a")
MsgBox(badString)
End Sub
End Class
如果需要循环字符串中的每个字符,可以使用以下方法完成:
Dim newString as String = ""
For Each ch As Char In badString
If ch <> "1" Then
newString &= ch
Endif
If ch = "1" Then
newString &= "a"
Endif
Next
Dim newString as String=“”
对于badString中的每个ch As字符
如果ch“1”,则
新闻字符串&=ch
恩迪夫
如果ch=“1”,则
新闻字符串&=“a”
恩迪夫
下一个
在列表框中的所有项目上循环,插入一个已更改的项目并删除损坏的项目:
For i as Integer = 0 To listBox1.Items.Count - 1
listBox1.Items.Insert(i, listBox1.Items(i).ToString().Replace("char1cter", "character"));
listBox1.Items.RemoveAt(i+1);
Next i
我会选择一种在加载数据时已经纠正它的方法,可能是这种风格的: 您的错误数据出现在某个地方:
Imports System.Collections.Generic
Public Class OriginalDataProvider
Public Shared Iterator Function GetData() As IEnumerable(Of OriginalRecord)
Yield New OriginalRecord(1, "Char1cter")
Yield New OriginalRecord(2, "Seco1d")
Yield New OriginalRecord(27, "Thir1")
End Function
End Class
以及定义记录的类:
Imports System
Public Class OriginalRecord
Public Sub New(id As Int32, label As String)
Me.ID = id
Me.LabelWithErrors = label
End Sub
Public ReadOnly Property ID As Int32
Public ReadOnly Property LabelWithErrors As String
End Class
然后,您需要一个用于更正记录的包装器:
Imports System
Public Class CorrectedRecord
Public Sub New(originalRecord As OriginalRecord)
If (originalRecord Is Nothing) Then Throw New ArgumentNullException(NameOf(originalRecord))
Original = originalRecord
End Sub
Public ReadOnly Property ID As Int32
Get
Return Original.ID
End Get
End Property
Public ReadOnly Property Label As String
Get
'Your sophisticated solution comes here (I just replace "1" through "a" which does not work for all my strings but as I don't have
'a clue what your logic is take it as an example)
Return Original.LabelWithErrors.Replace("1"c, "a"c)
End Get
End Property
Private ReadOnly Property Original As OriginalRecord
End Class
在加载表单事件中,您只需绑定到更正的记录:
Private Sub Form1_Load(sender As Object, args As EventArgs) Handles MyBase.Load
ComboBox1.DataSource = (From e In OriginalDataProvider.GetData() Select New CorrectedRecord(e)).ToArray()
ComboBox1.ValueMember = "ID"
ComboBox1.DisplayMember = "Label"
End Sub
这个答案和这个问题在我看来都很奇怪。您不希望替换下拉列表中的某些字符,而是要替换用作下拉列表源的基础数据中的字符。然后它只是对字符串集合进行字符串操作。
Buff=ListView1.Items(i).Text
不会编译。首先,字符串是字符数组。第二,我们谈论的是一个列表框而不是一个列表视图。不仅仅是字符示例:worldword internetworld示例World Output World Internet示例…所以修改我给出的代码。。我不是读心术的人;我不知道你想要做的所有不同的替换,你的问题也没有提到它们