[英]How can I replace “XX,XXX” with “XX XXX”?
I need to replace string like "XX,XXX" with "XX XXX". 我需要将“ XX,XXX”之类的字符串替换为“ XX XXX”。 The string "XX,XXX" is in another string, eg: 字符串“ XX,XXX”在另一个字符串中,例如:
"-1299-5,"XXX,XXXX",trft,4,0,10800"
The string is fetched from a text file. 该字符串是从文本文件中提取的。 I want to split the string by ",". 我想用“,”分割字符串。 But the comma in the substring led to the wrong result. 但是子字符串中的逗号导致了错误的结果。
The X represents a char. X代表一个字符。 I think regex can help, who can give me the right regex expression. 我认为正则表达式可以提供帮助,谁可以给我正确的正则表达式表达。
This expression, 这个表达,
(.*"[^,]*),([^,]*".*)
with a replacement of $1 $2
might work. 替换为$1 $2
可能会起作用。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(.*""[^,]*),([^,]*"".*)";
string substitution = @"\1 \2";
string input = @"-1299-5,""XXX,XXXX"",trft,4,0,10800";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
Simply, use 'Replace' to replace char from your string. 只需使用“替换”来替换字符串中的char。
var test = "XXX,XXXX";
var filtered = test.Replace(',', ' ');
Console.WriteLine(filtered);
Output : 输出:
XXX XXXX
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.