你如何用'#34;和"来代替a的最后一次出现?

时间:2017-08-23 06:07:42

标签: vb.net

如何使用单词和?替换a的最后一次出现?你能给我一个想法吗?

我有3个复选框,1个富文本框  显示输出,1按钮

(Aparri)或(Camalanuigan)或(Lallo)

Cagayan(Aparri,Camalanuigan)或Cagayan(Aparri,Camalanuigan,Lallo)

我希望输出如下:#Cagayan(Aparri和Camalanuigan)或#Cagayan(Aparri,Camalanuigan和Lallo)

这是我的代码:

 Dim rws As String

        If Aparri.Checked = True Then
            close_parenthesis.Checked = True
            If rws = "" Then
                rws = "(" + Aparri.Text
            End If
        End If

        If Aparri.Checked = False Then
            rws = ""
        End If


        If Camalanuigan.Checked = True Then
            close_parenthesis.Checked = True
            If rws = "" Then
                rws = "(" + Camalanuigan.Text
            Else
                rws = rws & ", " & Camalanuigan.Text
            End If
        End If


        If Lallo.Checked = True Then
            close_parenthesis.Checked = True
            If rws = "" Then
                rws = "(" + Lallo.Text
            Else
                rws = rws & ", " & Lallo.Text
            End If
        End If

 If close_parenthesis.Checked = True Then
            If rws = "" Then

            Else
                rws = rws + close_parenthesis.Text
            End If
        End If
    Display.Text = rws.ToString

输出:( Aparri,Camalanuigan,Lallo)

我希望像这样(Aparri,Camalanuigan和Lallo)

Sample Image

3 个答案:

答案 0 :(得分:2)

您可以使用以下功能替换最后一次出现。

Public Function ReplaceLastOccurrence(ByVal source As String, ByVal searchText As String, ByVal replace As String) As String
    Dim position = source.LastIndexOf(searchText)
    If (position = -1) Then Return source
    Dim result = source.Remove(position, searchText.Length).Insert(position, replace)
    Return result
End Function     

并使用显示文字作为 Display.Text = ReplaceLastOccurence(rws, ",", "and") 在你的最后一行代码中

答案 1 :(得分:2)

在这里,我甚至没有看到你的代码,但是通过查看图片,我得到了你想做的事情。它可以用更短的版本完成,但我已经解释了每一行中发生了什么,所以它很长。

我写了这段代码:

 'let's say the string is "Aparri, Camalanuigan, Lallo" . that's what your code does, right?
dim Strng as string = "Aparri, Camalanuigan, Lallo"

    'now find the position of last appearing ","
    Dim comaposition As Integer
    comaposition = Strng.LastIndexOf(",") 'it is zero  based

    'if not found, it will return -1 and u can exit, no need to do the work
    if commaposition = "-1" then

         exit sub
    end if

    'remove the comma
    Dim String_After_Removing_Comma As String
    String_After_Removing_Comma = Strng.Remove(comaposition, 1)

    'add "and" in the same position where comma was found
    Dim final_string As String
    final_string = String_After_Removing_Comma.Insert(comaposition, " and")

    'show it on the textbox
    DisplayTxt.Text = final_string

您可以在找到最终字符串(代码中的rws)后执行此操作。 希望这有帮助

答案 2 :(得分:0)

你总是可以通过单循环和最后一个索引的知识自己完成

' Create array of selected strings
Dim selectedTexts = 
    New List(Of CheckBox) From { Aparri, Camalanuigan, Lallo }.
                          Where(Function(checkbox) checkbox.Checked).
                          Select(Function(checkbox) checkbox.Text).
                          ToArray()

' Separate selected strings by delimeters
Dim lastIndex = selectedTexts.GetUpperBound(0)
Dim builder = New StringBuilder()
For i As Integer = 0 To lastIndex
    If i > 0 Then
        Dim delimeter = If(lastIndex > 0 AndAlso lastIndex = i, " and ", ", ")
        builder.Append(delimeter)
    End If

    builder.Append(test(i))
Next

' Wrap with parenthesis if result not empty
If builder.Length > 0 Then
    builder.Insert(0, "(")
    Dim close = If(close_parenthesis.Checked, close_parenthesis.Text, "")
    builder.Append(close)
End If

' Print result
Display.Text = builder.ToString()