473,882 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vb.net - finding a substring in a string

2 New Member
Hi,
I m currently working in VB .NET 2003.
I have the following string:
sBIG = "A final Goodbye from Greats"
I need to find whether the above word contains any word say "from".

I could not find a method in VB.net that can provide me the result.
Thanks,
Jul 10 '07 #1
7 43256
TRScheel
638 Recognized Expert Contributor
Hi,
I m currently working in VB .NET 2003.
I have the following string:
sBIG = "A final Goodbye from Greats"
I need to find whether the above word contains any word say "from".

I could not find a method in VB.net that can provide me the result.
Thanks,
I assume you want to check each word?

If so...

Expand|Select|Wrap|Line Numbers
  1. For i As Integer = 0 To sBig.Split(" ").Length - 1 Step 1
  2.     If (sBig.Split(" ")(i).ToLower().Contains("from")) Then
  3.             Console.WriteLine("Word #{0} '{1}' Contains 'from'", i + 1, sBig.Split(" ")(i))
  4.         End If
  5. Next
  6.  
Jul 10 '07 #2
programmerboy
84 New Member
I assume you want to check each word?

If so...

Expand|Select|Wrap|Line Numbers
  1. For i As Integer = 0 To sBig.Split(" ").Length - 1 Step 1
  2.     If (sBig.Split(" ")(i).ToLower().Contains("from")) Then
  3.             Console.WriteLine("Word #{0} '{1}' Contains 'from'", i + 1, sBig.Split(" ")(i))
  4.         End If
  5. Next
  6.  
I think he just wants to check word "from". You can just say

Expand|Select|Wrap|Line Numbers
  1. if (index = sBig.indexOf("from")) <> -1 then
  2. //Do whatever you want to do
  3. end if
indexOf returns -1 if the specified string is not found.
Jul 10 '07 #3
TRScheel
638 Recognized Expert Contributor
I think he just wants to check word "from". You can just say

Expand|Select|Wrap|Line Numbers
  1. if (index = sBig.indexOf("from")) <> -1 then
  2. //Do whatever you want to do
  3. end if
indexOf returns -1 if the specified string is not found.
Rereading his question, you may be right
Jul 10 '07 #4
Plater
7,872 Recognized Expert Expert
If you don't care where in the string the word is, just if it exists or not you could use:
Expand|Select|Wrap|Line Numbers
  1. if (aBig.Contains("From"))
  2. {//the word "From" appears in aBig
  3. }
  4.  
Jul 10 '07 #5
zion99
2 New Member
Thanks a lot for ur replies.
But I m afraid, i cud not find CONTAINS for STRING, it exist for STRINGCOLLECTIO N... :(

so i will be using the INDEX method. but even this method tells if there is an occurrence or not.

what if i want to know the TOTAL no. of occurrences of the word "FROM" in the statement ?
Thanks :)
Jul 11 '07 #6
TRScheel
638 Recognized Expert Contributor
Thanks a lot for ur replies.
But I m afraid, i cud not find CONTAINS for STRING, it exist for STRINGCOLLECTIO N... :(

so i will be using the INDEX method. but even this method tells if there is an occurrence or not.

what if i want to know the TOTAL no. of occurrences of the word "FROM" in the statement ?
Thanks :)
Expand|Select|Wrap|Line Numbers
  1. while(myString.IndexOf("from") != -1)
  2. {
  3.     myString = myString.SubString(myString.IndexOf("from"), myString.Length - myString.IndexOf("from"));
  4.     countOfFrom++;
  5. }
  6.  
I didnt test this code, but it should work. If it gives you an array exception for going out of bounds, add a -1 to the final parameter on SubString
Jul 11 '07 #7
OliveOyl3471
8 New Member
Hi,
I m currently working in VB .NET 2003.
I have the following string:
sBIG = "A final Goodbye from Greats"
I need to find whether the above word contains any word say "from".

I could not find a method in VB.net that can provide me the result.
Thanks,

Here's one way that will work
Expand|Select|Wrap|Line Numbers
  1.  
  2.         Dim sBIG As String = "A final Goodbye from Greats"
  3.         Dim intIndex As Integer
  4.  
  5.         intIndex = sBIG.IndexOf("from", 0)
  6.  
  7.         If intIndex <> -1 Then
  8.             lblDisplay.Text = "Your word is contained in this string"
  9.  
  10.         End If
  11.  
this code will search for whatever word you put in the parentheses after IndexOf.
If the compiler doesn't find your word in the string, then it will assign a value of -1 to the variable intIndex.

It is case sensitive so you could convert it to upper if you need to search for a word that might or might not contain capital letters.

Now, do you know how to populate a two dimensional array from a text file?

p.s. I didn't see that others have already answered your question.
Jul 12 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
2091
by: Glenn Fleishman | last post by:
I'm trying to perform a search that I can't find any information about in MySQL's manuals, discussion groups, or on the Web. I'm used to using BerkeleyDB's Btree, so I can browse to find the alphabetically next match against a query. If I query "faction" in a BDB btree database and use DB_SET_RANGE as one of the parameters, I can get "fiction" as the next closest higher match.
3
1890
by: GeRmIc | last post by:
How do you find if the returned XML string does not contain any record/elements? The XML string i receive just has the structure if there are not records, How do I check for this? <?xml version="1.0" encoding="UTF-8"?> <array-list xsi:type="java:java.util.ArrayList" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
29
4339
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return substring before a pattern), AFTER (return substring after a pattern), and BETWEEN (return substring between 2 patterns). My questions are: 1. Can any tell me how I can implement such functionality in C#? 2. Is it possible to add/include function...
94
4825
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
2
3759
by: Spooner | last post by:
I have code that reads in a paragraph of words from a .txt file. How would I go about finding a string that a user inputs into the program? I read in the text as a char, so perhaps I should change the user input into a string? Any ideas would be appreciated.
1
1482
by: shaahul | last post by:
1. I would like to parse a C file and get the list of functions in that file. 2. And then search for a string of the pattern "funcName(var)" in each of those functions. Is it possible to do such a thing in Perl. Are there any scripts available for doing the same? Thanks
3
2171
by: haran22 | last post by:
hi to all. I have a problem i finding substring. i have many strings in array one string as the value String s1="a|b" 2nd as the value String s2= "ab|cd" 3rd as the value String s3= "a|cde" i want to extract or substring these string values as two strings which is seprated by '|'. for eg 1st is substring as a and b 2nd is ab and cd 3rd is a and cde.
2
1478
by: pnsreee | last post by:
Hi All, I have the following strings seperated by space . I have to grep for Naveen having two values(Sha or See) in the string. 1)Naveen Sha reswww 2)Naveen See rex-www 3)Naveen See rex.x 4)Naveen xxxx cccccc 5)ABBCD See reggg I have the following regexp.
5
2319
by: sg14 | last post by:
I'm having a little trouble finding multiple occurrences of a string in a file. I have a txt file and I have to find e-mail addresses out of it. I open the file with ifstream, and use getline to read in each line. I then use str.find(str1, pos) to find the '@' symbol in the file. I just can't figure out how to pull the entire address out and not just the @ symbol. Any tips?
0
11106
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10724
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
10829
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
10403
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
9556
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...
1
7955
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4599
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
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.