remove of string

Discussion in 'C++' started by Mike, Apr 22, 2010.

  1. Mike

    Mike Guest

    Hi
    of a char: char filepath[255];
    I should remove the last two words: lin.xpl
    it's a path alike: blal/blaald/lin.xpl so I need something like: blal/
    blaald/
    How should I do that?
    Many thanks
    Michael
     
    Mike, Apr 22, 2010
    #1
    1. Advertising

  2. Mike

    Fred Zwarts Guest

    "Mike" <> wrote in message
    news:
    > Hi
    > of a char: char filepath[255];
    > I should remove the last two words: lin.xpl
    > it's a path alike: blal/blaald/lin.xpl so I need something like:
    > blal/ blaald/
    > How should I do that?
    > Many thanks
    > Michael


    filepath[12] = 0;
     
    Fred Zwarts, Apr 22, 2010
    #2
    1. Advertising

  3. Mike

    Fred Zwarts Guest

    OK, an attempt for a more serious reply.
    The first reply was trivial, because of the very vague and contradictionary question.

    "Fred Zwarts" <> wrote in message
    news:hqph40$fp1$
    > "Mike" <> wrote in message
    > news:
    >> Hi
    >> of a char: char filepath[255];
    >> I should remove the last two words: lin.xpl


    What exactly do you think defines the term "word" here.
    Words are normally separated by word separators.
    What do you consider a word separator?
    Only a '.', as in the example, or also a '-', a '/', a ' ', a '_', etc.?
    Are you sure that you always want to remove the last two words?
    Even in the case that the string is e.g.
    "/ect/bla.conf.original" -> "/etc/bla."
    or "/etc/init.d/rc5.d/S10product" -> /etc/init.d/rc."?
    or "/usr/local/bin" -> "/usr/".
    What if there is a word separator at the end, or if there are two successive word separators as in "/etc/..".

    >> it's a path alike: blal/blaald/lin.xpl so I need something like:
    >> blal/ blaald/


    This suggests that you do not want to remove the last two words,
    but everything following the last '/', which is something very different.
    (But you do not say so.)
    Do you only want to consider the '/', or also the '\' (as it is used in Windows)?

    >> How should I do that?


    First try to formulate what you want to do exactly.
    Then see if you really want to do this with an array of char with a fixed length,
    or with the more flexible std::string type, which has methods to find the
    first, or the last position of a given set of characters within the string,
    to extract substrings, and more string manipulation methods.

    >> Many thanks
    >> Michael

    >
    > filepath[12] = 0;
     
    Fred Zwarts, Apr 23, 2010
    #3
  4. Mike

    Mike Guest

    Hi
    filepath is a char * which i need for loading sound. Now i need the
    full path to the sound for loading. So
    remove all after the last / but add sounds/ban.wav.
    Something's wrong below.
    Many thanks again
    Michael


    std:: string s = filepath;
    std::string t = s.substr(0, s.find_last_of('/') + 1);
    char *conv;
    conv = new char[t.length() +1];
    strcpy(conv, t.c_str());
    char str[255] = "sounds/BAN11.wav";
    strcat(conv, str);
    alutLoadWAVFile((ALbyte*) conv, &format, &data, &size, &freq,
    &loop);
     
    Mike, Apr 23, 2010
    #4
  5. Mike

    Fred Zwarts Guest

    "Mike" <> wrote in message
    news:
    > Hi
    > filepath is a char * which i need for loading sound. Now i need the
    > full path to the sound for loading. So
    > remove all after the last / but add sounds/ban.wav.
    > Something's wrong below.
    > Many thanks again
    > Michael
    >
    >
    > std:: string s = filepath;
    > std::string t = s.substr(0, s.find_last_of('/') + 1);


    A bit dangerous. What happens if there is no '/' in s?
    Is an empty t intended?

    > char *conv;
    > conv = new char[t.length() +1];
    > strcpy(conv, t.c_str());
    > char str[255] = "sounds/BAN11.wav";
    > strcat(conv, str);
    > alutLoadWAVFile((ALbyte*) conv, &format, &data, &size, &freq, &loop);


    These last 7 lines can be written easier as

    t += "sounds/BAN11.wav";
    alutLoadWAVFile((ALbyte*) t.c_str (), &format, &data, &size, &freq, &loop);

    with the side effect that t has a different value at the end. If that is not what you want:

    const string conv = t + "sounds/BAN11.wav";
    alutLoadWAVFile((ALbyte*) conv.c_str (), &format, &data, &size, &freq, &loop);
     
    Fred Zwarts, Apr 23, 2010
    #5
  6. Mike

    Mike Guest

    Thanks Stuart. Still something is wrong....VB.NET is so much easier
    for strings....
    Many thanks again
    Michael
    ------
    XPLMGetPluginInfo( XPLMGetMyID(), NULL,filepath, NULL,NULL);
    std:: string str = filepath;
    std::string::size_type n = str.find_last_of('/');
    if(n == std::string::npos) {
    str.replace(n,str.length(),"sounds/BAN11.wav");
    }
    alutLoadWAVFile((ALbyte*) str.c_str (), &format, &data, &size,
    &freq, &loop);
     
    Mike, Apr 23, 2010
    #6
  7. Mike

    Jorgen Grahn Guest

    On Fri, 2010-04-23, Mike wrote:
    > Hi
    > filepath is a char * which i need for loading sound. Now i need the
    > full path to the sound for loading. So
    > remove all after the last / but add sounds/ban.wav.


    You seem to be after the function commonly known as dirname.
    It exists in the Unix shell, and for example in Python as
    os.path.dirname(). Does not exist in C++, but

    (a) finding its documentation helps a lot when you designin your own
    (b) and yours should probaly be called dirname()
    (c) there's probably one in Boost.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Apr 25, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    414
    Hans Kesting
    Jul 8, 2004
  2. =?Utf-8?B?SklNLkgu?=

    Q: remove a certain string in string

    =?Utf-8?B?SklNLkgu?=, Feb 18, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    351
    Joshua Flanagan
    Feb 28, 2005
  3. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,387
    Henrik
    Aug 10, 2005
  4. tshad
    Replies:
    6
    Views:
    21,495
    tshad
    Aug 8, 2006
  5. soni29

    remove a string from within a string

    soni29, Aug 5, 2004, in forum: Javascript
    Replies:
    2
    Views:
    90
Loading...

Share This Page