Is there any standard C++ function for generating a substr up until a new-line char?

This is a discussion on Is there any standard C++ function for generating a substr up until a new-line char? within the C++ Programming forums, part of the General Programming Boards category; I was wondering if there is any standard C++ function for generating a substring up until the first '\n' character ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Is there any standard C++ function for generating a substr up until a new-line char?

    I was wondering if there is any standard C++ function for generating a substring up until the first '\n' character read in a source string passed to the function, after a position specified in the string? If not, I guess I'll have to write my own.

    I noticed that the string::substr() function only allows you to pass a starting pos, and the length, and the substring generated will include any new-line characters it encounters before the end of length, and I can't have that, so that's why I'm looking for an alternative function to do what I specified above.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why not use find to tell you where the \n character is, and pass that to substr?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    So get the length of the string
    find the starting position of your substring
    *subtract 1* to avoid transferring the newline at the end.

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Thumbs up

    Quote Originally Posted by tabstop View Post
    Why not use find to tell you where the \n character is, and pass that to substr?
    Good idea. I didn't think of that.

    Thanks.

    So what I got is:

    Code:
    vector<string> C_script_operations::findJsFunctionsNames(string& scriptStr) {
    
        vector<string> result_vec;
        string search_str = "function";
        string current_function_name;
        size_t last_function_keyword_pos = scriptStr.find_last_of(search_str, scriptStr.size());
        if (last_function_keyword_pos != string::npos) {
            for (size_t i = scriptStr.find(search_str, 0); i < last_function_keyword_pos; i = scriptStr.find(search_str, i+1)) {
                 if (i != string::npos) {
                     current_function_name = stripStr(scriptStr.substr(scriptStr.find_last_of(search_str, i + search_str.size() - 1),
                                                      scriptStr.find('\n', i + search_str.size() - 1)), "function", "\n", 1, exclusive);
                     if (!current_function_name.empty()) {
                         removeSpcsFromStr(current_function_name);
                         result_vec.push_back(current_function_name);
                     }
                 }
            }
        }
        return result_vec;
    
    }
    So I'm off now to go test my function...
    Last edited by Programmer_P; 01-22-2011 at 07:59 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

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