A little string question

This is a discussion on A little string question within the C++ Programming forums, part of the General Programming Boards category; Hi! I’ve got this char string: “E:\Movies\X-files” And I want to make it to this string: “E:\Movies” What’s the easiest ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386

    A little string question

    Hi!
    I’ve got this char string: “E:\Movies\X-files”
    And I want to make it to this string: “E:\Movies”
    What’s the easiest way to crop out that “\X-files” bit, please? Are there any functions to do that?

    Thanks.
    Last edited by geek@02; 01-20-2005 at 05:32 AM.

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    If you are using an actual C++ string, look into the .erase() function.

    If you are using a char* string of characters, you may need to devise your own function. The standard C library string.h contains a few functions that would be useful here, such as strncpy() and strncat(), which will return a substring and copy two strings together respectively.

    Unless you have a compelling reason to do so however, I would suggest using the C++ string class since you know the .erase() function has already been tested and probably will work better than anything you can come up with. In addition, if you need to use the string as a 'C' style array of characters, the C++ string class allows you to use mystring.c_str() which you can use in exactly the same was as if you had declared
    Code:
    char* mystring;
    In either case, you will have to write the part of the function that determines what part of the string you want to remove.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 05:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM

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