question

JasonDaurison-0792 avatar image
0 Votes"
JasonDaurison-0792 asked SamMueller-0712 commented

Split String

Hello everyone,

I was wondering if there is a way to determine if a string contains a value in a array without using a loop. Does the array class have a function for that?

Thanks.

dotnet-visual-basic
· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I was wondering if there is a way to determine if a
string contains a value in a array without using a loop.

As a matter of interest, why do you want to avoid
using a loop? If it's because you think some other
method will be faster then you may have a basic
misunderstanding of how data is stored and
accessed in arrays.

Any method that needs to search an array for a
match - including find() and contains() - must
traverse the array an item at a time looking for
a match. That implies looping as it must be able
to process an indeterminate number of elements in
the array.

There is no hidden or secret index-like access
capability that would allow a member method to
somehow locate an element based on content. So
you do not eliminate looping by using a member
method - you just substitute an implicit loop
for an explicit loop.

All you might save is some lines of source code.

  • Wayne

0 Votes 0 ·

Thank you, I was just asking out of curiosity if I didn't need to use a loop.

0 Votes 0 ·
SimpleSamples avatar image SimpleSamples JasonDaurison-0792 ·

As Wayne indicated, it depends whether you are asking for your convenience or the computer's. If you are trying to make it easier for the computer then it is not likely to be possible and is not worth being concerned about. If you want to make it easier for yourself then that is what LINQ is designed to do.

0 Votes 0 ·
Show more comments

Do you want to search a string to determine if one of many possibilities is in that one string or do you want to search an array of strings to determine if a specified string is one of the array's elements? There is confusion. I assumed you need the first one and the other answer assumes the other. Your title is Split String but the question does not ask that. I assume you need to find one of several possible strings and you need to know where it is so you can split the string. If none of the answers help then you need to clarify the question; some samples will help.

0 Votes 0 ·

Hi @JasonDaurison-0792 ,
How is the question going? Please let me know if you need further assistance.

0 Votes 0 ·
SimpleSamples avatar image
0 Votes"
SimpleSamples answered SimpleSamples commented

You can do something like:

 searchstrings.Find(Function(f) s1.Contains(f))

The following is a complete sample.

 Dim s1 As String = "RedGreenBlue"
 Dim s2 As String = "BlackWhite"
 Dim searchstrings As New List(Of String) From {"Blue", "Orange"}
 Dim v As String
 v = searchstrings.Find(Function(f) s1.Contains(f))
 Console.WriteLine("Found {0}",If(v Is Nothing, "nothing", v))
 v = searchstrings.Find(Function(f) s2.Contains(f))
 Console.WriteLine("Found {0}",If(v Is Nothing, "nothing", v))

See FindStringInArrayInStringVB for a fiddle you can use to try it.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you need to know the position of the string found then we can do that.

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered SamMueller-0712 commented

You can use .Contains extension.

  • First example finds karen againsts Karen because of the comparer

  • Second example does not as strings are case sensitive.

So it's wise to know if an exact match is needed or not.

 Dim findThisName = "Karen"
 Dim someArray = New String() {"karen", "bill", "mary"}
    
 Dim test = someArray.Contains(findThisName, StringComparer.OrdinalIgnoreCase)
 If test Then
     Console.WriteLine($"{findThisName} was found case insensitive")
 Else
     Console.WriteLine($"{findThisName} was not found case insensitive")
 End If
    
 test = someArray.Contains(findThisName)
 If test Then
     Console.WriteLine($"{findThisName} was found case sensitive")
 Else
     Console.WriteLine($"{findThisName} was not found case sensitive")
 End If




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Try this:

 string[] tokens = str.Split(new[] { "is Marco and" }, StringSplitOptions.None);

If you have a single character delimiter (like "," for instance), you can reduce that to the following (note the single quotes):

 string[] tokens = str.Split(',');

I also recommend you to look briefly through this tutorial.


3 Votes 3 ·