Thread: String

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    String

    Is there a function to split a string by a character like if you use that on a string: "do killing", it splits it to "do" and "killing" strings

  2. #2
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    not sure if there's a library function to do that but something like this will work

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <vector>
    
    using namespace std;
    
    //returns true if c is a space
    bool space ( char c ){
    
    	    return isspace(c);
    
         }
    //returns true if c is not a space
    bool not_space ( char c ){
    
    		return !isspace(c);
    
         }
    //split function 
    vector<string> split ( string s ){
    		      //i will be at the beginning of the string
    		      string::iterator i = s.begin();
    
    	              //vector of the words that we will return
    		      vector<string> ret;
    
    		      //run the loop until we reach the end
    		      while ( i != s.end() ){
    			     //find the first nonspace character from i 
    			     i = find_if ( i, s.end(), not_space );
    			    
    			     //if i found a nonspace character
    			     if ( i != s.end() ){
    
    				 //j will be at the first space character
    			     	 string::iterator j = i;
    
                             	 //find the space, if there is not, s.end() will be returned
    				 j = find_if ( i, s.end(), space );
    
    				 //push back the string delimited by  [i, j)
    			         ret.push_back ( string ( i, j ) );
    
    				 //set i equal to j to look through the rest of the string
    		                 i = j;
                        
                                  }
    
    		       }
    
    		       return ret;
    
    		}
    
    int main(){
    
    	 string s = "     hello world";
    
    	 vector<string> vec = split(s);
    
    	 cout << vec[0] << endl << vec[1];
    
    	 return 0;
    
        }
    That will output

    hello
    world


    but if you're just receiving input, either from a file or from cin, the input stream will ignore the whitespace unless you say so by using noskipws

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    
             string s;
    
    	 while ( cin >> s ){
    
    		cout << s << endl;
    
    	}
    
    	return 0;
    
        }
    that will work just fine also. try running it and typing something like "hey there stranger" or something, and you'll see that the output is

    hey
    there
    stranger
    Last edited by dra; 08-22-2005 at 10:15 AM.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Thanks

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
    	vector<string> tokens;
    	string s1 = ("Split Me Up! Word1 Word2 Word3");
    	string s2;
    	stringstream ss ( s1 );
    	while ( ss >> s2 )
    	{
    		cout << s2 << endl;
    	}
    	cin.get();
    	return 0;
    }

  5. #5
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Or you could use the good old strtok method which also allows you to split a string. strtok is a little more situational I think, but it should be perfect to split "do killing" into "do" and "killing"

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  7. #7
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Here are my string splitting and trimming functions.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    ////////////////////////////////////////////////////////////////////////////////
    
    string& trim_string(string &str);
    string& trim_string(string &str,
                        const string &pre_chars,
                        const string &post_chars);
    string split_string(string &str,
                        const string &delim_chars,
                        bool trim_space = true);
    
    ////////////////////////////////////////////////////////////////////////////////
    
    // trim surrounding space from a string
    string& trim_string(string &str) {
      return trim_string(str, " \t\r\n", " \t\r\n");
    }
    
    // trim specified characters around a string
    string& trim_string(string &str,
                        const string &pre_chars,
                        const string &post_chars)
    {
      // only do something if the string is not empty
      if (str.length() > 0) {    
        string::size_type first = str.find_first_not_of(pre_chars);
        string::size_type last = str.find_last_not_of(post_chars);
        
        // if none of the pre_chars are located, just clear the string
        if (first == string::npos)
          str.erase();
        // otherwise grab a substring after pre_chars and before post_chars
        else
          str = str.substr(first,
            first != string::npos && last != string::npos
            ?
              last - first + 1
            :
              string::npos);
      }
      
      return str;  
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    
    // this splits part of a string from str using any of delim_chars
    // to do the tokenizing
    string split_string(string &str,
                        const string &delim_chars,
                        bool trim_space)
    {
      string split;
    
      // search for any of the delim_chars in the source string
      string::size_type n = str.find_first_of(delim_chars);
      // if none were found, split the entire string apart and leave
      // the source empty
      if (n == string::npos) {
        split = str;
        str.erase();
      }
      // otherwise separate the string apart from the source, insuring
      // that we drop the delimiting character
      else {
        split = str.substr(0, n);
        str = str.substr(n + 1);
      }
    
      // if the user desires, we trim the space around the split
      // string and the source string
      if (trim_space) {
        trim_string(split);
        trim_string(str);
      }
    
      return split;
    }
    
    ////////////////////////////////////////////////////////////////////////////////
    
    // here is an example
    int main() {
      string str = "Peter Griffin, Quahog, Rhode_Island";
      while (str.length() > 0) {
        string next = split_string(str, ", ");
        cout << next << endl;
      }
      return 0;
    }
    Here's the output:
    Quote Originally Posted by output
    Peter
    Griffin
    Quahog
    Rhode_Island
    Last edited by LuckY; 08-22-2005 at 12:43 PM.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    How to do it with a char* variable?
    Last edited by maxorator; 10-30-2005 at 12:34 AM.

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Very carefully .
    Very similary but you would use strcpy and such.
    Oh and lucky gigity gigity gigity alright!
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM