我基本上想删除数组中第一个有效字符之前的所有前导空格。
例如,如果我有类似 ' 1.6, 1.7'
的内容,我希望它是 '1.6, 1.7'
或者只是 '1.7 , 1.8'
,那么它将是 '1.7, 1.8'
这是我处理空白的方法,但它只显示空白所在的位置。我需要帮助删除它。
char **storeArray
void Students::removeSpace()
{
int MAX_SIZE = 30;
for(int i=0; i<3; i++)
{
for(int j=0; j<MAX_SIZE; j++)
{
if(isspace(storeArray[i][j]) && !(isspace(storeArray[i][j++])
{
// I NEED HELP HERE. I'M TRYING TO REMOVE ALL THE LEADING WHITESPACE ONLY
}
}
}
}
最佳答案
要删除多余的空格,请遍历字符串:
void Remove_Leading_ExtraMiddle_Trailing_Whitespace(char *s, int size) {
char *end = &s[size];
char *t = s;
// skip leading
while (isspace(*s))
s++;
// middle
for (;;) {
while (!isspace(*s) && *s != '\0') {
*t++ = *s++;
}
if (*s == '\0')
break;
*t = *s++;
while (isspace(*s))
s++;
if (*s == '\0') {
break;
}
t++;
}
// end
while (t < end) {
*t++ = '\0';
}
}
void removeSpace() {
int MAX_SIZE = 30;
char storeArray[4][MAX_SIZE];
for (int i = 0; i < 3; i++) {
Remove_Leading_ExtraMiddle_Trailing_Whitespace(storeArray[i], MAX_SIZE);
}
}
关于c - 删除第一个有效字符 C 之前的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27433809/