• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

Seaching a string

Hi all,

I need to search the following example string (strings will be varying lengths)...

England Garden Centre (EGC)

I need to extract just the name upto the first bracket and I guess we would need to trim incase there was a space betweem 'e' and '(' ?

Many Thanks,
Dean
0
deanlee17
Asked:
deanlee17
  • 5
  • 2
  • 2
  • +2
1 Solution
 
käµfm³d 👽Commented:
Dim result As String = source.Substring(0, source.IndexOf("("c)).Trim()

Open in new window

0
 
tel2Commented:
Hi Dean,

Here's an alternative method (with a probably more old-school coding style) which prevents an error if there is no "(" in the source string:

    Dim source = "England Garden Centre 123"
    Dim hit = InStr(source, "(")
    If hit = 0 Then hit = 999
    Dim result = Trim(Mid(source, 1, hit - 1))

If the source string could be longer than 999 chars, change the "999" to be some number longer than the maximum length it could be.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can also do:
Dim result As String = source.Split("(")(0).Trim

Open in new window

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
tel2Commented:
Nice work, IM!  Concise!
0
 
käµfm³d 👽Commented:
which prevents an error if there is no "(" in the source string:
Fine, if you're going to be a stickler about it  ; )

Dim result As String = source.Substring(0, If(source.IndexOf("("c) < 0, source.Length, source.IndexOf("("c))).Trim()

Open in new window

0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
you can also use this:

Dim result As String = System.Text.RegularExpressions.Regex.Replace(s, "\s*\(.*\)", "")

Open in new window

0
 
deanlee17Author Commented:
Thanks for all the replies. However I was looking to extract the name and not the code, so...

England Garden Centre

and not EGC!

Also, technically, do i give the points to the correct right answer? Or the answer I like the most?

:)
0
 
Shahan AyyubSenior Software Engineer - iOSCommented:
Here:

System.Text.RegularExpressions.Regex.Replace(s, "\s*\(.*\)", "")

          s  = source string

\s*\(.*\)  = pattern

  \s* = here \s looks for whitespace characters and * represents zero or more occurances,
  \(  = '(' is expected character at this position and '\' is escape sequence.
  .* = mactes any character zero or more time
  \)  = ')' is expected character at this position and '\' is escape sequence.

Replace() method will replace this pattern to empty string.

e.g.,

Replace " (EGC)" from "England Garden Centre (EGC)" and leave "England Garden Centre"
if no matches found for text like " (EGC)" no replacement happens and simply text like "England Garden Centre" returns.

hope it helps!
0
 
tel2Commented:
I suggest you use Idle_Mind's, as it is the most consise/simple, IMHO.
    Dim result As String = source.Split("(")(0).Trim
I think that translates: Split the source into parts delimited by "(", then take the 1st part "part #(0)", trim leading/trailing spaces, and return that in result.

I expect all the answers are correct, but you might prefer some to others, so I suggest you split your points accordingly, giving more weight to the answer(s) which were most useful to you.  That's how I try to do it, anyway.
0
 
tel2Commented:
PS: Which of the answers are giving you the code ("EGC") instead of the name?
0
 
deanlee17Author Commented:
Sorry, my mistake, they arent, I switched some of my own code around :)
0
 
tel2Commented:
Well they all worked for me in VB.Net 2005 Express (though I haven't tested Shahan's or kaufmed's 2nd one).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 5
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now