【问题标题】:How can I replace \xA0 (or Non-ASCII) chars in a string to ' '?如何将字符串中的 \xA0 (或非 ASCII)字符替换为 ' '?
【发布时间】:2010-10-01 02:54:05
【问题描述】:

我有一个包含许多非 ASCII 字符的 excel 文件,我想用空格字符替换它们。

此文本将被输入到 MySQL 数据库中,并且不会在字符串中包含这些字符。我在尝试发布该行时收到 HY000 Incorrect string value

【问题讨论】:

    标签: delphi string non-ascii-characters


    【解决方案1】:

    如果非 Ascii 字符集是固定的,您可以使用:

    NewString := StringReplace(OriginalString,#1#4,' ',[rfReplaceAll])
    

    其中 #1#4 是您要替换的非 ascii 字符。

    Here is some docs on it's use.

    您也可以这样做。

    function StripNonAlpha(aInput : String) : String;
    var
     I : Integer;
    begin
     result := aInput;
     for I := 1 to length(result) do
     begin
       if not CharInSet(result[I],['A'..'Z','a'..'z']) then
          result[I] := ' ';
     end;
    end;
    

    然后你可以将 CharInSet 中的 Set 更改为可接受的字符。

    【讨论】:

    • 谢谢 Rob,工作得很好。对于 Delphi 7 函数 StripNonAlpha(aInput : String) : String; var I:整数;开始结果 := aInput; for I := 1 to length(result) 如果不是则开始(ValidChars 中的result[I])然后 result[I] := ' ';结尾;结尾;其中 ValidChars:Char 集 = ['0'..'9','A'..'Z','a'..'z','?','.','>','
    猜你喜欢
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2015-02-27
    • 2013-09-08
    • 1970-01-01
    • 2021-12-22
    • 2013-06-20
    • 1970-01-01
    相关资源
    最近更新 更多