none
Removing parenthesis and its content from String RRS feed

  • Question

  • Greetings,

    This is our source string :  "abcd (test)"

    I need to remove :

    1 - the space behind the parenthesis

    2- the parenthesis and its content 

    So, our target string should be just : "abcd"

    P.S there is always a space before the parenthesis.

    Also Note that "abcd" and the string in the parenthesis  are just examples, so : different strings , different lengths!

    Thanks in advance.



    • Edited by Kevin993 Wednesday, January 1, 2020 3:06 PM
    Wednesday, January 1, 2020 3:04 PM

Answers

  • You can do something like :

    Dim sTextSample As String = "abcd (test)"
    Dim sTextResult As String = Regex.Replace(sTextSample, " \([^()]*\)", String.Empty)

    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:54 PM
    Wednesday, January 1, 2020 3:14 PM
  • Hi

    And another way. Send string to Function and get back starting portion OR full string if not fit to pattern.

      ' Form1 with TexBox1, Label1
    ' and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = GetStart(TextBox1.Text)
      End Sub
      Function GetStart(s As String) As String
        ' PATTERN - must have ( character
        ' preceded by Space character.
        Dim i As Integer = s.IndexOf("("c)
        Dim sp As Boolean = s(i - 1) = " "c
        If i > 0 AndAlso sp Then Return s.Substring(0, i - 1)
        Return s
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:55 PM
    Wednesday, January 1, 2020 3:49 PM
  • If this is needed in more than one place consider an extension method

    Code module

    Imports System.Text.RegularExpressions

    Public Module StringExtensions
        <Runtime.CompilerServices.Extension>
        Public Function RemoveUnwanted(sender As String) As String
            Return Regex.Replace(sender, " ?\(.*?\)", String.Empty)
        End Function
    End Module
    

    Example in a form

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sourceData = New List(Of String) From {"abcd (test)", "111111(Inside out)", "Last"}
    
            For Each item As String In sourceData
                Console.WriteLine($"'{item.RemoveUnwanted()}'")
            Next
    
            Dim results = sourceData.Select(Function(data) data.RemoveUnwanted()).ToList()
            For Each item As String In results
                Console.WriteLine($"'{item.RemoveUnwanted()}'")
            Next
    
            Dim singleItem = "ABC (11111)"
            Console.WriteLine(singleItem.RemoveUnwanted())
        End Sub
    End Class

    Output in the IDE Output window, apostrophes are there to show the actual result

    'abcd'
    '111111'
    'Last'
    'abcd'
    '111111'
    'Last'
    ABC


    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

    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:55 PM
    Wednesday, January 1, 2020 4:46 PM
    Moderator
  • Beside the one which are currently already shown there are probably more than 5 others. 

    One of those a very lazy one. 

    Dim textSample = "abcd (test)"
    Dim result = textSample.Split(" "c)(0)
    For this is the speed is not important as long as it is not on a desktop and this is a billion times done in a minute. 

    (The one from Les is in nanoseconds the fastest, the Regex one and this one the slowest). 

    Be aware, I was not sure if it has to be a split on space( or just cut at the space. 

    the space( can go as well, but then it becomes a VB String split. 


    Success
    Cor


    • Edited by Cor Ligthert Wednesday, January 1, 2020 6:02 PM
    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:55 PM
    Wednesday, January 1, 2020 6:01 PM

  • This is our source string :  "abcd (test)"

    I need to remove :

    1 - the space behind the parenthesis

    2- the parenthesis and its content 

    So, our target string should be just : "abcd"

    P.S there is always a space before the parenthesis.


    Yet another of the many ways to get there:

    Dim source As String = "abcd (test)"
    Dim target As String = source.Remove(source.LastIndexOf(" (")) ' note space before (
    

    - Wayne

    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:55 PM
    Wednesday, January 1, 2020 6:25 PM

All replies

  • You can do something like :

    Dim sTextSample As String = "abcd (test)"
    Dim sTextResult As String = Regex.Replace(sTextSample, " \([^()]*\)", String.Empty)

    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:54 PM
    Wednesday, January 1, 2020 3:14 PM
  • Hi

    And another way. Send string to Function and get back starting portion OR full string if not fit to pattern.

      ' Form1 with TexBox1, Label1
    ' and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label1.Text = GetStart(TextBox1.Text)
      End Sub
      Function GetStart(s As String) As String
        ' PATTERN - must have ( character
        ' preceded by Space character.
        Dim i As Integer = s.IndexOf("("c)
        Dim sp As Boolean = s(i - 1) = " "c
        If i > 0 AndAlso sp Then Return s.Substring(0, i - 1)
        Return s
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:55 PM
    Wednesday, January 1, 2020 3:49 PM
  • If this is needed in more than one place consider an extension method

    Code module

    Imports System.Text.RegularExpressions

    Public Module StringExtensions
        <Runtime.CompilerServices.Extension>
        Public Function RemoveUnwanted(sender As String) As String
            Return Regex.Replace(sender, " ?\(.*?\)", String.Empty)
        End Function
    End Module
    

    Example in a form

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim sourceData = New List(Of String) From {"abcd (test)", "111111(Inside out)", "Last"}
    
            For Each item As String In sourceData
                Console.WriteLine($"'{item.RemoveUnwanted()}'")
            Next
    
            Dim results = sourceData.Select(Function(data) data.RemoveUnwanted()).ToList()
            For Each item As String In results
                Console.WriteLine($"'{item.RemoveUnwanted()}'")
            Next
    
            Dim singleItem = "ABC (11111)"
            Console.WriteLine(singleItem.RemoveUnwanted())
        End Sub
    End Class

    Output in the IDE Output window, apostrophes are there to show the actual result

    'abcd'
    '111111'
    'Last'
    'abcd'
    '111111'
    'Last'
    ABC


    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

    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:55 PM
    Wednesday, January 1, 2020 4:46 PM
    Moderator
  • Beside the one which are currently already shown there are probably more than 5 others. 

    One of those a very lazy one. 

    Dim textSample = "abcd (test)"
    Dim result = textSample.Split(" "c)(0)
    For this is the speed is not important as long as it is not on a desktop and this is a billion times done in a minute. 

    (The one from Les is in nanoseconds the fastest, the Regex one and this one the slowest). 

    Be aware, I was not sure if it has to be a split on space( or just cut at the space. 

    the space( can go as well, but then it becomes a VB String split. 


    Success
    Cor


    • Edited by Cor Ligthert Wednesday, January 1, 2020 6:02 PM
    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:55 PM
    Wednesday, January 1, 2020 6:01 PM

  • This is our source string :  "abcd (test)"

    I need to remove :

    1 - the space behind the parenthesis

    2- the parenthesis and its content 

    So, our target string should be just : "abcd"

    P.S there is always a space before the parenthesis.


    Yet another of the many ways to get there:

    Dim source As String = "abcd (test)"
    Dim target As String = source.Remove(source.LastIndexOf(" (")) ' note space before (
    

    - Wayne

    • Marked as answer by Kevin993 Wednesday, January 1, 2020 6:55 PM
    Wednesday, January 1, 2020 6:25 PM
  • Hi

    And just to complete my offering, the simplest and safest way and needs no conditional tests.

    Takes a string parameter for delimiter 'sp', as well as string to be split 's'

    tested using:
    ' AbCdE(123)      returns AbCdE(123)
    ' AbCdE (123)     returns AbCdE
    ' AbC dE (123)    returns AbC dE
    ' AbC(dE (123)    returns AbC(dE
    ' AbC( dE (123)   returns AbC( dE
    ' AbC (dE (123)   returns AbC

     

    ' Usage

    Label1.Text = GetStart(TextBox1.Text, " (") Function GetStart(s As String, Optional sp As String = " ") As String Return Split(s, sp)(0) End Function


    Wednesday, January 1, 2020 8:17 PM
  • Hi                    (forum playing silly b***** again!)

    And just to complete my offering, the simplest and safest way (that I have found) and needs no conditional tests.

    Takes a string parameter for delimiter 'sp', as well as string to be split 's'

    tested using:
    ' AbCdE(123)      returns AbCdE(123)
    ' AbCdE (123)     returns AbCdE
    ' AbC dE (123)    returns AbC dE
    ' AbC(dE (123)    returns AbC(dE
    ' AbC( dE (123)   returns AbC( dE
    ' AbC (dE (123)   returns AbC

     

    ' Usage

    Label1.Text = GetStart(TextBox1.Text, " (") Function GetStart(s As String, Optional sp As String = " ") As String Return Split(s, sp)(0) End Function


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, January 1, 2020 8:22 PM
    Wednesday, January 1, 2020 8:20 PM
  • Sub Main(args As String()) Dim stringWithoutExtras As String = StringWeWantFrom("abcd (test)") Console.WriteLine(stringWithoutExtras) End Sub Function StringWeWantFrom(stringToSplit) Dim noRight As String = Left(stringToSplit, InStrRev(stringToSplit, " ") - 1) Return noRight End Function


    There can even be a space inside the string you want to capture since the function comes at the string from the right side.
    • Proposed as answer by NasunoR Thursday, January 2, 2020 12:19 AM
    • Edited by NasunoR Thursday, January 2, 2020 12:23 AM
    Thursday, January 2, 2020 12:06 AM