删除字符串中的空格的问题 [英] problem with removing spaces in the string
问题描述
假设我有这样的字符串:
我有一个字符串\和一个内部string\\\"我想删除
这个字符串中的空格但不在内部字符串中
在上面的字符串中我必须删除空格,但不要在内部
string(\"和一个内部字符串\\\")。有谁请告诉我怎么做
这个?
问候
Hi,
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?
regards
推荐答案
ramu写道:
ramu wrote:
假设我有一个这样的字符串:
我有一个string \"和一个内部字符串\\\"我想在此字符串中删除
空格但不在内部字符串中
在上面的字符串中我必须删除空格,但不能删除
内部字符串(\"和内部字符串\\\")。请有人请
告诉我该怎么做?
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove
space in this string but not in the inner string"
In the above string I have to remove spaces, but not in the
inner string(\"and a inner string\\\"). Will anyone please
tell me how to do this?
没问题。我想你有一个输入,比如
char s [] ="我有一个字符串\"和一个内部字符串\\\"我想要
删除此字符串中的空格但不删除
内部字符串中的空格" ;;
只需在遍历字符串时开始删除空格; <每当你看到'''''暂停空格移除时,当你看到另一个''''''恢复它时,
。别忘了在
结尾处停下来。
Ralf
No problem. I suppose you have an input like
char s[] = "I have a string \"and a inner string\\\" I want"
" to remove space in this string but not in the "
"inner string";
Just start removing spaces while you walk through the string;
whenever you see a ''"'' suspend the space removal and when you
see another ''"'' resume it. Don''t forget to stop at the end of
the string.
Ralf
"瑞木"写道:
"ramu" writes:
假设我有一个这样的字符串:
我有一个字符串\和一个内部字符串\\\我想删除
这个字符串中的空格但不在内部字符串中
在上面的字符串中我必须删除空格,但不要在内部
string(\"和一个内部字符串\\\")。有人请告诉我怎么做
这个?
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?
设置一个标志,表明您遇到了第一个分隔符。想想一个
或更多_states_。如果您仍有问题,请搜索有关模式和状态的信息并说明
图表。
Set a flag indicating you have encountered the first delimiter. Think of one
or more _states_. Search for information on modes and states and state
diagrams if you still have trouble.
9月19日下午12:16,锇" < r124c4u ... @ comcast.netwrote:
On Sep 19, 12:16 pm, "osmium" <r124c4u...@comcast.netwrote:
" ramu"写道:
"ramu" writes:
假设我有一个这样的字符串:
Suppose I have a string like this:
"我有一个字符串\和一个内部字符串\\\我想删除
这个字符串中的空格,但不是在内部字符串中删除
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
在上面的字符串中我必须删除空格,但不能删除内部
字符串(\"和内部字符串\\\。有人请告诉我怎么做
这个?
In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?
设置一个标志,表明您遇到了第一个分隔符。想想一个
或更多_states_。如果您仍有问题,请搜索有关模式和状态的信息并说明
图表。
Set a flag indicating you have encountered the first delimiter. Think of one
or more _states_. Search for information on modes and states and state
diagrams if you still have trouble.
我这样做了:
char * strrmspace(char * s)
{
char * sptr = s;
char * instr = s;
char * outstr;
int i = 0;
boolean found_quote = FALSE;
outstr =(char *)malloc(strlen(s)+1);
while(* instr!=''\''')
{
if(found_quote)
{
outstr [i ++] = * instr;
}
else
{
if( !isspace(* instr))
{
outstr [i ++] = * instr;
}
}
if((* instr ==''"'')&&(instr [-1]!=''\\'')) />
{
found_quote =!found_quote;
}
instr ++;
} / *而* /
outstr [i] =''\ 0'';
(无效)strcpy(s,outstr);
free(outstr);
return(sptr);
} / * strrmspace * /
如果我有一个字符串
char s [] ="我有一个字符串\" \\\"我想删除此
字符串中的空格,但不删除\内部字符串\中的空格" ;;
但这是删除\内部字符串\中的空格当前一个内部字符串中有
反斜杠时,即。 \" \\\"。
我不想删除内部字符串中的空格。我怎么能实现这个呢?
问候
I have done like this:
char *strrmspace (char *s )
{
char *sptr = s;
char *instr = s;
char *outstr;
int i = 0;
boolean found_quote = FALSE;
outstr = (char * )malloc(strlen(s)+1);
while ( *instr != ''\0'')
{
if ( found_quote )
{
outstr[i++] = *instr;
}
else
{
if ( !isspace( *instr) )
{
outstr[i++] = *instr;
}
}
if (( *instr == ''"'') && ( instr[-1] != ''\\''))
{
found_quote = !found_quote;
}
instr++;
} /* while */
outstr[i] = ''\0'';
(void)strcpy(s, outstr);
free (outstr);
return(sptr);
} /* strrmspace */
And if I have a string
char s[] = "I have a string \"\\\" I want to remove space in this
string but not in the \"inner string\" ";
But this is removing spaces in \"inner string\" when there is
backslash in the previous inner string ie. \"\\\".
I don''t want to remove the spaces in the inner strings. How can I
achieve this?
regards
这篇关于删除字符串中的空格的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!