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 / C++ / MFC
  4. CString search

CString search

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
9 Posts 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I have 2 CStrings one has 2 dashs in it eg test1-test2-test3.txt the other one has 3 dashs in it. eg test1-test2-test3-test4.txt I need some code to find the one with the 3 dashs and kill the file. I'm in a while loop but and have been trying to get the find to work right I can find the first - but how do I continue on from there ? Thanks in advance for any help

    P realJSOPR P 3 Replies Last reply
    0
    • L Lost User

      I have 2 CStrings one has 2 dashs in it eg test1-test2-test3.txt the other one has 3 dashs in it. eg test1-test2-test3-test4.txt I need some code to find the one with the 3 dashs and kill the file. I'm in a while loop but and have been trying to get the find to work right I can find the first - but how do I continue on from there ? Thanks in advance for any help

      P Offline
      P Offline
      Philip Nicoletti
      wrote on last edited by
      #2

      Maybe just count the number of '-' in the string ...

      CString testString = "test1-test2-test3--.txt";

      int numberFound = 0;

      for (int i=0; i<testString.GetLength(); i++)
      if (testString.GetAt(i) == '-') numberFound++;

      L 1 Reply Last reply
      0
      • P Philip Nicoletti

        Maybe just count the number of '-' in the string ...

        CString testString = "test1-test2-test3--.txt";

        int numberFound = 0;

        for (int i=0; i<testString.GetLength(); i++)
        if (testString.GetAt(i) == '-') numberFound++;

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Phil first thanks I don't know if it was a typo but the i<testString would not compile I changed it to i&testString and then it compiled fine but it's just returning a UAB what is that ?

        P 1 Reply Last reply
        0
        • L Lost User

          I have 2 CStrings one has 2 dashs in it eg test1-test2-test3.txt the other one has 3 dashs in it. eg test1-test2-test3-test4.txt I need some code to find the one with the 3 dashs and kill the file. I'm in a while loop but and have been trying to get the find to work right I can find the first - but how do I continue on from there ? Thanks in advance for any help

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          int CountCharacters(CString sString, char cChar) { int nLen = sString.GetLength(); int nFound = 0; if (cChar == '\0' { nFound = nLen; } else { for (int nIndex = 0; nIndex < nLen; nIndex++) { nFound += ((sString[nIndex] == cChar) ? 1 : 0); } } return nFound; } //in the calling function, put this: if (CountCharacters(myString, '-') == 3) { // ... do something ... }

          L 1 Reply Last reply
          0
          • L Lost User

            Phil first thanks I don't know if it was a typo but the i<testString would not compile I changed it to i&testString and then it compiled fine but it's just returning a UAB what is that ?

            P Offline
            P Offline
            Philip Nicoletti
            wrote on last edited by
            #5

            I am having problems formatting the "less than sign" (it thinks it is a start of a html tag). CString testString = "test-1test-2test3.txt"; int numberFound = 0; for (int i=0; i LESS_THAN_SIGN testString.GetLength(); i++) if (testString.GetAt(i) == '-')numberFound++; the GetLength() is a member function of the CString class

            1 Reply Last reply
            0
            • realJSOPR realJSOP

              int CountCharacters(CString sString, char cChar) { int nLen = sString.GetLength(); int nFound = 0; if (cChar == '\0' { nFound = nLen; } else { for (int nIndex = 0; nIndex < nLen; nIndex++) { nFound += ((sString[nIndex] == cChar) ? 1 : 0); } } return nFound; } //in the calling function, put this: if (CountCharacters(myString, '-') == 3) { // ... do something ... }

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I'm getting this error. any clues ? 'CountCharacters' : local function definitions are illegal

              P realJSOPR 2 Replies Last reply
              0
              • L Lost User

                I'm getting this error. any clues ? 'CountCharacters' : local function definitions are illegal

                P Offline
                P Offline
                Philip Nicoletti
                wrote on last edited by
                #7

                I think that you put the CountCharacters() function INSIDE of an existing function. Place it at the end of your CPP file (and remember to declare it at the top of the CPP file or in the .H file)

                1 Reply Last reply
                0
                • L Lost User

                  I'm getting this error. any clues ? 'CountCharacters' : local function definitions are illegal

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  Yeah, you've coopy the CountCharacters() function into another function. While this is a valid construct in Pascal, it's a no-no in C/C++.

                  1 Reply Last reply
                  0
                  • L Lost User

                    I have 2 CStrings one has 2 dashs in it eg test1-test2-test3.txt the other one has 3 dashs in it. eg test1-test2-test3-test4.txt I need some code to find the one with the 3 dashs and kill the file. I'm in a while loop but and have been trying to get the find to work right I can find the first - but how do I continue on from there ? Thanks in advance for any help

                    P Offline
                    P Offline
                    Paolo Messina
                    wrote on last edited by
                    #9

                    Hi, Try this function to count the dashes:

                    int CountChar(CString str, TCHAR ch)
                    {
                    int found = 0;
                    int start = 0;
                    start = str.Find(ch, start);

                    while (start => 0)
                    {
                    found++;
                    start = str.Find(ch, start);
                    }

                    return found;
                    }

                    I wrote this while replying, so I didn't try that. Should work... Paolo.

                    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