String manipulation help

Posted on 2005-04-21
Medium Priority
Last Modified: 2010-04-07
I have a string that could contain any of the following:

newString = "joseph OR medal OR gold"
newString = "christopher OR medals"
newString = "medals"
newString = "medal"

I want to check "newString" for "medals" or "medal", then I want to replace
the "OR" with "AND" . . something like this:

If newString has "medals" or "medal" Then
  Replace "OR" with "AND"

So the string "joseph OR medal OR gold"  would be "joseph AND medal AND gold"

Also . . .  If just the one word "medals" or "medal" is in the string (newString = "medals")
I want to  Response.Redirect("default.aspx") . . like this:

If newString has ONLY "medal" or "medals"

Question by:MikeMCSD
LVL 33

Assisted Solution

raterus earned 800 total points
ID: 13835269
The String.IndexOf() Method is what you need to become familiar with.

To check if a string contains a string

If newString.indexOf("medals") <> -1 then
  'string contains "medals"
end if

Also, your last problem can be solved with the exclusive or operator (xor), You can lookup xor yourself, but basically it allows one or the other, but not both.

If newString.indexOf("medals") <> -1 xor newString.indexOf("medal") <> -1 then
end if

Hope this helps,

Accepted Solution

NauticalNonsense earned 800 total points
ID: 13835275
Not sure what you mean on the logic of Only here, but this should get you in the right direction:

                  string s;
                  if (s.IndexOf("medal") > 0 || s.IndexOf("medals") > 0)
                        s.Replace(" OR ", " AND ");
                  if (s.IndexOf("medal") == 1)

Dim s As String
If s.IndexOf("medal") > 0 Or s.IndexOf("medals") > 0 Then
   s.Replace(" OR ", " AND ")
End If
If s.IndexOf("medal") = 1 Then
End If

The =1 line means only one occurence.

Hope this helps!

LVL 12

Assisted Solution

laotzi2000 earned 400 total points
ID: 13835573
Don't think you should use indexOf here,
What if someone named medalville is in the string?

I think you should split new string to a string array
and then check each item in the array
After you're done, you can join the array back to a string

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question