C++中实现字符串分割方法

来源:互联网 发布:网络点播系统 编辑:程序博客网 时间:2024/05/21 00:17

作为开头:在Java语言中,对String类的对象中都有一个splite方法来根据我们自己设定的分隔符“#”来将一串数字分割为一个个小的字符串,来达到我们的目的;这对于实现一些协议等是至关重要的;下面举一个例子来说明下问题;

例如:

我们有字符串“”abc#def#ghi“,我们的目标是将其按照“#”来将其分割开,最后的结果为:

abc

def

ghi

在Java中有split函数,原型如下:

public String[] split(String regex);
其中参数为“分割符”,返回值为string对象数组来存放分割后的子字符串;

例如,字符串 "boo:and:foo" 使用这些表达式可生成以下结果:

Regex结果:{ "boo", "and", "foo" }o{ "b", "", ":and:f" }
(参考JDK1.6);
而在C++中,我们发现在string中并不存在这样的一个函数,但是在C语言中的<string.h>头文件有一个函数能够达到我们的目的,原型如下:
char * strtok ( char * str, const char * delimiters );
其接收2个参数,第一个为指向源字符串的指针(C分隔的),第二个为一个const char *类型,其为我们的分隔字符串,这里的返回值为一个指针,指向每次划分后的字符串的首地址;
下面用程序说明其用法:
#include <stdio.h>#include <string.h>int main (){  char str[] ="This#a#sample#string";  char * pch;  printf ("Splitting string \"%s\" into tokens:\n",str);  pch = strtok (str,"#");  while (pch != NULL)  {    printf ("%s\n",pch);    pch = strtok (NULL, "#");  }  return 0;}
结果:
This
a
sample
string
</pre><pre code_snippet_id="1897642" snippet_file_name="blog_20160924_8_771657" name="code" class="cpp">这里对此函数说明的有两点,第一点是第一个参数,第二个是其在内存中的变化:
第一个参数说明:
<pre name="code" class="cpp">On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of the last token as the new starting location for scanning.
To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token. The scan also stops if the terminating null character is found.
This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function.
<span style="font-family: Arial, Helvetica, sans-serif;">以上为官方文档给出的解释,其说第一次调用此函数的时候需要我们传入一个字符串的首地址,并将第一个非分隔符的字符作为第一个位置,在第二次的时候其希望是一个空指针,</span></span>
以便继续查看字符串,直到在字符串中遇到‘\0’字符结束。
内存中的字符串:
我们发现,在第一次运行到char *p = strtok(c, "#");这条语句后,在[4]的位置已经将分割符变为了'\0',而其他的不变;这也就是我们能够打赢出各个字符窜的原因;其它
以此类推,直到字符串的末尾;在文档中给出了返回值的说明:If a token is found, a pointer to the beginning of the token.
Otherwise, a null pointer.
null pointer is always returned when the end of the string (i.e., a null character) is reached in the string being scanned.
以上为对strtok函数的说明;
下面说下在编程中遇到的C分隔的字符串和C++字符串之间的转换:1、C风格字符串转C++字符串:
char c[]="hello world";
转换方法:    string s(c);
可以看到,直接构造stirng对象即可;
2、C++转C风格字符串:
string s="hello world";
       const char *p=s.c_str();
正如以上,我们转换以后是一个const char *类型的,此时,为了能够对字符串进行处理,我们利用strcpy函数来进行转换;
      char temp[100];
       strcpy(temp,p);
OK,我们可以对以上的temp的C风格字符串进行处理了;
ps:当我们要在终端获取字符串的时候,在C++中,我们可以利用
string ss;
getline(std::cin,ss);
来将终端上的包含空格的字符串写入到ss中,此时不能适应cin>>ss,虽然ss也重载了》操作符。因为cin是以空格来进行分割的。
除了以上的方法,我们可以利用C语言中为我们提供的gets方法来获取;
char str[100];
gets(str);
这样也是可以将空格写入到str中的。利用puts来输出即可。同样需要注意的是,scanf("%s",str)也是以空格分割的,所以只能读到第一个字符;

0 0
原创粉丝点击