空格可能是连续一片,或跳着出现
还有回车换行也要去掉
    str1:=StringReplace(str1, #13, '',[rfReplaceAll, rfIgnoreCase]);
    showmessage(str1);
    str1:=StringReplace(str1, #10, '',[rfReplaceAll, rfIgnoreCase]);
    showmessage(str1);
    str1:=StringReplace(str1, ' ', '',[rfReplaceAll, rfIgnoreCase]);
    showmessage(str1);
我这样写能去掉一部分,但去不掉所有的.
还有我想把str1[index]='';这样写怎么不行?另:字符串可能很长

解决方案 »

  1.   

    使用StringReplace应该可以去掉所有的空格、回车换行符号。str1[index]='';-》
    Delete(str1, Index, 1);
    Insert('xx', str1, Index);—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————
      

  2.   

    var
      str1: Widestring;
      str2:string;
      index:integer;
    begin
       str1:='aaaaaaaaaaaaaaaaaaaaaaaaaa';
       str2:='bbbbbbbbbbbbbbbbbbbbbbbbbb';
      index:=0;
      str1[index]:='';
      str2[index]:='';
    //................................
    [Error] java.pas(99): Incompatible types: 'WideChar' and 'String'
    [Error] java.pas(100): Incompatible types: 'Char' and 'String'
      

  3.   

    trim不行
    var
    str:String  ;
    beginstr:='               a b      c f       h          j';
    str:=trim(str);
    showmessage(Str);
    end;
    结果: a b      c f       h          j'
      

  4.   


    If the Flags parameter does not include rfReplaceAll, StringReplace only replaces the first occurrence of OldPattern in S. Otherwise, all instances of OldPattern are replaced by NewPattern.If the Flags parameter includes rfIgnoreCase, The comparison operation is case insensitive.只要你指定了rfreplaceall 就应该可以换掉所以的子串的!!
    实在不行你设一个循环:先用 pos 定位看看有没有子串,然后再stringreplace.应该可以成功的!
      

  5.   

    Trim字符串可以去掉字符串的前后多余的空格,
      

  6.   

    StringReplace在字符串短的时候可以,但是很长,比如偶读入了一段java源代码
    后,后面那部分就包含很多空格了,可能有255字符长度的问题.str1[index]='';这样写怎么不行?提示[Error] java.pas(100): Incompatible types: 'Char' and 'String'.我记得可以的呀另:来的都有分(偶写了trim不行的,再说trim的没有)
      

  7.   

    用AnsiReplaceStr()函数试试
      myStr:=Trim(myStr);
      myStr:=AnsiReplaceStr(myStr,' ','');
      

  8.   

    第一个问题换算法解决了第二问
    str1[index]='';这样写怎么不行?提示[Error] java.pas(100): Incompatible types: 'Char' and 'String'.
    怎么没人回答?
      

  9.   

    感觉stringreplace应该好用呀。 可能是你字符串太长的原因吧。没试着把字符串截短吗?
      

  10.   

    '' 是string 类型。
    Char 类型没有空值,如果想置空,一般用 #0。