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. Need help using IndexOf()

Need help using IndexOf()

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
9 Posts 4 Posters 0 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.
  • R Offline
    R Offline
    Rbledwards
    wrote on last edited by
    #1

    I'm trying to do a quick search on a string array. I'm using IndexOf() but I'm not getting the desired results. I've tried two ways, the first I thought should work, the second is just to prove to you I don't know what I'm doing. private int CheckIsDate(string ColumnName) { string strDate = "Date"; int position = strDate.IndexOf(ColumnName); return position; } private int CheckIsCurrency(string ColumnName) { string[] strCurrency = {"Amount","Cost"}; int position = -1; foreach (string s in strCurrency) { if (s.IndexOf(ColumnName) == -1) { } else { position = 0; break; } } return position; } This should be simple right? Robert

    L I J 3 Replies Last reply
    0
    • R Rbledwards

      I'm trying to do a quick search on a string array. I'm using IndexOf() but I'm not getting the desired results. I've tried two ways, the first I thought should work, the second is just to prove to you I don't know what I'm doing. private int CheckIsDate(string ColumnName) { string strDate = "Date"; int position = strDate.IndexOf(ColumnName); return position; } private int CheckIsCurrency(string ColumnName) { string[] strCurrency = {"Amount","Cost"}; int position = -1; foreach (string s in strCurrency) { if (s.IndexOf(ColumnName) == -1) { } else { position = 0; break; } } return position; } This should be simple right? Robert

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      Rbledwards wrote: I don't know what I'm doing Neither do I, please explain a bit more (scenario etc), and paste less code. :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

      R 1 Reply Last reply
      0
      • L leppie

        Rbledwards wrote: I don't know what I'm doing Neither do I, please explain a bit more (scenario etc), and paste less code. :) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        R Offline
        R Offline
        Rbledwards
        wrote on last edited by
        #3

        leppie wrote: and paste less code Oops, was asuming more was better. I just want to know if my string exists in the array. In the debugger I can see that it does exist. For example. I have a column named Amount billed. The strings in my array are {"Amount","Cost"}. I am using IndexOf to search the ColumnName for one of the strings in the array. It doesn't find "Amount" for some reason. Thanks Robert

        1 Reply Last reply
        0
        • R Rbledwards

          I'm trying to do a quick search on a string array. I'm using IndexOf() but I'm not getting the desired results. I've tried two ways, the first I thought should work, the second is just to prove to you I don't know what I'm doing. private int CheckIsDate(string ColumnName) { string strDate = "Date"; int position = strDate.IndexOf(ColumnName); return position; } private int CheckIsCurrency(string ColumnName) { string[] strCurrency = {"Amount","Cost"}; int position = -1; foreach (string s in strCurrency) { if (s.IndexOf(ColumnName) == -1) { } else { position = 0; break; } } return position; } This should be simple right? Robert

          I Offline
          I Offline
          Ista
          wrote on last edited by
          #4

          It should work. maybe it something else your doing int iCOunt = -1; foreach ( string s in strCurrency ) { iCount++; if ( s.IndexOf(ColumnName) != -1 ) return iCount; }

          R 1 Reply Last reply
          0
          • I Ista

            It should work. maybe it something else your doing int iCOunt = -1; foreach ( string s in strCurrency ) { iCount++; if ( s.IndexOf(ColumnName) != -1 ) return iCount; }

            R Offline
            R Offline
            Rbledwards
            wrote on last edited by
            #5

            No, I had it backwards. I was looking to see if s existed in ColumnName. so I needed to put ColumnName.IndexOf(s) instead of s.IndexOf(ColumnName). Such is life. Robert

            1 Reply Last reply
            0
            • R Rbledwards

              I'm trying to do a quick search on a string array. I'm using IndexOf() but I'm not getting the desired results. I've tried two ways, the first I thought should work, the second is just to prove to you I don't know what I'm doing. private int CheckIsDate(string ColumnName) { string strDate = "Date"; int position = strDate.IndexOf(ColumnName); return position; } private int CheckIsCurrency(string ColumnName) { string[] strCurrency = {"Amount","Cost"}; int position = -1; foreach (string s in strCurrency) { if (s.IndexOf(ColumnName) == -1) { } else { position = 0; break; } } return position; } This should be simple right? Robert

              J Offline
              J Offline
              Julian Bucknall MSFT
              wrote on last edited by
              #6

              I think what you're asking is to see if a particular string is in an array of strings. Use this:

              public bool ExistsInArray(string s, string[] sList)
              {
              foreach (string item in sList)
              {
              if (item == s) return true;
              }
              return false;
              }

              In other words, check all strings in the list to see if the one I want is there. If it is, exit immediately I find it, returning true. If I fall through the loop, having checked all strings in the list, return false. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

              I 1 Reply Last reply
              0
              • J Julian Bucknall MSFT

                I think what you're asking is to see if a particular string is in an array of strings. Use this:

                public bool ExistsInArray(string s, string[] sList)
                {
                foreach (string item in sList)
                {
                if (item == s) return true;
                }
                return false;
                }

                In other words, check all strings in the list to see if the one I want is there. If it is, exit immediately I find it, returning true. If I fall through the loop, having checked all strings in the list, return false. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

                I Offline
                I Offline
                Ista
                wrote on last edited by
                #7

                but if you do "==" arent you just comparing the instances and not the value s.Equals(ColumnName)

                J 1 Reply Last reply
                0
                • I Ista

                  but if you do "==" arent you just comparing the instances and not the value s.Equals(ColumnName)

                  J Offline
                  J Offline
                  Julian Bucknall MSFT
                  wrote on last edited by
                  #8

                  No. The == operator for strings is overridden to compare the actual string contents. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

                  I 1 Reply Last reply
                  0
                  • J Julian Bucknall MSFT

                    No. The == operator for strings is overridden to compare the actual string contents. Cheers, Julian Program Manager, C# This posting is provided "AS IS" with no warranties, and confers no rights.

                    I Offline
                    I Offline
                    Ista
                    wrote on last edited by
                    #9

                    well cool I'm not an expert yet, but I play one at work. Yeah and here too.

                    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