代码之家  ›  专栏  ›  技术社区  ›  Robert Zunr

从字符串中剥离字符

c#
  •  0
  • Robert Zunr  · 技术社区  · 6 年前

    我正在发送带有以下代码的请求:

    https://example.com “,”POST“,formData);

    转换 byte string 有了这个:

    string responsefromserver = Encoding.UTF8.GetString(responseBytes);
    

    responsefromserver "abcd"

    我想脱光衣服 " 角色。因此,我使用以下方法:

    Console.WriteLine(responsefromserver.Replace('"', ''));
    

    但是 '' 显示此错误: Empty character literal

    string.Empty 而不是 '' Argument 2: cannot convert from 'string' to 'char'

    我该怎么脱光衣服 字符串中的字符?

    2 回复  |  直到 6 年前
        1
  •  4
  •   David Zemens    6 年前

    根本没有 Char.Empty like there is a String.Empty ,所以您需要使用 String.Replace String

    您需要对双引号进行转义,因此应该是:

    Replace("\"", "");
    

    或:

    Replace("\"", String.Empty);
    
        2
  •  0
  •   Nydirac    6 年前

    你有一些选择:

    responsefromserver.Replace("\"", "");//escaping the "
    
    responsefromserver.Replace('"', '\0'); //null symbol <- not reccomended, only to allow you to use the char overload. It will NOT remove the " but replace with a null symbol!
    responsefromserver.Replace('"', (char)0); // same as above in another format
    

    在您的例子中,您可以使用Trim:这有一个额外的好处,即只从字符串的第一/最后一个位置删除字符,从而允许它的其余部分包含“:

    responsefromserver.Trim('"'); // Remove first/last "