473,883 Members | 1,623 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 3184
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(B yVal stringToSearch As String, ByVal
startFlag As String, ByVal endFlag As String) As String
Try
Dim mintStartLocati on, mintEndLocation As Int32
Dim mstrFound As String = Nothing
'---Get the start location
mintStartLocati on = stringToSearch. IndexOf(startFl ag)
'---Check that a start location was found and that it isn't at the end of
the string
If (mintStartLocat ion > -1) And (mintStartLocat ion < stringToSearch. Length)
Then
'Search for the end location
mintEndLocation = stringToSearch. IndexOf(endFlag , mintStartLocati on)
'---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(mintS tartLocation)
Else
mstrFound = stringToSearch. Substring(mintS tartLocation, (mintEndLocatio n -
mintStartLocati on))
End If
End If
Return mstrFound
Catch e As Exception
Throw e
End Try
End Function

Public Function ExtractString(B yVal stringToSearch As String, ByVal
startIndex As Int32, ByVal startFlag As String, ByVal endFlag As String) As
String
Try
Dim mintStartLocati on, mintEndLocation As Int32
Dim mstrFound As String = Nothing
'---Get the start location
mintStartLocati on = stringToSearch. IndexOf(startFl ag, startIndex)
'---Check that a start location was found and that it isn't at the end of
the string
If (mintStartLocat ion > -1) And (mintStartLocat ion < stringToSearch. Length)
Then
'Search for the end location
mintEndLocation = stringToSearch. IndexOf(endFlag , mintStartLocati on)
'---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(mintS tartLocation)
Else
mstrFound = stringToSearch. Substring(mintS tartLocation, (mintEndLocatio n -
mintStartLocati on))
End If
End If
Return mstrFound
Catch e As Exception
Throw e
End Try
End Function

Public Function ExtractString(B yVal stringToSearch As String, ByVal
startFlag As String, ByVal endFlags() As String) As String
Try
Dim mintStartLocati on, mintEndLocation , mintTempEndLoca tion As Int32
Dim mstrFound As String = Nothing
Dim mblnEndFound As Boolean = False
'---Get the start location
mintStartLocati on = stringToSearch. IndexOf(startFl ag)
'---Check that a start location was found and that it isn't at the end of
the string
If (mintStartLocat ion > -1) And (mintStartLocat ion < 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
mintTempEndLoca tion = stringToSearch. IndexOf(EndStri ng, mintStartLocati on)
If (mintTempEndLoc ation > -1) And (mintTempEndLoc ation < mintEndLocation )
Then
mintEndLocation = mintTempEndLoca tion
End If
Next
'---Extract the string found
mstrFound = stringToSearch. Substring(mintS tartLocation, (mintEndLocatio n -
mintStartLocati on))
End If
Return mstrFound
Catch e As Exception
Throw e
End Try
End Function

Public Function ExtractString(B yVal stringToSearch As String, ByVal
startIndex As Int32, ByVal startFlag As String, ByVal endFlags() As String)
As String
Try
Dim mintStartLocati on, mintEndLocation , mintTempEndLoca tion As Int32
Dim mstrFound As String = Nothing
Dim mblnEndFound As Boolean = False
'---Get the start location
mintStartLocati on = stringToSearch. IndexOf(startFl ag, startIndex)
'---Check that a start location was found and that it isn't at the end of
the string
If (mintStartLocat ion > -1) And (mintStartLocat ion < 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
mintTempEndLoca tion = stringToSearch. IndexOf(EndStri ng, mintStartLocati on)
If (mintTempEndLoc ation > -1) And (mintTempEndLoc ation < mintEndLocation )
Then
mintEndLocation = mintTempEndLoca tion
End If
Next
'---Extract the string found
mstrFound = stringToSearch. Substring(mintS tartLocation, (mintEndLocatio n -
mintStartLocati on))
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****@ismynam e.com> wrote in message
news:e0******** ***********@new s.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
6576
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 difference between "." and "current()", I found a neat way of doing the same without keys or generate-id(): <xsl:template match="/"> <!-- Selects all "new" elements --> <xsl:for-each select="//Name"> <!-- Display the element -->
6
22761
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? // Remove (first occurence of) sub from src void func(char *src, char *sub) { char *p;
11
5371
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 (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
7
3456
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 example: var re = /some(thing|or other)?.*(n(est)(?:ed)?.*(parens) )/ var text = "There were some nesting parens in the test"; alert (regExpPos (text, re, 3));
2
2008
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 reason now it is not finding ones that are single posts. Here is an example of a header for a single. (Ask the Dust ) - "atd-ftc-repack.nfo" www.ctjes.com (1/1) (1/1) at the end means it is part 1 of a 1 part post. Any help would be...
6
9276
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 = "kelly" with no error // but string s2 = "kelly".Substring(0,200) // results in // ArgumentOutOfRangeException
2
2181
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 integers, but I still get 2 on the output. Here is the code: void IntegerNumber() { int NumberOfIntegers = 0; string SubString; int flag;
53
3445
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 anything like that in c? The closest thing I can find is strchr, but it check for a character,
2
1494
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 am looking for a way to know how many characters the string has from 0 to the 30th occurance of "\n" within the main string I have considered a string.split or indexOf but can't wrap my head around how to determine the character location of the...
0
9932
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9778
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10833
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9559
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
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 we have to send another system
3
3228
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.