deanlee17
asked on
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
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
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Nice work, IM! Concise!
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()
you can also use this:
Dim result As String = System.Text.RegularExpressions.Regex.Replace(s, "\s*\(.*\)", "")
ASKER
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?
:)
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?
:)
Here:
System.Text.RegularExpress ions.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!
System.Text.RegularExpress
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!
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.
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.
PS: Which of the answers are giving you the code ("EGC") instead of the name?
ASKER
Sorry, my mistake, they arent, I switched some of my own code around :)
Well they all worked for me in VB.Net 2005 Express (though I haven't tested Shahan's or kaufmed's 2nd one).
Open in new window