char testStr[] = " trim this ";
char** pTestStr = &testStr;
trim(pTestStr);
int trim(char** pStr)
{
char* str = *pStr;
while(isspace(*str)) {
(*pStr)++;
str++;
}
if(*str == 0) {
return 0;
}
char *end = str + strlen(str) - 1;
while(end > str && isspace(*end))
end--;
*(end+1) = 0;
return 0;
}
1
A
回答
2
char testStr[] = " trim this ";
char* pTestStr = &testStr[0];
trim(&pTestStr);
void trim(char* str)
{
if(!str)
return;
char* ptr = str;
int len = strlen(ptr);
while(len-1 > 0 && isspace(ptr[len-1]))
ptr[--len] = 0;
while(*ptr && isspace(*ptr))
++ptr, --len;
memmove(str, ptr, len + 1);
}
4
你需要讓testStr
可寫:
char testStr[] = " trim this ";
的問題是,char *ptr = ...
有ptr
指向實際文字字符串是在只讀存儲器中。
通過使用char testStr[] = ...
您正在分配一個數組,並使用與文字字符串相同的內容初始化數組。由於這是一個數組,它是可寫的。
0
編輯:更新基於zString庫的最新版本的代碼。
以下是我對trim
,left-trim
和right-trim
函數(將被添加到zString string library)的實現。
儘管函數返回*char
,由於原始字符串被修改,所以這些將用於您的目的。
可以使用標準庫函數,如isspace()
,但下面的實現是裸露骨代碼,它依賴於庫函數。
/* trim */
char *zstring_trim(char *str){
char *src=str; /* save the original pointer */
char *dst=str; /* result */
char c;
int is_space=0;
int in_word=0; /* word boundary logical check */
int index=0; /* index of the last non-space char*/
/* validate input */
if (!str)
return str;
while ((c=*src)){
is_space=0;
if (c=='\t' || c=='\v' || c=='\f' || c=='\n' || c=='\r' || c==' ')
is_space=1;
if(is_space == 0){
/* Found a word */
in_word = 1;
*dst++ = *src++; /* make the assignment first
* then increment
*/
} else if (is_space==1 && in_word==0) {
/* Already going through a series of white-spaces */
in_word=0;
++src;
} else if (is_space==1 && in_word==1) {
/* End of a word, dont mind copy white spaces here */
in_word=0;
*dst++ = *src++;
index = (dst-str)-1; /* location of the last char */
}
}
/* terminate the string */
*(str+index)='\0';
return str;
}
/* right trim */
char *zstring_rtrim(char *str){
char *src=str; /* save the original pointer */
char *dst=str; /* result */
char c;
int is_space=0;
int index=0; /* index of the last non-space char */
/* validate input */
if (!str)
return str;
/* copy the string */
while(*src){
*dst++ = *src++;
c = *src;
if (c=='\t' || c=='\v' || c=='\f' || c=='\n' || c=='\r' || c==' ')
is_space=1;
else
is_space=0;
if (is_space==0 && *src)
index = (src-str)+1;
}
/* terminate the string */
*(str+index)='\0';
return str;
}
/* left trim */
char *zstring_ltrim(char *str){
char *src=str; /* save the original pointer */
char *dst=str; /* result */
char c;
int index=0; /* index of the first non-space char */
/* validate input */
if (!str)
return str;
/* skip leading white-spaces */
while((c=*src)){
if (c=='\t' || c=='\v' || c=='\f' || c=='\n' || c=='\r' || c==' '){
++src;
++index;
} else
break;
}
/* copy rest of the string */
while(*src)
*dst++ = *src++;
/* terminate the string */
*(src-index)='\0';
return str;
}
相關問題
- 1. Golang:字符串修剪功能
- 2. 修剪功能沒有真正修剪
- 3. 回聲修剪字符串
- 4. 修剪字符串
- 5. 修剪字符串
- 6. 修剪字符串
- 7. 修剪字符串
- 8. 修剪字符串在Javascript
- 9. 在C#中建立一個智能字符串修剪功能
- 10. 修剪數字功能
- 11. 修剪字符串NHibernate的
- 12. Java字符串修剪功能不起作用
- 13. 字符串修剪功能不起作用
- 14. 修剪字符和修剪字符串 - Python
- 15. 字符串修剪/子字符串C#
- 16. 試圖修剪'字符串'字符串?
- 17. Vbscript修剪功能
- 18. 完全修剪字符串
- 19. 修剪字符串與Dapper.NET
- 20. php修剪字符串
- 21. Swift修剪字符串
- 22. php修剪功能修剪額外的小於/大於符號
- 23. 修剪在C#中的字符串
- 24. 字符串空白清理不修剪
- 25. 腳本字符串不修剪
- 26. 從字符串修剪字符
- 27. 函數返回子和修剪串
- 28. 修剪部分返回的字段
- 29. 在C++中修剪字符串
- 30. 如何在JavaScript中修剪字符串?
重複:http://stackoverflow.com/questions/122616/painless-way-to-trim-leading-trailing-whitespace-in-c – indiv 2010-06-17 19:37:48