string manipulation problem

This is a discussion on string manipulation problem within the C++ Programming forums, part of the General Programming Boards category; in Basic language I would program this line of code to remove a bit of data from a string and ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    string manipulation problem

    in Basic language I would program this line of code to remove a bit of data from a string and create a new string:

    dstring = MID$(sstring, start, n)

    is there a comperable method of doing this with the C or C++ language?

    I have searched and searched to no avail.

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You want the std::string class. It has a substr member function.

    Code:
    # include <string>
    using namespace std;
    
    int main()
    {
        string source = "abcdefg";
    
        // should put "ab" into dest. The first parameter is
        // the start index, and the second is the length of
        // the substring
        string dest = source.substr(1,2);
    
        return 0;
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    IfYouSaySo posted the best solution, but if you want to do it with char arrays instead of STL strings, here's a little function I just whipped up:
    Code:
    char* sub_copy(const char *source, char *dest, int index, int count) {
      char *p = dest;
      for (; count > 0; --count) {
        *p = source[index++];
        ++p;
      }
      return dest;
    }
    Last edited by LuckY; 03-18-2005 at 05:46 PM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by IfYouSaySo
    You want the std::string class. It has a substr member function.

    Code:
    # include <string>
    using namespace std;
    
    int main()
    {
        string source = "abcdefg";
    
        // should put "ab" into dest. The first parameter is
        // the start index, and the second is the length of
        // the substring
        string dest = source.substr(1,2);
    
        return 0;
    }
    source.substr(1,2) will give you "bc", source.substr(0,2) will give you "ab". Indices are zero based.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21