none
find char in string RRS feed

  • Question

  • Dim testString As String = "ABCDEFGHIJKL"
    Dim searchChar1 As String = "BC"
    Dim searchChar2 As String = "JK"

    hi

      i went put "BCDEFGHIJK" in textbox but how ?


    • Edited by Sajjad.h Saturday, August 10, 2019 9:07 AM
    Saturday, August 10, 2019 9:04 AM

All replies

  • Hello,

    It's unclear what you want, from what I see it seems like an append to the beginning and end of the string rather than a find Char as there are no Char there e.g. Dim SearchChar1 As Char = "A"c would be a char.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, August 10, 2019 9:37 AM
    Moderator
  • Hi

    Here is some code that willdo EXACTLY as you ask - take note, string case is NOT ignored.

    ' Form1 with
    ' TextBoxes 1-4 and
    ' Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Function GetSubString(testString As String, searchChar1 As String, searchChar2 As String) As String
        Dim i1 As Integer = testString.IndexOf(searchChar1(0))
        Dim i2 As Integer = testString.IndexOf(searchChar1(1))
        Dim result As String = Nothing
        If i2 = i1 + 1 Then
          Dim i3 As Integer = testString.IndexOf(searchChar2(0))
          Dim i4 As Integer = testString.IndexOf(searchChar2(1))
          If i4 = i3 + 1 Then
            result = testString.Substring(i1, i4 - i1 + 1)
          End If
        End If
        Return result
      End Function
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox4.Text = GetSubString(TextBox1.Text, TextBox2.Text, TextBox3.Text)
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Saturday, August 10, 2019 4:20 PM
  • Hi,

    try my code:

    Imports System.Text.RegularExpressions
    
    Public Class Form1
        Dim testString As String = "ABCDEFGHIJKL"
        Dim searchChar1 As String = "BC"
        Dim searchChar2 As String = "JK"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim rg As Regex = New Regex("(?=(" & searchChar1 & "))[.\s\S]*?(?<=(" & searchChar2 & "))", RegexOptions.Multiline Or RegexOptions.Singleline)
            MsgBox(rg.Match(testString).Value)
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 12, 2019 5:14 AM