472,142 Members | 1,273 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,142 software developers and data experts.

vb.net - finding a substring in a string

2
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 42983
TRScheel
638 Expert 512MB
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
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 Expert 512MB
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 Expert 4TB
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
Thanks a lot for ur replies.
But I m afraid, i cud not find CONTAINS for STRING, it exist for STRINGCOLLECTION... :(

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 Expert 512MB
Thanks a lot for ur replies.
But I m afraid, i cud not find CONTAINS for STRING, it exist for STRINGCOLLECTION... :(

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
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

Post your reply

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

Similar topics

reply views Thread by Glenn Fleishman | last post: by
3 posts views Thread by GeRmIc | last post: by
29 posts views Thread by zoro | last post: by
3 posts views Thread by haran22 | last post: by
5 posts views Thread by sg14 | last post: by

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.