is there a function to split a string by a certain character?

Answered is there a function to split a string by a certain character?

  • Friday, September 07, 2012 2:52 PM
     
     

    or to do it manually is the only way?

    thanks

    EDIT:

    split a string by new line characters


    • Edited by ColdBackup Friday, September 07, 2012 3:01 PM
    • Edited by ColdBackup Friday, September 07, 2012 3:02 PM
    •  

All Replies

  • Friday, September 07, 2012 3:07 PM
     
      Has Code

    You can use substr() function

    std::string name("Split \n Text \n NewName \n Print");
    std::cout<<name.substr(0, name.find("\\n"));

    Thanks


    Rupesh Shukla






  • Friday, September 07, 2012 4:02 PM
     
      Has Code

    You can use substr() function

    std::string name("Split \n Text \n NewName \n Print");
    std::cout<<name.substr(0, name.find("\\n"));



    it's wrong to use two consecutive backspaces only replacing them with a single one produced expected results and by the way, how do you advance forward? your example shows only how to get text that is before the first occcurance of new line character
  • Friday, September 07, 2012 4:10 PM
     
     Answered Has Code

    I Know it's single \n just write the code on fly and don't worry it's not wrong. Second thing this was a example of your first question "split a string by new line characters" multiple \n doesn't mean anything as you can see substr function start from 0. If you want to extract all the string then why not use simply a loop with substr function and extract all the new line character in some string and then store that string inside some vector or any other collection a simple example can be

    int _tmain(int argc, _TCHAR* argv[])
    {
     	string input="string \n newName \n position",rString;		
    	unsigned int loc = input.find( "\n", 0 );
    	while(loc != string::npos )
    	{
    		rString = input.substr(0,input.find("\n"));
            input = input.substr(input.find("\n")+1,input.length());
            loc = input.find("\n", 0 );
    		cout<<rString<<endl;
    	}		
    	cout<<input;
    	return 0;
    }

    Thanks


    Rupesh Shukla




  • Friday, September 07, 2012 4:42 PM
     
     Answered

    There isn't a Split method like .NET has, if that is what you are asking.

    Your options are manual (with find), or you can pull out the big guns and use something like regex_token_iterator which can split the string not just on a character but on extremely complicated expressions as well.

    • Marked As Answer by ColdBackup Friday, September 07, 2012 5:27 PM
    •  
  • Friday, September 07, 2012 4:52 PM
     
     

    I Know it's single \n just write the code on fly and don't worry it's not wrong.


    if you expect your code to split a string at new line characters, but the whole string with all of its newlines is displayed instead, then you know for a fact it is wrong, simply because it doesn't split a string at new line character, let alone character(S) and I'm not the one who should worry about it, the code wasn't mine

    If you want to extract all the string then why not use simply a loop with substr function and extract all the new line character in some string and then store that string inside some vector or any other collection

    that wouldn't be exactly a function call, would it be? that's called doing it manually, something I try to avoid altogether if possible

    but I agree that I should've been more specific from the start


    • Edited by ColdBackup Friday, September 07, 2012 4:55 PM
    • Edited by ColdBackup Friday, September 07, 2012 4:55 PM
    •  
  • Friday, September 07, 2012 5:07 PM
     
     
    that wouldn't be exactly a function call, would it be? that's called doing it manually, something I try to avoid altogether if possible

    but I agree that I should've been more specific from the start


    It's a Function call substr you are just telling the position from where you have to extract you are not doing it manually.

    Thanks


    Rupesh Shukla

  • Friday, September 07, 2012 5:40 PM
     
      Has Code

    A better Solution you can write by your self like

    std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { std::stringstream ss(s); std::string item; while(std::getline(ss, item, delim)) { elems.push_back(item); } return elems; } std::vector<std::string> split(const std::string &s, char delim) { std::vector<std::string> elems; return split(s, delim, elems); } int _tmain(int argc, _TCHAR* argv[]) { string sentence = "Something i said\n which went \n wrong"; vector<string> str = split(sentence,'\n'); return 0; }

    Now you have all the extracted value inside your vector

    Thanks


    Rupesh Shukla