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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString

CString

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 3 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.
  • J Offline
    J Offline
    Johnny Peszek
    wrote on last edited by
    #1

    Hi! I got data stored in CString variable. The data consist of mulitiline numbers each number in new line. The length of the CString depends on the data and is various. How can I delete e.g. 5 lines from the end of the CString? Thanks in advance.

    A V 2 Replies Last reply
    0
    • J Johnny Peszek

      Hi! I got data stored in CString variable. The data consist of mulitiline numbers each number in new line. The length of the CString depends on the data and is various. How can I delete e.g. 5 lines from the end of the CString? Thanks in advance.

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      Answer: basic math, pen and a paper. Following your description, if you'd need to delete the last 5 lines, this means that you'd need to delete so many characters starting from the end that you'd reach the fifth '\n' character. The lines in a CString object are stored sequentically, with line changes denoted as '\n' and the end of the string as '\0'. An example. Consider that your arbitary string is structured as follows "111111111111\n22222222222\n333333333333\n44444444444\n555555555555\n6666666666666\n77777777777\0". Now, if you'd need to delete the last two lines, you'd first need to determine how many lines there are on the string. This means finding how many '\n' characters are found. The line quantity is this added with 1 (last line doesn't have a '\n'). When you had this amount, in this case, 7, and would need to delete 2 lines, it would leave us 4 '\n' characters, right ? So, let's search until we find the fifth '\n' character. After we get it's index, we delete characters from the string, starting from this index. Thus, the last fifth '\n' is replaced by a '\0', and all remaining characters are deleted. Finding the amount of '\n' is easy: start from the beginning, find the first one. Then start again, from the index returned by the first search plus one. By using this recursive search method, you can run through the string and find out how many line changes you have. It's pretty straigthforward, although might feel difficult at first. But the quicker you start, the quicker you get it done :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

      J 1 Reply Last reply
      0
      • A Antti Keskinen

        Answer: basic math, pen and a paper. Following your description, if you'd need to delete the last 5 lines, this means that you'd need to delete so many characters starting from the end that you'd reach the fifth '\n' character. The lines in a CString object are stored sequentically, with line changes denoted as '\n' and the end of the string as '\0'. An example. Consider that your arbitary string is structured as follows "111111111111\n22222222222\n333333333333\n44444444444\n555555555555\n6666666666666\n77777777777\0". Now, if you'd need to delete the last two lines, you'd first need to determine how many lines there are on the string. This means finding how many '\n' characters are found. The line quantity is this added with 1 (last line doesn't have a '\n'). When you had this amount, in this case, 7, and would need to delete 2 lines, it would leave us 4 '\n' characters, right ? So, let's search until we find the fifth '\n' character. After we get it's index, we delete characters from the string, starting from this index. Thus, the last fifth '\n' is replaced by a '\0', and all remaining characters are deleted. Finding the amount of '\n' is easy: start from the beginning, find the first one. Then start again, from the index returned by the first search plus one. By using this recursive search method, you can run through the string and find out how many line changes you have. It's pretty straigthforward, although might feel difficult at first. But the quicker you start, the quicker you get it done :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

        J Offline
        J Offline
        Johnny Peszek
        wrote on last edited by
        #3

        Your idea is pretty good. Thanks!!

        1 Reply Last reply
        0
        • J Johnny Peszek

          Hi! I got data stored in CString variable. The data consist of mulitiline numbers each number in new line. The length of the CString depends on the data and is various. How can I delete e.g. 5 lines from the end of the CString? Thanks in advance.

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          hope this helps... ==>From MSDN: Deletes a character or characters from a string starting with the character at the given index. int Delete( int iIndex, int nCount = 1 ); Parameters iIndex The zero-based index of the first character in the CStringT object to delete. nCount The number of characters to be removed. Return Value The length of the changed string. Remarks If nCount is longer than the string, the remainder of the string will be removed. Example The following example demonstrates the use of CStringT::Delete. //typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CAtlString; CAtlString str( "Soccer is best!"); printf("Before: %s\n", (LPCTSTR) str); int n = str.Delete(6, 3); printf("After: %s\n", (LPCTSTR) str); _ASSERT(n == str.GetLength()); Output Before: Soccer is best! After: Soccer best! See Also CStringT Overview | Class Members "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

          J 1 Reply Last reply
          0
          • V V 0

            hope this helps... ==>From MSDN: Deletes a character or characters from a string starting with the character at the given index. int Delete( int iIndex, int nCount = 1 ); Parameters iIndex The zero-based index of the first character in the CStringT object to delete. nCount The number of characters to be removed. Return Value The length of the changed string. Remarks If nCount is longer than the string, the remainder of the string will be removed. Example The following example demonstrates the use of CStringT::Delete. //typedef CStringT< TCHAR, StrTraitATL< TCHAR > > CAtlString; CAtlString str( "Soccer is best!"); printf("Before: %s\n", (LPCTSTR) str); int n = str.Delete(6, 3); printf("After: %s\n", (LPCTSTR) str); _ASSERT(n == str.GetLength()); Output Before: Soccer is best! After: Soccer best! See Also CStringT Overview | Class Members "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

            J Offline
            J Offline
            Johnny Peszek
            wrote on last edited by
            #5

            Hi! I was thinking of that function but in my case the length of the CString depends on the data and is various so I don’t know exactly the iIndex. And that’s why I can’t use the function delete. Thanks.

            V 1 Reply Last reply
            0
            • J Johnny Peszek

              Hi! I was thinking of that function but in my case the length of the CString depends on the data and is various so I don’t know exactly the iIndex. And that’s why I can’t use the function delete. Thanks.

              V Offline
              V Offline
              V 0
              wrote on last edited by
              #6

              Here's a list of some basic CString members you can use: CString is quite a powerful and easy class, you'll be able to use these members in order to get the required result. Good luck! AllocSysString Allocates a BSTR from CStringT data. AnsiToOem Makes an in-place conversion from the ANSI character set to the OEM character set. AppendFormat Appends formatted data to an existing CStringT object. Collate Compares two strings (case sensitive, uses locale-specific information). CollateNoCase Compares two strings (case insensitive, uses locale-specific information). Compare Compares two strings (case sensitive). CompareNoCase Compares two strings (case insensitive). Delete Deletes a character or characters from a string. Find Finds a character or substring inside a larger string. FindOneOf Finds the first matching character from a set. Format Formats the string as sprintf does. FormatMessage Formats a message string. FormatMessageV Formats a message string using a variable argument list. FormatV Formats the string using a variable list of arguments. GetEnvironmentVariable Sets the string to the value of the specified environment variable. Insert Inserts a single character or a substring at the given index within the string. Left Extracts the left part of a string. LoadString Loads an existing CStringT object from a Windows resource. MakeLower Converts all the characters in this string to lowercase characters. MakeReverse Reverses the string. MakeUpper Converts all the characters in this string to uppercase characters. Mid Extracts the middle part of a string. OemToAnsi Makes an in-place conversion from the OEM character set to the ANSI character set. Remove Removes indicated characters from a string. Replace Replaces indicated characters with other characters. ReverseFind Finds a character inside a larger string; starts from the end. Right Extracts the right part of a string. SetSysString Sets an existing BSTR object with data from a CStringT object. SpanExcluding Extracts characters from the string, starting with the first character, that are not in the set of characters identified by pszCharSet. SpanIncluding Extracts a substring that contains only the characters in a set. Tokenize Extracts specified tokens in a target string. Trim

              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