473,497 Members | 2,051 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finding a SubString within a String

Hello,

Using VB and ASP,NET I would like to be able to search a STRING for a
smaller STRING within, based on the characters which appear before and
after.

For example:

String1 = " That was a tasty burger"

How can I return the word Tasty, by referencing the word "a" and
"burger"....

In pseudo style:

Find all characters between " a " and " burger" (note the spaces), and store
as a string

Result:
tasty
Any ideas?

Regards,

Gary.
Mar 31 '06 #1
2 3159
This is what regular expressions are for. There's a quick overview of
match groups and VB.Net at
http://www.regular-expressions.info/dotnet.html

Mar 31 '06 #2
Gary,

I agree with Flinky. Regular Expressions are probably the best way to go.
However here are a few subroutines I wrote a while ago that do it a
different way if you'd like:

Really the very first method is the answer to the example you give. I just
have a few other overloaded methods so that the start and end flags of the
string(s) being searched for may be varied.

Public Function ExtractString(ByVal stringToSearch As String, ByVal
startFlag As String, ByVal endFlag As String) As String
Try
Dim mintStartLocation, mintEndLocation As Int32
Dim mstrFound As String = Nothing
'---Get the start location
mintStartLocation = stringToSearch.IndexOf(startFlag)
'---Check that a start location was found and that it isn't at the end of
the string
If (mintStartLocation > -1) And (mintStartLocation < stringToSearch.Length)
Then
'Search for the end location
mintEndLocation = stringToSearch.IndexOf(endFlag, mintStartLocation)
'---Extract the string found
If mintEndLocation = -1 Then
'---If no end location was found then grab everything from the start to the
end
mstrFound = stringToSearch.Substring(mintStartLocation)
Else
mstrFound = stringToSearch.Substring(mintStartLocation, (mintEndLocation -
mintStartLocation))
End If
End If
Return mstrFound
Catch e As Exception
Throw e
End Try
End Function

Public Function ExtractString(ByVal stringToSearch As String, ByVal
startIndex As Int32, ByVal startFlag As String, ByVal endFlag As String) As
String
Try
Dim mintStartLocation, mintEndLocation As Int32
Dim mstrFound As String = Nothing
'---Get the start location
mintStartLocation = stringToSearch.IndexOf(startFlag, startIndex)
'---Check that a start location was found and that it isn't at the end of
the string
If (mintStartLocation > -1) And (mintStartLocation < stringToSearch.Length)
Then
'Search for the end location
mintEndLocation = stringToSearch.IndexOf(endFlag, mintStartLocation)
'---Extract the string found
If mintEndLocation = -1 Then
'---If no end location was found then grab everything from the start to the
end
mstrFound = stringToSearch.Substring(mintStartLocation)
Else
mstrFound = stringToSearch.Substring(mintStartLocation, (mintEndLocation -
mintStartLocation))
End If
End If
Return mstrFound
Catch e As Exception
Throw e
End Try
End Function

Public Function ExtractString(ByVal stringToSearch As String, ByVal
startFlag As String, ByVal endFlags() As String) As String
Try
Dim mintStartLocation, mintEndLocation, mintTempEndLocation As Int32
Dim mstrFound As String = Nothing
Dim mblnEndFound As Boolean = False
'---Get the start location
mintStartLocation = stringToSearch.IndexOf(startFlag)
'---Check that a start location was found and that it isn't at the end of
the string
If (mintStartLocation > -1) And (mintStartLocation < stringToSearch.Length)
Then
'---Set the end location equal to the end of the string to search
mintEndLocation = stringToSearch.Length
'---Move through each string in the endflag array and set the end flag
location to the closest one.
For Each EndString As String In endFlags
mintTempEndLocation = stringToSearch.IndexOf(EndString, mintStartLocation)
If (mintTempEndLocation > -1) And (mintTempEndLocation < mintEndLocation)
Then
mintEndLocation = mintTempEndLocation
End If
Next
'---Extract the string found
mstrFound = stringToSearch.Substring(mintStartLocation, (mintEndLocation -
mintStartLocation))
End If
Return mstrFound
Catch e As Exception
Throw e
End Try
End Function

Public Function ExtractString(ByVal stringToSearch As String, ByVal
startIndex As Int32, ByVal startFlag As String, ByVal endFlags() As String)
As String
Try
Dim mintStartLocation, mintEndLocation, mintTempEndLocation As Int32
Dim mstrFound As String = Nothing
Dim mblnEndFound As Boolean = False
'---Get the start location
mintStartLocation = stringToSearch.IndexOf(startFlag, startIndex)
'---Check that a start location was found and that it isn't at the end of
the string
If (mintStartLocation > -1) And (mintStartLocation < stringToSearch.Length)
Then
'---Set the end location equal to the end of the string to search
mintEndLocation = stringToSearch.Length
'---Move through each string in the endflag array and set the end flag
location to the closest one.
For Each EndString As String In endFlags
mintTempEndLocation = stringToSearch.IndexOf(EndString, mintStartLocation)
If (mintTempEndLocation > -1) And (mintTempEndLocation < mintEndLocation)
Then
mintEndLocation = mintTempEndLocation
End If
Next
'---Extract the string found
mstrFound = stringToSearch.Substring(mintStartLocation, (mintEndLocation -
mintStartLocation))
End If
Return mstrFound
Catch e As Exception
Throw e
End Try
End Function

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Badass Scotsman" <ba****@ismyname.com> wrote in message
news:e0*******************@news.demon.co.uk...
Hello,

Using VB and ASP,NET I would like to be able to search a STRING for a
smaller STRING within, based on the characters which appear before and
after.

For example:

String1 = " That was a tasty burger"

How can I return the word Tasty, by referencing the word "a" and
"burger"....

In pseudo style:

Find all characters between " a " and " burger" (note the spaces), and
store as a string

Result:
tasty
Any ideas?

Regards,

Gary.

Mar 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6555
by: Victor Engmark | last post by:
When looking for a method to fetch unique elements and counting the number of occurences of each of them, I found quite a lot of gross examples of complex XSL. But after realizing the subtle...
6
22704
by: becte | last post by:
I am little bit confused Is this a legal way of removing a substring from a string? What about the second alternative using strcpy, is it ok even though the source and dest. strings overlap? ...
11
5323
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
7
3421
by: Csaba Gabor | last post by:
I need to come up with a function function regExpPos (text, re, parenNum) { ... } that will return the position within text of RegExp.$parenNum if there is a match, and -1 otherwise. For...
2
1986
by: Extremest | last post by:
Here is the code I have so far. It connects to a db and grabs headers. It then sorts them into groups and then puts all the complete ones into another table. Problem I am having is that for some...
6
9245
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will =...
2
2165
by: jewel87 | last post by:
Hello, I've got a problem and would appreciate any help. I have to count the number of integers in a string like this "1.1 some text. 1.2 some text", where it should return 0, as there are no...
53
3351
by: yinglcs | last post by:
Hi, In java, there is an 'indexOf' function in String which does this: indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. is there...
2
1468
by: Maxington | last post by:
The problem I am left with is that I need to split a string into substrings and determine the character location of the 30th occurance of "\n" string. I have a string that has "\n" within it and I...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.