将字符串中多处花括号内的ASCII数字转换为它们对应的字符。

huangapple go评论68阅读模式
英文:

Convert ASCII numbers between curly brackets in multiple places in a string to their char equivalent

问题

I can help you with the translation. Here's the translated text:

在数据库表中,我有一些存储字符串的列,其中某些非字母数字字符被存储为花括号之间的ASCII整数。

示例:C:\Folder#3\File_1_@.TXT 在数据库中存储为 C:\Folder{35}3\File{95}1{95}{64}.TXT

我想编写一个函数,可以将字符串作为参数传递,然后检查它是否包含字符 # _ ? % @

如果包含这些字符,我希望将字符串处理并将这些ASCII转换回它们的字符等效形式。

简而言之,如果我将"C:\Folder{35}3\File{95}1{95}{64}.TXT"作为输入字符串传递,我希望输出为"C:\Folder#3\File_1_@.TXT"。

我一直在尝试使用System.Text.RegularExpressions来解决这个问题,但没有取得进展。

感谢任何帮助。

英文:

I have columns in a database table storing strings where SOME non-alphanumeric characters are stored as ASCII integers between curly braces.

Example: C:\Folder#3\File_1_@.TXT is stored in the DB as C:\Folder{35}3\File{95}1{95}{64}.TXT

I want to write a function where I can pass a string as a parameter and then check if it contains characters # _ ? % @

If it does, I want to treat the string and convert these ASCII to back to their char equivalents.

In brief, if I pass "C:\Folder{35}3\File{95}1{95}{64}.TXT" as the input string, I want "C:\Folder#3\File_1_@.TXT" as the output.

I've been trying to use System.Text.RegularExpressions to solve this but not getting anywhere.

Any help is appreciated.

答案1

得分: 2

You just use the lambda expression in Regex.Replace:

var input = @"C:\Folder{35}3\File{95}1{95}{64}.TXT";
var output = Regex.Replace(input, @"\{(\d+)\}", m => ((char)int.Parse(m.Groups[1].Value)).ToString());

result for output:

C:\Folder#3\File_1_@.TXT

the function ((char)int.Parse(m.Groups[1].Value)).ToString()) gets the character from the ASCII value (string here and not int).

英文:

you just use the lambda expression in Regex.Replace:

        var input = @"C:\Folder{35}3\File{95}1{95}{64}.TXT";
        var output = Regex.Replace(input, @"\{(\d+)\}", m => ((char)int.Parse(m.Groups[1].Value)).ToString());

result for output:

C:\Folder#3\File_1_@.TXT

the function ((char)int.Parse(m.Groups[1].Value)).ToString()) get the character from the ascii value (string here and not int)

答案2

得分: 1

我希望这对你有所帮助。

string address = "C:\\Folder{35}3\\File{95}1{95}{64}.TXT";

while (address.Contains("{")) {
	Int32 start = address.IndexOf("{");
	Int32 end = address.IndexOf("}");

	String subString = address.Substring(start, end - start + 1);

	Char @char = (Char)Int32.Parse((address.Substring(start + 1, end - start - 1)));

	address = address.Remove(start, end - start + 1);
	address = address.Insert(start, @char.ToString());

	//or
	//address = address.Replace(subString, @char.ToString());
}

Console.WriteLine(address);
英文:

I hope this helps.

String address = "C:\\Folder{35}3\\File{95}1{95}{64}.TXT";

while (address.Contains("{")) {
	Int32 start = address.IndexOf("{");
	Int32 end = address.IndexOf("}");

	String subString = address.Substring(start, end - start + 1);

	Char @char =  (Char)Int32.Parse((address.Substring(start + 1, end - start - 1)));

	address = address.Remove(start, end - start + 1);
	address = address.Insert(start, @char.ToString());

	//or
	//address = address.Replace(subString, @char.ToString());
}

Console.WriteLine(address);

答案3

得分: 1

I think you were in a good way to solve it.
我认为你在解决这个问题的途中是正确的。

I can suggest you to use:
我建议你使用:

public static string Replace(string input, string pattern, System.Text.RegularExpressions.MatchEvaluator evaluator, System.Text.RegularExpressions.RegexOptions options);

where your pattern could be a regex: {.}|{..}|{...} which match all characters (3 characters maximum) inside your braces
其中你的模式可以是一个正则表达式:{.}|{..}|{...},它匹配花括号内的所有字符(最多3个字符)。

And your match evaluator could be just an ASCII to char converter.
而且你的匹配评估器可以只是一个ASCII转字符的转换器。

See the Microsoft documentation below:
请参阅下面的Microsoft文档:

system-text-regularexpressions-regex-replace

英文:

I think you were in a good way to solve it.
I can suggest you to use :


public static string Replace (string input, string pattern, System.Text.RegularExpressions.MatchEvaluator evaluator, System.Text.RegularExpressions.RegexOptions options);

where your pattern could be a regex : {.}|{..}|{...} which match all characters (3 characters maximum) inside your braces
And your match evaluator could be just an ascii to char converter.

See the microsoft documentation below:

system-text-regularexpressions-regex-replace

huangapple
  • 本文由 发表于 2023年5月7日 16:48:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定