?
Solved

Seaching a string

Posted on 2012-08-23
12
Medium Priority
?
482 Views
Last Modified: 2012-09-10
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
Comment
Question by:deanlee17
  • 5
  • 2
  • 2
  • +2
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38327482
Dim result As String = source.Substring(0, source.IndexOf("("c)).Trim()

Open in new window

0
 
LVL 12

Expert Comment

by:tel2
ID: 38327528
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38327791
You can also do:
Dim result As String = source.Split("(")(0).Trim

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:tel2
ID: 38328015
Nice work, IM!  Concise!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38328078
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
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 38328126
you can also use this:

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

Open in new window

0
 

Author Comment

by:deanlee17
ID: 38328167
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
 
LVL 19

Expert Comment

by:Shahan Ayyub
ID: 38328194
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
 
LVL 12

Expert Comment

by:tel2
ID: 38328196
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
 
LVL 12

Expert Comment

by:tel2
ID: 38328199
PS: Which of the answers are giving you the code ("EGC") instead of the name?
0
 

Author Comment

by:deanlee17
ID: 38328201
Sorry, my mistake, they arent, I switched some of my own code around :)
0
 
LVL 12

Expert Comment

by:tel2
ID: 38328212
Well they all worked for me in VB.Net 2005 Express (though I haven't tested Shahan's or kaufmed's 2nd one).
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question