strtok作为字符串替换功能问题? [英] strtok as string replace function problem ?

查看:83
本文介绍了strtok作为字符串替换功能问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我选择了strtok用于我的字符串替换功能。

但是我丢失了最后一个令牌,如果有的话。


此字符串将被替换


选择name,vorname,userid,passwort等。来自用户通过这个来订购

" users"




select& quot; name& quot ;,& quot; vorname& quot;,& quot; userid& quot;,

& quot; passwort& quot;来自& quot; users& quot;由& quot;用户订购


因此最后的一行丢失。


代码中是否缺少某些东西?


谢谢,Lothar


lb_I_String& LB_STDCALL lbString :: replace(const char * toReplace,const

char * with){

//不要担心我的对象变量: - )

UAP_REQUEST(getModuleInstance(),lb_I_String,rep)


//字符串

char * token = strtok(stringdata,toReplace);


if((token!= NULL)&&(token!= stringdata)){

* rep + = with;

}


while(令牌!= NULL)

{

* rep + = token;

token = strtok(NULL,toReplace);

if(token!= NULL)* rep + = with;

}


setData(rep-> charrep());


返回* this;

}

Hi,

I have selected strtok to be used in my string replacement function.
But I lost the last token, if there is one.

This string would be replaced

select "name", "vorname", "userid", "passwort" from "users" order by
"users"

by this one:

select "name", "vorname", "userid",
"passwort" from "users" order by "users

Thus the last quot is lost.

Is there something missing in the code ?

Thanks, Lothar

lb_I_String& LB_STDCALL lbString::replace(const char* toReplace, const
char* with) {
// Don''t worry about my object variables :-)
UAP_REQUEST(getModuleInstance(), lb_I_String, rep)

// the string
char* token = strtok(stringdata, toReplace);

if ((token != NULL) && (token != stringdata)) {
*rep += with;
}

while(token != NULL)
{
*rep += token;
token = strtok(NULL, toReplace);
if (token != NULL) *rep += with;
}

setData(rep->charrep());

return *this;
}

推荐答案

文章< 11 ********************** @ q69g2000hsb.googlegroups .com> ;,

Lothar Behrens< lo ************ @ lollisoft.dewrote:
In article <11**********************@q69g2000hsb.googlegroups .com>,
Lothar Behrens <lo************@lollisoft.dewrote:

>我选择了strtok用于我的字符串替换功能n。
但是如果有的话,我丢失了最后一个令牌。
>I have selected strtok to be used in my string replacement function.
But I lost the last token, if there is one.


>此字符串将被替换为
>This string would be replaced


> select " name"," vorname"," userid"," passwort"来自用户通过
用户订购
>select "name", "vorname", "userid", "passwort" from "users" order by
"users"


>由这一个:
>by this one:


> select& amp ;"& quot;,& quot; vorname& quot;,& quot; userid& quot;,
& quot; passwort& quot;来自& quot; users& quot;订单按& quot; users
>select &quot;name&quot;, &quot;vorname&quot;, &quot;userid&quot;,
&quot;passwort&quot; from &quot;users&quot; order by &quot;users


>因此最后一个参数丢失。
>Thus the last quot is lost.


>代码中是否缺少某些内容?
>Is there something missing in the code ?


> lb_I_String& LB_STDCALL lbString :: replace(const char * toReplace,const
char * with){
>lb_I_String& LB_STDCALL lbString::replace(const char* toReplace, const
char* with) {



你确定这不是C ++吗? ::是可疑的,而且&不能
出现在C的那个位置。

Are you sure this isn''t C++? The :: is suspicious, and the & cannot
occur in that position in C.


> //不要担心我的对象变量:-)
> // Don''t worry about my object variables :-)


//字符串
char * token = strtok(stringdata,取代);
// the string
char* token = strtok(stringdata, toReplace);



序列中的第一个调用搜索指向

的字符串s1表示当前

由s2指向的分隔符字符串。如果找不到这样的字符

,则s1

指向的字符串中没有标记,strtok函数返回空指针。


这意味着如果字符串以分隔符结尾,

那么strtok()将*不*返回指向空行的指针。

srtrok()实际上无法区分

的字符串以\0结尾的字符串和以任意数字结尾的字符串

给定的分隔符:你有通过

来跟踪最后一个令牌的结束位置并将其与字符串结尾的已知位置进行比较。

-

那时候我很年轻,但我也很沮丧。

- 克里斯托弗牧师

The first call in the sequence searches the string pointed to
by s1 for first character that is not contained in the current
separator string pointed to by s2. If no such character is
found, then there are no tokens in the string pointed to by s1
and the strtok function returns a null pointer."

What this implies is that if the string ends in the delimiter,
then strtok() will *not* return a pointer to the empty line.
srtrok() effectively cannot distinguish between the case of
the string ending in \0 and the string ending in any number
of the given delimiters: you have to do that yourself by
keeping track of where the last token ends and comparing that to
the known position of the end of the string.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest


6月5日,22:54,rober ... @ ibd.nrc-cnrc.gc.ca(Walter Roberson)写道:
On 5 Jun., 22:54, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:

>

你确定这不是C ++吗? ::是可疑的,而且&不能在B的那个位置发生

>
Are you sure this isn''t C++? The :: is suspicious, and the & cannot
occur in that position in C.



是的,对不起。但是我把完整的函数用来表示C ++代码。

因为

也是+运算符:-)

Yes, sorry. But I put the complete function to indicate C++ code.
Because there
are also + operators used :-)


>

这意味着如果字符串以分隔符结尾,那么
thenstrtok()将*不*返回指向空行的指针。 br />
srtrok()实际上无法区分

的字符串以\0结尾的字符串和以任意数字结尾的字符串

的给定分隔符:你必须自己通过

跟踪最后一个标记的结束位置并将其与字符串结尾的已知位置进行比较。
>
What this implies is that if the string ends in the delimiter,
thenstrtok() will *not* return a pointer to the empty line.
srtrok() effectively cannot distinguish between the case of
the string ending in \0 and the string ending in any number
of the given delimiters: you have to do that yourself by
keeping track of where the last token ends and comparing that to
the known position of the end of the string.



使用标志并确定尾随令牌将有助于......


谢谢

Lothar

Using a flag and determining a trailing token would then help...

Thanks

Lothar


Lothar Behrens写道:
Lothar Behrens wrote:

6月5日, 22:54,rober ... @ ibd.nrc-cnrc.gc.ca(Walter Roberson)写道:
On 5 Jun., 22:54, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:


你确定这不是'不是C ++? ::是可疑的,而且&不能
出现在C的那个位置。

Are you sure this isn''t C++? The :: is suspicious, and the & cannot
occur in that position in C.



是的,对不起。但我把完整的函数用来表示C ++代码。


Yes, sorry. But I put the complete function to indicate C++ code.



这意味着你在错误的新闻组中。你想要comp.lang.c ++。

实际上,你也可能想要以完全不同的方式解决问题。


Brian

Which means you are in the wrong newsgroup. You want comp.lang.c++.
Actually, you also probably want to do the problem quite differently.


Brian


这篇关于strtok作为字符串替换功能问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆