Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. IndexOf Question

IndexOf Question

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studio
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnalogNerd
    wrote on last edited by
    #1

    I just ran into a real headscratcher. I have code that does the following:

    var textReader = new StreamReader(path + "\\\\web.config");
    var fileContents = textReader.ReadToEnd();
    if (fileContents.IndexOf(oldPwd, StringComparison.Ordinal) == -1)
    {
        Log("Old Password Not Found.", 0);
    }
    else
    {
        Log("Replacing old password", 1);
        fileContents = fileContents.Replace(oldPwd, newPwd);
    }
    

    What I've found is that if the oldPwd contains ^ then things get weird. 1. If the old password ends in a ^, e.g. 123^ then the IndexOf works, but the Replace does will keep the ^ intact. So, with oldPwd = 123^ and newPwd = 456 then Replace will leave the file looking like 456^ 2. If the old password has a ^ in the middle of it then IndexOf will return a -1. I'm baffled to be honest and not sure if it has to do with how I'm reading the file in or what. But I tried this on an online compiler and it worked just fine. So it has to be me, right? EDIT: I also tried it in my code with hard coded strings and it behaves just fine:

    var mystring = "this is a password 123^ yeah";
    Console.WriteLine("Did it find it: " + mystring.IndexOf("123^", StringComparison.Ordinal).ToString());
    Console.WriteLine("Did it replace it: " + mystring.Replace("123^", "456"));
    mystring = "this is a password 123^456 yeah";
    Console.WriteLine("Did it find it: " + mystring.IndexOf("123^456", StringComparison.Ordinal).ToString());
    Console.WriteLine("Did it replace it: " + mystring.Replace("123^456", "abcdefg"));
    

    FURTHER EDIT: Apparently if I run this through Visual Studio and step through the code it works fine. When I compile the code and run it from the command prompt it exhibits the behavior I outlined above.

    F P 2 Replies Last reply
    0
    • A AnalogNerd

      I just ran into a real headscratcher. I have code that does the following:

      var textReader = new StreamReader(path + "\\\\web.config");
      var fileContents = textReader.ReadToEnd();
      if (fileContents.IndexOf(oldPwd, StringComparison.Ordinal) == -1)
      {
          Log("Old Password Not Found.", 0);
      }
      else
      {
          Log("Replacing old password", 1);
          fileContents = fileContents.Replace(oldPwd, newPwd);
      }
      

      What I've found is that if the oldPwd contains ^ then things get weird. 1. If the old password ends in a ^, e.g. 123^ then the IndexOf works, but the Replace does will keep the ^ intact. So, with oldPwd = 123^ and newPwd = 456 then Replace will leave the file looking like 456^ 2. If the old password has a ^ in the middle of it then IndexOf will return a -1. I'm baffled to be honest and not sure if it has to do with how I'm reading the file in or what. But I tried this on an online compiler and it worked just fine. So it has to be me, right? EDIT: I also tried it in my code with hard coded strings and it behaves just fine:

      var mystring = "this is a password 123^ yeah";
      Console.WriteLine("Did it find it: " + mystring.IndexOf("123^", StringComparison.Ordinal).ToString());
      Console.WriteLine("Did it replace it: " + mystring.Replace("123^", "456"));
      mystring = "this is a password 123^456 yeah";
      Console.WriteLine("Did it find it: " + mystring.IndexOf("123^456", StringComparison.Ordinal).ToString());
      Console.WriteLine("Did it replace it: " + mystring.Replace("123^456", "abcdefg"));
      

      FURTHER EDIT: Apparently if I run this through Visual Studio and step through the code it works fine. When I compile the code and run it from the command prompt it exhibits the behavior I outlined above.

      F Offline
      F Offline
      fjdiewornncalwe
      wrote on last edited by
      #2

      The web.config file is just an xml file, so perhaps the way to deal with this is to load the file as an XMLDocument and then simply navigate to the node containing the old password and change its value.

      I wasn't, now I am, then I won't be anymore.

      A 1 Reply Last reply
      0
      • F fjdiewornncalwe

        The web.config file is just an xml file, so perhaps the way to deal with this is to load the file as an XMLDocument and then simply navigate to the node containing the old password and change its value.

        I wasn't, now I am, then I won't be anymore.

        A Offline
        A Offline
        AnalogNerd
        wrote on last edited by
        #3

        Unfortunately I don't always know which node the password will be in which is why I went with treating it as a string.

        1 Reply Last reply
        0
        • A AnalogNerd

          I just ran into a real headscratcher. I have code that does the following:

          var textReader = new StreamReader(path + "\\\\web.config");
          var fileContents = textReader.ReadToEnd();
          if (fileContents.IndexOf(oldPwd, StringComparison.Ordinal) == -1)
          {
              Log("Old Password Not Found.", 0);
          }
          else
          {
              Log("Replacing old password", 1);
              fileContents = fileContents.Replace(oldPwd, newPwd);
          }
          

          What I've found is that if the oldPwd contains ^ then things get weird. 1. If the old password ends in a ^, e.g. 123^ then the IndexOf works, but the Replace does will keep the ^ intact. So, with oldPwd = 123^ and newPwd = 456 then Replace will leave the file looking like 456^ 2. If the old password has a ^ in the middle of it then IndexOf will return a -1. I'm baffled to be honest and not sure if it has to do with how I'm reading the file in or what. But I tried this on an online compiler and it worked just fine. So it has to be me, right? EDIT: I also tried it in my code with hard coded strings and it behaves just fine:

          var mystring = "this is a password 123^ yeah";
          Console.WriteLine("Did it find it: " + mystring.IndexOf("123^", StringComparison.Ordinal).ToString());
          Console.WriteLine("Did it replace it: " + mystring.Replace("123^", "456"));
          mystring = "this is a password 123^456 yeah";
          Console.WriteLine("Did it find it: " + mystring.IndexOf("123^456", StringComparison.Ordinal).ToString());
          Console.WriteLine("Did it replace it: " + mystring.Replace("123^456", "abcdefg"));
          

          FURTHER EDIT: Apparently if I run this through Visual Studio and step through the code it works fine. When I compile the code and run it from the command prompt it exhibits the behavior I outlined above.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          This confused me a while back as well. ^ is the escape character on the command line, so escape it (^^) and it should work as expected.

          A 1 Reply Last reply
          0
          • P PIEBALDconsult

            This confused me a while back as well. ^ is the escape character on the command line, so escape it (^^) and it should work as expected.

            A Offline
            A Offline
            AnalogNerd
            wrote on last edited by
            #5

            I owe you a beer! Thank you!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups