简体   繁体   中英

Is There A Built-In Way to Split Strings In C++?

well is there? by string i mean std::string

Here's a perl-style split function I use:

void split(const string& str, const string& delimiters , vector<string>& tokens)
{
    // Skip delimiters at beginning.
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos     = str.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos)
    {
        // Found a token, add it to the vector.
        tokens.push_back(str.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = str.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = str.find_first_of(delimiters, lastPos);
    }
}

在C ++中没有内置的方法来分割字符串,但是boost提供字符串算法库来执行所有类型的字符串操作,包括字符串拆分

Yup, stringstream.

std::istringstream oss(std::string("This is a test string"));
std::string word;
while(oss >> word) {
    std::cout << "[" << word << "] ";
}

STL strings

You can use string iterators to do your dirty work.

std::string str = "hello world";

std::string::const_iterator pos = std::find(string.begin(), string.end(), ' '); // Split at ' '.

std::string left(str.begin(), pos);
std::string right(pos + 1, str.end());

// Echoes "hello|world".
std::cout << left << "|" << right << std::endl;
void split(string StringToSplit, string Separators)
{
    size_t EndPart1 = StringToSplit.find_first_of(Separators)
    string Part1 = StringToSplit.substr(0, EndPart1);
    string Part2 = StringToSplit.substr(EndPart1 + 1);
}

The answer is no. You have to break them up using one of the library functions.

Something I use:

std::vector<std::string> parse(std::string l, char delim) 
{
    std::replace(l.begin(), l.end(), delim, ' ');
    std::istringstream stm(l);
    std::vector<std::string> tokens;
    for (;;) {
        std::string word;
        if (!(stm >> word)) break;
        tokens.push_back(word);
    }
    return tokens;
}

You can also take a look at the basic_streambuf<T>::underflow() method and write a filter.

There is no common way doing this.

I prefer the boost::tokenizer , its header only and easy to use.

What the heck... Here's my version...

Note: Splitting on ("XZaaaXZ", "XZ") will give you 3 strings. 2 of those strings will be empty, and won't be added to theStringVector if theIncludeEmptyStrings is false.

Delimiter is not any element in the set, but rather matches that exact string.

 inline void
StringSplit( vector<string> * theStringVector,  /* Altered/returned value */
             const  string  & theString,
             const  string  & theDelimiter,
             bool             theIncludeEmptyStrings = false )
{
  UASSERT( theStringVector, !=, (vector<string> *) NULL );
  UASSERT( theDelimiter.size(), >, 0 );

  size_t  start = 0, end = 0, length = 0;

  while ( end != string::npos )
  {
    end = theString.find( theDelimiter, start );

      // If at end, use length=maxLength.  Else use length=end-start.
    length = (end == string::npos) ? string::npos : end - start;

    if (    theIncludeEmptyStrings
         || (   ( length > 0 ) /* At end, end == length == string::npos */
             && ( start  < theString.size() ) ) )
      theStringVector -> push_back( theString.substr( start, length ) );

      // If at end, use start=maxSize.  Else use start=end+delimiter.
    start = (   ( end > (string::npos - theDelimiter.size()) )
              ?  string::npos  :  end + theDelimiter.size()     );
  }
}


inline vector<string>
StringSplit( const  string  & theString,
             const  string  & theDelimiter,
             bool             theIncludeEmptyStrings = false )
{
  vector<string> v;
  StringSplit( & v, theString, theDelimiter, theIncludeEmptyStrings );
  return v;
}

C strings

Simply insert a \\0 where you wish to split. This is about as built-in as you can get with standard C functions.

This function splits on the first occurance of a char separator, returning the second string.

char *split_string(char *str, char separator) {
    char *second = strchr(str, separator);
    if(second == NULL)
        return NULL;

    *second = '\0';
    ++second;
    return second;
}

A fairly simple method would be to use the c_str() method of std::string to get a C-style character array, then use strtok() to tokenize the string. Not quite as eloquent as some of the other solutions listed here, but it's easy and works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM