• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

String manipulation help

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"
  Response.Redirect("default.aspx")

thanks
0
MikeMCSD
Asked:
MikeMCSD
3 Solutions
 
raterusCommented:
The String.IndexOf() Method is what you need to become familiar with.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemstringclassindexoftopic.asp

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
  Response.Redirect("default.aspx")
end if

Hope this helps,
--Michael
0
 
NauticalNonsenseCommented:
Not sure what you mean on the logic of Only here, but this should get you in the right direction:

C#
================================
                  string s;
                  if (s.IndexOf("medal") > 0 || s.IndexOf("medals") > 0)
                  {
                        s.Replace(" OR ", " AND ");
                  }
                  if (s.IndexOf("medal") == 1)
                  {
                        Response.Redirect("whatever.aspx");
                  }


VB
=========================================
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
   Response.Redirect("whatever.aspx")
End If


The =1 line means only one occurence.

Hope this helps!

aaron
0
 
laotzi2000Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now