转载:http://www.sharejs.com/codes/cpp/5780
1 /* 2 去掉字符串首尾的 \x20 \r \n 字符 3 by sincoder 4 */ 5 void clean_string(char *str) 6 { 7 char *start = str - 1; 8 char *end = str; 9 char *p = str; 10 while(*p) 11 { 12 switch(*p) 13 { 14 case ‘ ‘: 15 case ‘\r‘: 16 case ‘\n‘: 17 { 18 if(start + 1==p) 19 start = p; 20 } 21 break; 22 default: 23 break; 24 } 25 ++p; 26 } 27 //现在来到了字符串的尾部 反向向前 28 --p; 29 ++start; 30 if(*start == 0) 31 { 32 //已经到字符串的末尾了 33 *str = 0 ; 34 return; 35 } 36 end = p + 1; 37 while(p > start) 38 { 39 switch(*p) 40 { 41 case ‘ ‘: 42 case ‘\r‘: 43 case ‘\n‘: 44 { 45 if(end - 1 == p) 46 end = p; 47 } 48 break; 49 default: 50 break; 51 } 52 --p; 53 } 54 memmove(str,start,end-start); 55 *(str + (int)end - (int)start) = 0; 56 } 57 58 //该代码片段来自于: http://www.sharejs.com/codes/cpp/5780
时间: 2024-10-11 11:52:42