简体   繁体   English

修剪char数组C ++

[英]trim char array C++

How do i trim a char array ? 我如何修剪一个char数组?

I'm reading this array from the text file and when it matches a certain string , ive to process it. 我正在从文本文件中读取此数组,当它与某个字符串匹配时,就可以对其进行处理。 But it never matches. 但它永远不会匹配。 I assume because it contains some trailing and leading spaces or\\and '\\n' 我认为是因为它包含一些尾随和前导空格或\\和'\\ n'

I dont know how to trim a char array but i used this to removes spaces. 我不知道如何修剪char数组,但是我用它来删除空格。 This has a problem that when token[0] = {0 0 0 0} this makes it {0000}. 这有一个问题,当token[0] = {0 0 0 0} ,它成为{0000}。 So i dont know what to do next.. 所以我不知道下一步该怎么办..

char a[50] = {0};
int j = 0;

    //Removing spaces or garbage values in token[0] for perfect string match
    for(int i=0; (unsigned)i<strlen(token[0]) ; ++i )
    {
      if( (isalnum(token[0][i])) || token[0][i] == 95)
      {
        a[j]=token[0][i];
        j++;
      }
      else
      {
        continue;
      }
    }

Given that you have not mentioned anything else, the following could always be done: 鉴于您没有提到其他任何内容,可以始终执行以下操作:

copy the array(to be checked) to another array char by char to only the number of chars required...(this way you could also check how many chars you need to trim): 将每个要复制的数组(待检查)复制到另一个数组char中,仅复制所需的字符数...(这样,您还可以检查需要修剪的字符数):

a=0;

while(array1[a] != '\0'){
    array2[a] = array1[a];
    a++;
}

array2[a] = '\0';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM