开发者

Splitting the string at Enter key

I'm getting the text from editbox and I'd want to get each name separated by enter key like the character string below with NULL characters.

    char *names = "Name1\0Name2\0Name3\0Name4\0Name5";

    while(*names)
    {
        names += strlen(names)+1;
    }

how would you do the same for enter开发者_如何学C key (i.e separated by /r/n) ? can you do that without using the std::string class?


Use strstr:

while (*names)
{
    char *next = strstr(names, "\r\n");
    if (next != NULL)
    {
        // If you want to use the key, the length is
        size_t len = next - names;

        // do something with a string here.  The string is not 0 terminated
        // so you need to use only 'len' bytes.  How you do this depends on
        // your need.

        // Have names point to the first character after the \r\n
        names = next + 2;
    }
    else
    {
        // do something with name here.  This version is 0 terminated
        // so it's easy to use

        // Have names point to the terminating \0
        names += strlen(names);
    } 
}

One thing to note is that this code also fixes an error in your code. Your string is terminated by a single \0, so the last iteration will have names point to the first byte after your string. To fix your existing code, you need to change the value of names to:

// The algorithm needs two \0's at the end (one so the final
// strlen will work and the second so that the while loop will
// terminate).  Add one explicitly and allow the compiler to
// add a second one.
char *names = "Name1\0Name2\0Name3\0Name4\0Name5\0";


If you want to start and finish with a C string, it's not really C++.

This is a job for strsep.

#include <stdlib.h>
void split_string( char *multiline ) {
    do strsep( &multiline, "\r\n" );
    while ( multiline );
}

Each call to strsep zeroes out either a \r or a \n. Since only the string \r\n appears, every other call will return an argument. If you wanted, you could build an array of char*s by recording multiline as it advances or the return value of strsep.

void split_string( char *multiline ) {
    vector< char* > args;
    do {
        char *arg = strsep( &multiline, "\r\n" );
        if ( * arg != 0 ) {
            args.push_back( arg );
        }
    } while ( multiline );
}

This second example is at least not specific to Windows.


Here's a pure pointer solution

   char * names = "name1\r\nName2\r\nName3";
   char * plast = names;

   while (*names)
      {
      if (names[0] == '\r' && names[1] == '\n')
         {
         if (plast < names)
            {
            size_t cch = names - plast;
            // plast points to a name of length cch, not null terminated.
            // to extract the name use
            // strncpy(pout, plast, cch);
            // pout[cch] = '\0';
            }
         plast = names+2;
         }
      ++names;
      }

   // plast now points to the start of the last name, it is null terminated.
   // extract it with
   // strcpy(pout, plast);


Since this has the C++ tag, the easiest would probably using the C++ standard library, especially strings and string streams. Why do you want to avoid std::string when you're doing C++?

std::istringstream iss(names);
std::string line;
while( std::getline(iss,line) )
  process(line); // do process(line.c_str()) instead if you need to 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜