String manipulation help

Posted on 2005-04-21
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

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

    Accepted Solution

    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

    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

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now