Answered by:
Check if a string contain a string.

Question
-
Morning all.
How can I get a Select Case statement to check if a string contain a string.
Have a look at the first If, this will only fire if the hole string "Cash Sale" is found.
What I need is for something like "Mike's Cafe - Cash Sale" to fire that same case statement since it contains the string above.
For Each DataRow In SalesRecordBindingSource Select Case CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 2).DataPropertyName).ToString Case Is = "Cash Sale" Rebate = Rebate + CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 1).DataPropertyName) Me.RebateLabel.Text = "Rebate to date = R " + Rebate.ToString If SalesRecordDataGridView.SelectedCells(0).OwningRow Is SalesRecordDataGridView.Rows(0) Then Exit For Else SalesRecordBindingSource.MovePrevious() End If Case Is = "Cash Sale & Interrupted Purchase Frequency" Rebate = Rebate + CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 1).DataPropertyName) Me.RebateLabel.Text = "Rebate to date = R " + Rebate.ToString Exit For Case Is = "Interrupted Purchase Frequency" Rebate = Rebate + CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 1).DataPropertyName) Me.RebateLabel.Text = "Rebate to date = R " + Rebate.ToString Exit For Case Is = "Late Payment" Me.RebateLabel.Text = "Rebate to date = R " + Rebate.ToString Exit For Next
Hendri Bissolati
Monday, September 23, 2013 6:13 AM
Answers
-
Have you tried something like this:
If string1.Contains(string2) Then 'More code here End If
Regards Ron
- Proposed as answer by IronRazerz Monday, September 23, 2013 11:12 AM
- Marked as answer by Leo (Apple) Yang Monday, September 30, 2013 2:13 AM
Monday, September 23, 2013 7:02 AM -
Dim string1 As String = "tests" Dim string2 As String = "Actual Test" Dim string3 As String = "Actual" If string2.Contains(string1) Then Console.Write("Found!") End If If string2.IndexOf(string1) > -1 Then Console.Write("Found!") End If Select Case True Case string2.IndexOf(string1) > -1 Console.Write("Found Test!") 'Executes when the word tests is found Case string2.IndexOf(string3) > -1 Console.Write("Found Actual!") 'Executes when the word Actual is found End Select
Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.
- Edited by Ahmed-Samy Monday, September 23, 2013 12:13 PM adding comments
- Proposed as answer by Victor.Rally Monday, September 23, 2013 12:51 PM
- Marked as answer by Leo (Apple) Yang Monday, September 30, 2013 2:13 AM
Monday, September 23, 2013 11:41 AM -
Perhaps something like this:
For Each DataRow In SalesRecordBindingSource Dim potentialRebate = CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 1).DataPropertyName) Dim transactionType As String = CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 2).DataPropertyName).ToString Select Case True Case transactionType.IndexOf("Cash Sale", StringComparison.InvariantCultureIgnoreCase) >= 0 rebate = rebate + potentialRebate Me.RebateLabel.Text = "Rebate to date = R " + rebate.ToString If SalesRecordDataGridView.SelectedCells(0).OwningRow Is SalesRecordDataGridView.Rows(0) Then Exit For Else SalesRecordBindingSource.MovePrevious() End If Case transactionType.IndexOf("Cash Sale & Interrupted Purchase Frequency", StringComparison.InvariantCultureIgnoreCase) >= 0 rebate = rebate + potentialRebate Me.RebateLabel.Text = "Rebate to date = R " + rebate.ToString Exit For Case transactionType.IndexOf("Interrupted Purchase Frequency", StringComparison.InvariantCultureIgnoreCase) >= 0 rebate = rebate + potentialRebate Me.RebateLabel.Text = "Rebate to date = R " + rebate.ToString Exit For Case transactionType.IndexOf("Late Payment", StringComparison.InvariantCultureIgnoreCase) >= 0 Me.RebateLabel.Text = "Rebate to date = R " + rebate.ToString Exit For End Select Next
HTH,
Andrew
- Proposed as answer by Frank L. Smith Monday, September 23, 2013 7:31 PM
- Marked as answer by Leo (Apple) Yang Monday, September 30, 2013 2:13 AM
Monday, September 23, 2013 7:23 PM -
How can I get a Select Case statement to check if a string contain a string.
My preference is to not force Select/Case into this format. When your circumstances don't match one of the rules that are available with the Case clause
expression1 To expression2
[ Is ] comparisonoperator expression
expressionthen you are probably better off with If statements, with the IndexOf method, testing for -1 to indicate not found.
http://msdn.microsoft.com/en-us/library/2y7ddk24.aspxYou can use Continue For to jump to the next entry, which corresponds to what a Select/Case would do. Note that sequence will be important - the code above is testing in a sequence that will create a result that is probably not what you intend.
- Edited by Acamar Monday, September 23, 2013 6:35 AM frmt
- Proposed as answer by IronRazerz Monday, September 23, 2013 11:11 AM
- Marked as answer by Leo (Apple) Yang Monday, September 30, 2013 2:13 AM
Monday, September 23, 2013 6:33 AM
All replies
-
How can I get a Select Case statement to check if a string contain a string.
My preference is to not force Select/Case into this format. When your circumstances don't match one of the rules that are available with the Case clause
expression1 To expression2
[ Is ] comparisonoperator expression
expressionthen you are probably better off with If statements, with the IndexOf method, testing for -1 to indicate not found.
http://msdn.microsoft.com/en-us/library/2y7ddk24.aspxYou can use Continue For to jump to the next entry, which corresponds to what a Select/Case would do. Note that sequence will be important - the code above is testing in a sequence that will create a result that is probably not what you intend.
- Edited by Acamar Monday, September 23, 2013 6:35 AM frmt
- Proposed as answer by IronRazerz Monday, September 23, 2013 11:11 AM
- Marked as answer by Leo (Apple) Yang Monday, September 30, 2013 2:13 AM
Monday, September 23, 2013 6:33 AM -
Have you tried something like this:
If string1.Contains(string2) Then 'More code here End If
Regards Ron
- Proposed as answer by IronRazerz Monday, September 23, 2013 11:12 AM
- Marked as answer by Leo (Apple) Yang Monday, September 30, 2013 2:13 AM
Monday, September 23, 2013 7:02 AM -
Dim string1 As String = "tests" Dim string2 As String = "Actual Test" Dim string3 As String = "Actual" If string2.Contains(string1) Then Console.Write("Found!") End If If string2.IndexOf(string1) > -1 Then Console.Write("Found!") End If Select Case True Case string2.IndexOf(string1) > -1 Console.Write("Found Test!") 'Executes when the word tests is found Case string2.IndexOf(string3) > -1 Console.Write("Found Actual!") 'Executes when the word Actual is found End Select
Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.
- Edited by Ahmed-Samy Monday, September 23, 2013 12:13 PM adding comments
- Proposed as answer by Victor.Rally Monday, September 23, 2013 12:51 PM
- Marked as answer by Leo (Apple) Yang Monday, September 30, 2013 2:13 AM
Monday, September 23, 2013 11:41 AM -
try this
Dim string1 As String = "tests" Dim string2 As String = "Actual Test" Select Case string2.Contains(string1) Case True Console.Write("Found Test!") Case False Console.Write("Found Actual!") End Select
jdweng
Monday, September 23, 2013 11:48 AM -
Why not use the indexof?
Dim searchelement As String = CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 2).DataPropertyName).ToString If searchelement.IndexOf("yourstring") > -1 OrElse searchelement.IndexOf("yourotherstring") > 1 Then 'Yoursyntax End If
Success
CorMonday, September 23, 2013 3:29 PM -
Perhaps something like this:
For Each DataRow In SalesRecordBindingSource Dim potentialRebate = CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 1).DataPropertyName) Dim transactionType As String = CType(SalesRecordBindingSource.Item(SalesRecordBindingSource.Position), DataRowView).Item(SalesRecordDataGridView.Columns(SalesRecordDataGridView.ColumnCount - 2).DataPropertyName).ToString Select Case True Case transactionType.IndexOf("Cash Sale", StringComparison.InvariantCultureIgnoreCase) >= 0 rebate = rebate + potentialRebate Me.RebateLabel.Text = "Rebate to date = R " + rebate.ToString If SalesRecordDataGridView.SelectedCells(0).OwningRow Is SalesRecordDataGridView.Rows(0) Then Exit For Else SalesRecordBindingSource.MovePrevious() End If Case transactionType.IndexOf("Cash Sale & Interrupted Purchase Frequency", StringComparison.InvariantCultureIgnoreCase) >= 0 rebate = rebate + potentialRebate Me.RebateLabel.Text = "Rebate to date = R " + rebate.ToString Exit For Case transactionType.IndexOf("Interrupted Purchase Frequency", StringComparison.InvariantCultureIgnoreCase) >= 0 rebate = rebate + potentialRebate Me.RebateLabel.Text = "Rebate to date = R " + rebate.ToString Exit For Case transactionType.IndexOf("Late Payment", StringComparison.InvariantCultureIgnoreCase) >= 0 Me.RebateLabel.Text = "Rebate to date = R " + rebate.ToString Exit For End Select Next
HTH,
Andrew
- Proposed as answer by Frank L. Smith Monday, September 23, 2013 7:31 PM
- Marked as answer by Leo (Apple) Yang Monday, September 30, 2013 2:13 AM
Monday, September 23, 2013 7:23 PM -
Andrew,
In your code,
Case transactionType.IndexOf("Cash Sale & Interrupted Purchase Frequency", StringComparison.InvariantCultureIgnoreCase) >= 0
will never evaluate since the first case will evaluate to true when "Cash Sale & Interrupted ... " is present in the string
Monday, September 23, 2013 7:44 PM -
Case transactionType.IndexOf("Cash Sale & Interrupted Purchase Frequency", StringComparison.InvariantCultureIgnoreCase) >= 0
will never evaluate since the first case will evaluate to true when "Cash Sale & Interrupted ... " is present in the string
A very good point.
OP: The order of the Case statements could be changed, or the string to look for made more specific, or the transactionType could be some other thing. As it is, having the "customer name" in the string is perhaps not optimal.
--
AndrewMonday, September 23, 2013 8:29 PM -
Select Case True
Ugh! When you have to use it in this backwards format in order to make a usable test then any advantage in using Select/Case over other forms of testing disappears.
Monday, September 23, 2013 9:12 PM -
that's is true if he is examining a value. but he is examining the existence of a string in a value. In this case Select Case True will achieve what he wants.
Regards.
Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.
Tuesday, September 24, 2013 5:36 AM -
The Select Case examines the Case which is in this case the constant True.
In my idea 3 simple if statements all closed with an "end if" using the by my given code does it easier, quicker and more readable.
Although you know that I like the Select Case in VB (and not the Switch in C#), are in my idea simple 3 if statements better.
Success
CorTuesday, September 24, 2013 8:32 AM