C# 用字符串(通配符)替换 "Random" 文本
C# replacing "Random" text with string (wildcard)
我想用 "color":"0000ff" 替换这个字符串:"color":"efea27",
但是 "efea27" 可以是任何东西(它是随机的)。我想将其更改为用户输入的内容。例如 0000ff 当我不知道颜色“”之间的内容时我该怎么做?
(这是我要编辑的文件:pastebin。com/JWzJQcVm)
谢谢!
-shinevision
选项 1
根据您想要的模块化,这将很快完成工作。但是如果你的上下文比你原来的更复杂 post 看起来像:
就不是很方便了
var yourColor = "\"Color\":\"efea27\"";
var split = yourColor.Split(':');
split[1] = "YourOwnText";
yourColor = String.Join(":", split);
Console.WriteLine(yourColor); // "Color":"YourOwnText"
基本上,将原始字符串的键和值存储在一个数组中,用您的替换值,然后用“:”作为分隔符将它们连接起来。
选项 2
我从不使用 Json.Net 中的 feature,但它应该可以胜任。要使用它,您需要安装 nuget 依赖项:
Install-Package Newtonsoft.Json -Version 9.0.1
如果有时间,我会试着用它写点东西(这里有一些tips)。
正则表达式替换应该能够做你想做的事:
using System.Text.RegularExpressions;
//Matches: "color":"######" where #'s can be A-F and 0-9
Regex r = new Regex("\"color\":\"[a-fA-F0-9]{6}\"");
//Example input (will be your JSON)
string json = "some stuff \"color\":\"efea27\" more stuff";
//Color to replace original with
string newColor = "\"color\":\"FF0000\"";
//output is: some stuff "color":"FF0000" more stuff
json = r.Replace(json, newColor);
为了使其更具可重用性,您可以这样做(只需将字符串“0000FF”替换为您的输入):
string replaceFormat = "\"color\":\"{0}\"";
json = r.Replace(json, string.Format(replaceFormat, "0000FF"));
请注意,这将替换字符串中出现的所有 "color":"######"。
您可以这样使用 Json.NET:
var newColor = "hello";
var jtoken = JObject.Parse("{yourinput}");
var colorProperties = jtoken
.Descendants()
.OfType<JProperty>()
.Where(x => x.Name == "color")
.ToList();
foreach (var prop in colorProperties)
{
prop.Replace(new JProperty("color", newColor));
}
var result = jtoken.ToString(Newtonsoft.Json.Formatting.Indented);
正如其他评论所说,如果需要,您需要添加 nuget 包:Install-Package Newtonsoft.Json
并添加相关命名空间:
using Newtonsoft.Json.Linq;
using System.Linq;
此代码将更改整个输入中出现的所有 "color" 属性,无论其层次结构如何,这可能是也可能不是您想要的。
我想用 "color":"0000ff" 替换这个字符串:"color":"efea27", 但是 "efea27" 可以是任何东西(它是随机的)。我想将其更改为用户输入的内容。例如 0000ff 当我不知道颜色“”之间的内容时我该怎么做?
(这是我要编辑的文件:pastebin。com/JWzJQcVm)
谢谢! -shinevision
选项 1
根据您想要的模块化,这将很快完成工作。但是如果你的上下文比你原来的更复杂 post 看起来像:
就不是很方便了var yourColor = "\"Color\":\"efea27\"";
var split = yourColor.Split(':');
split[1] = "YourOwnText";
yourColor = String.Join(":", split);
Console.WriteLine(yourColor); // "Color":"YourOwnText"
基本上,将原始字符串的键和值存储在一个数组中,用您的替换值,然后用“:”作为分隔符将它们连接起来。
选项 2
我从不使用 Json.Net 中的 feature,但它应该可以胜任。要使用它,您需要安装 nuget 依赖项:
Install-Package Newtonsoft.Json -Version 9.0.1
如果有时间,我会试着用它写点东西(这里有一些tips)。
正则表达式替换应该能够做你想做的事:
using System.Text.RegularExpressions;
//Matches: "color":"######" where #'s can be A-F and 0-9
Regex r = new Regex("\"color\":\"[a-fA-F0-9]{6}\"");
//Example input (will be your JSON)
string json = "some stuff \"color\":\"efea27\" more stuff";
//Color to replace original with
string newColor = "\"color\":\"FF0000\"";
//output is: some stuff "color":"FF0000" more stuff
json = r.Replace(json, newColor);
为了使其更具可重用性,您可以这样做(只需将字符串“0000FF”替换为您的输入):
string replaceFormat = "\"color\":\"{0}\"";
json = r.Replace(json, string.Format(replaceFormat, "0000FF"));
请注意,这将替换字符串中出现的所有 "color":"######"。
您可以这样使用 Json.NET:
var newColor = "hello";
var jtoken = JObject.Parse("{yourinput}");
var colorProperties = jtoken
.Descendants()
.OfType<JProperty>()
.Where(x => x.Name == "color")
.ToList();
foreach (var prop in colorProperties)
{
prop.Replace(new JProperty("color", newColor));
}
var result = jtoken.ToString(Newtonsoft.Json.Formatting.Indented);
正如其他评论所说,如果需要,您需要添加 nuget 包:Install-Package Newtonsoft.Json
并添加相关命名空间:
using Newtonsoft.Json.Linq;
using System.Linq;
此代码将更改整个输入中出现的所有 "color" 属性,无论其层次结构如何,这可能是也可能不是您想要的。