从在C字符串中删除字符 [英] Remove characters from a string in C
问题描述
我只能访问'C',需要一个字符数组中替换字符。我还没有想出了这个相对简单的程序,任何清洁的解决方案。
我是通过一个字符数组,例如:
字符strBuffer [] = \"/html/scorm12/course/course_index.jsp?user_id=100000232&course_id=100000879&course_$p$pfix=ACQ&version=2&scorm_version=3&roster_id=100011365&course_name=Test%20Course%201.2&mode=browse&course_number=0000&mode_id=1\";
我需要修改此缓冲区,以取代所有的&放大器;放大器;
与&安培;
。所得缓冲器不必覆盖strBuffer(可以创建一个新的缓冲液)。
有什么建议?
编辑:
在过去,我已经做在一个循环中的strstr的功能,但一直在寻找一个简单的解决方案,也许是等价的C到与string.replace方法。
编辑:
对于我的眼前需要,以下是我所需要的。
字符strBuffer [] = \"/html/scorm12/course/course_index.jsp?user_id=100000232&course_id=100000879&course_$p$pfix=ACQ&version=2&scorm_version=3&roster_id=100011365&course_name=Test%20Course%201.2&mode=browse&course_number=0000&mode_id=1\";
焦炭strTemp [1024];
字符* S =(字符*)strBuffer;
INT I = 0;而(* S)
{
strTemp [我++] = * S;
如果(STRNCMP(S,&放大器;放大器;,5)== 0)
{
S + = 5;
}
其他
小号++;
}
strTemp [I] = 0;
未来修改:
- 创建一个效用函数来存储此功能。
- 通过查找字符串作为参数。</ li>
- 确定搜索字符串的长度,所以很难$ C $ 5 CD的可以被删除。
- 动态分配strTemp变量。
- 错误检查空字符串和字符未找到。
编辑:
我创建了一个博客帖子详细的步骤,并提供了更灵活的解决方案:
的http://www.solutionmaniacs.com/blog/2012/11/25/c-removereplace-characters-in-a-string.html
的char * S =(字符*)strBuffer;
焦炭sClean [strlen的(strBuffer)+ 1]; / * +1空字节* /
/ *如果上面没有你的编译工作,请使用:
字符* sClean =(字符*)malloc的(的sizeof(strBuffer)+ 1);
* /
INT I = 0;
而(* S)
{
sClean [我++] = * S;
如果((* S =='和;')及与放大器;!(STRNCMP(S,与&amp;放大器;,5))S + = 5;
其他小号++;
}
sClean [I] = 0;
I only have access to 'C' and need to replace characters within a character array. I have not come up with any clean solutions for this relatively simple procedure.
I am passed a character array, for example:
char strBuffer[] = "/html/scorm12/course/course_index.jsp?user_id=100000232&course_id=100000879&course_prefix=ACQ&version=2&scorm_version=3&roster_id=100011365&course_name=Test%20Course%201.2&mode=browse&course_number=0000&mode_id=1";
I need to modify this buffer to replace all the &
with &
. The resulting buffer does not have to overwrite strBuffer (a new buffer can be created).
Any suggestions?
Edit:
In the past I have done the strstr function in a loop, but was looking for a simpler solution, perhaps the C equivalent to the String.Replace method.
Edit:
For my immediate needs, the following is all that I need.
char strBuffer[] = "/html/scorm12/course/course_index.jsp?user_id=100000232&course_id=100000879&course_prefix=ACQ&version=2&scorm_version=3&roster_id=100011365&course_name=Test%20Course%201.2&mode=browse&course_number=0000&mode_id=1";
char strTemp[1024];
char *s = (char*)strBuffer;
int i=0;
while (*s)
{
strTemp[i++] = *s;
if (strncmp(s,"&",5) == 0)
{
s += 5;
}
else
s++;
}
strTemp[i] = 0;
Future modifications:
- Create a utility function to store this function.
- Pass the search string as a parameter.
- Determine the search string's length, so the hardcoded 5's can be removed.
- Dynamically allocate the strTemp variable.
- Error checking for empty strings and chars not found.
EDIT:
I created a blog post to detail the steps and provide a more flexible solution:
http://www.solutionmaniacs.com/blog/2012/11/25/c-removereplace-characters-in-a-string.html
char *s = (char*)strBuffer;
char sClean[strlen(strBuffer) + 1]; /* +1 for null-byte */
/* if above does not work in your compiler, use:
char *sClean = (char*)malloc(sizeof(strBuffer) + 1);
*/
int i=0;
while (*s)
{
sClean[i++]= *s;
if ((*s == '&') && (!strncmp(s, "&", 5)) s += 5;
else s++;
}
sClean[i] = 0;
这篇关于从在C字符串中删除字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!