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.
我只能访问“C”,需要替换字符数组中的字符。对于这个相对简单的过程,我没有提出任何干净的解决方案。
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).
我需要修改这个缓冲区来替换所有的& &;&。产生的缓冲区不必重写strBuffer(可以创建一个新的缓冲区)。
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.
在过去,我在一个循环中完成了strstr函数,但是我正在寻找一个更简单的解决方案,也许C等价于字符串。替代方法。
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.
- 确定搜索字符串的长度,因此可以删除硬编码的5。
- Dynamically allocate the strTemp variable.
- 动态分配strTemp变量。
- 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
http://www.solutionmaniacs.com/blog/2012/11/25/c-removereplace-characters-in-a-string.html
4 个解决方案
#1
1
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;
#2
5
C isn't noted for it's ease of use, especially when it comes to strings, but it has some rather nice standard library functions that will get the job done. If you need to work extensively on strings you'll probably need to know about pointers and pointer arithmetic, but otherwise here are some library functions that will undoubtedly help you:
C并不以它的易用性而著称,尤其是当它涉及到字符串时,但是它有一些相当不错的标准库函数来完成这项工作。如果你需要大量研究字符串,你可能需要了解指针和指针算法,否则这里有一些库函数无疑会对你有所帮助:
-
strchr()
to find a character (say,'&'
) in a string. - strchr()在字符串中查找字符(比如'&')。
-
strcmp()
andstrncmp()
to compare two strings. - strcmp()和strncmp()来比较两个字符串。
-
strstr()
to find a substring in a string (probably easier and faster than using thestrchr()
/strcmp()
combination). - 在字符串中查找子字符串(可能比使用strchr()/strcmp()组合更容易、更快)。
-
malloc()
to allocate a new string. - malloc()用于分配新字符串。
#3
4
Basically, you need to:
基本上,你需要:
- use the
strstr()
function to find the "&"s - 使用strstr()函数来查找“&”。
- copy characters to the resulting buffer up to the position found
- 将字符复制到所找到的缓冲区
- skip 4 characters
- 跳过4字符
- repeat until NUL
- 重复,直到空
#4
2
Allocate another buffer, either on the stack or the heap, and then copy the string into the new buffer 1 character at a time. Make special handling when you encounter the &
character.
在堆栈或堆上分配另一个缓冲区,然后每次将字符串复制到新的缓冲区1字符中。遇到&字符时要特别处理。
#1
1
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;
#2
5
C isn't noted for it's ease of use, especially when it comes to strings, but it has some rather nice standard library functions that will get the job done. If you need to work extensively on strings you'll probably need to know about pointers and pointer arithmetic, but otherwise here are some library functions that will undoubtedly help you:
C并不以它的易用性而著称,尤其是当它涉及到字符串时,但是它有一些相当不错的标准库函数来完成这项工作。如果你需要大量研究字符串,你可能需要了解指针和指针算法,否则这里有一些库函数无疑会对你有所帮助:
-
strchr()
to find a character (say,'&'
) in a string. - strchr()在字符串中查找字符(比如'&')。
-
strcmp()
andstrncmp()
to compare two strings. - strcmp()和strncmp()来比较两个字符串。
-
strstr()
to find a substring in a string (probably easier and faster than using thestrchr()
/strcmp()
combination). - 在字符串中查找子字符串(可能比使用strchr()/strcmp()组合更容易、更快)。
-
malloc()
to allocate a new string. - malloc()用于分配新字符串。
#3
4
Basically, you need to:
基本上,你需要:
- use the
strstr()
function to find the "&"s - 使用strstr()函数来查找“&”。
- copy characters to the resulting buffer up to the position found
- 将字符复制到所找到的缓冲区
- skip 4 characters
- 跳过4字符
- repeat until NUL
- 重复,直到空
#4
2
Allocate another buffer, either on the stack or the heap, and then copy the string into the new buffer 1 character at a time. Make special handling when you encounter the &
character.
在堆栈或堆上分配另一个缓冲区,然后每次将字符串复制到新的缓冲区1字符中。遇到&字符时要特别处理。