想要用给定的十六进制代码值形成一个字符串

发布于 2024-08-03 02:02:49 字数 429 浏览 2 评论 0原文

我想用其他字符替换输入字符串中的某些字符。

输入文本具有 Microsoft 左右智能引号,我想将其转换为单个“。

我计划使用替换操作,但在形成要搜索的文本字符串时遇到问题。

我想替换输入序列(十六进制)\xE2809C,并将该序列更改为单个“。 \xE2809D 同上。

如何形成要在替换操作中使用的字符串?

我正在考虑类似的事情(在循环中):

tempTxt = tempTxt.Replace(charsToRemove[i], charsToSubstitute[i]);

但我在创建 charsToRemove 数组时遇到问题。

也许一个更大的问题是是否可以使用 C# 中的一些读/写和字符串转换来读取整个输入文件并将其转换为纯 ASCII。

谢谢,迈克

I want to replace certain characters in an input string with other characters.

The input text has Microsoft left and right smart quotes which I would like to convert to just a single ".

I was planning on using the Replace operation, but am having trouble forming the text string to be searched for.

I would like to replace the input sequence (in hex) \xE2809C, and change that sequence to just a single ". Ditto with \xE2809D.

How do I form the string to use in the Replace operation?

I'm thinking of something like (in a loop):

tempTxt = tempTxt.Replace(charsToRemove[i], charsToSubstitute[i]);

but I'm having trouble creating the charsToRemove array.

Maybe a bigger question is whether the whole input file can be read and converted to plain ASCII using some read/write and string conversions in C#.

Thanks, Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小忆控 2024-08-10 02:02:49

像这样的东西吗?

char [] charsToRemove = {
    '\u201C', // These are the Unicode code points (not the UTF representation)
    '\u201D'
};

char [] charsToSubstitute = {
    '"',
    '"'
};

Something like this?

char [] charsToRemove = {
    '\u201C', // These are the Unicode code points (not the UTF representation)
    '\u201D'
};

char [] charsToSubstitute = {
    '"',
    '"'
};
ˇ宁静的妩媚 2024-08-10 02:02:49

您可能想尝试一下 Regex。下面是一个示例,它将用单个“替换智能引用文本”。

string tempTxt = "I am going to “test” this.  “Hope” it works";
string formattedText = Regex.Replace(tempTxt, "s/“|”|“|”/", @"""");

You may want to give Regex a shot. Here's an example that will replace smart-quoted text with the single ".

string tempTxt = "I am going to “test” this.  “Hope” it works";
string formattedText = Regex.Replace(tempTxt, "s/“|”|“|”/", @"""");
时光病人 2024-08-10 02:02:49

我正在使用 ReqPro40.dll 来读取数据。数据以文本形式存储。希望我在下面的复制/粘贴时没有损失太多。据我所知,以下内容有效。但我想摆脱较长的坏字符序列。 E2809C 应该成为报价,但我无法匹配它。

string tempTxt = Req.get_Tag(ReqPro40.enumTagFormat.eTagFormat_ReqNameOrReqText);
tempTxt=tempTxt.Substring(1, tempTxt.Length-1);

char[] charsToRemoveForXMLLegality = new char[]
{ '\x000a', '\x000b', '\x0002', '\x001e', // NL, VT, STX, RS
  '\x0034', '\x8220', '\x8221',           // ", left double, right double quote
  '\x8216', '\x8217',                     // left single quote, right single quote
  'x8211', '\x8212',                     // en-dash, em-dash
  '\x0188', '\x0177',                     // 1/4 fraction, plus/minus
  '\x8230', '\x0160'                      // ellipsis, non-breaking space
};   
string[] charsToSubstituteForXMLLegality = new string[]
        { " ", " ", "", "-",
          "\"", "\"", "\"",
          "\'", "\'",
          "-", "-",
          "1/4", "+/-",
          "...", " "
       };

for (int i = 0; i < charsToRemoveForXMLLegality.Length; i++)
{
    tempTxt = tempTxt.Replace(charsToRemoveForXMLLegality[i].ToString(), charsToSubstituteForXMLLegality[i]);
}

I'm using a ReqPro40.dll to read data. The data is stored as text. Hope I didn't lose too much on copy/paste below. The stuff below works to the best of my knowledge. But I want to get rid of longer sequences of bad characters. E2809C should become a quote, but I'm having trouble matching it.

string tempTxt = Req.get_Tag(ReqPro40.enumTagFormat.eTagFormat_ReqNameOrReqText);
tempTxt=tempTxt.Substring(1, tempTxt.Length-1);

char[] charsToRemoveForXMLLegality = new char[]
{ '\x000a', '\x000b', '\x0002', '\x001e', // NL, VT, STX, RS
  '\x0034', '\x8220', '\x8221',           // ", left double, right double quote
  '\x8216', '\x8217',                     // left single quote, right single quote
  'x8211', '\x8212',                     // en-dash, em-dash
  '\x0188', '\x0177',                     // 1/4 fraction, plus/minus
  '\x8230', '\x0160'                      // ellipsis, non-breaking space
};   
string[] charsToSubstituteForXMLLegality = new string[]
        { " ", " ", "", "-",
          "\"", "\"", "\"",
          "\'", "\'",
          "-", "-",
          "1/4", "+/-",
          "...", "&nbsp;"
       };

for (int i = 0; i < charsToRemoveForXMLLegality.Length; i++)
{
    tempTxt = tempTxt.Replace(charsToRemoveForXMLLegality[i].ToString(), charsToSubstituteForXMLLegality[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文