如何替换c#字符串中的特定字符?
如果我有一个大致如下的字符串:“user:jim;id:23;group:49st;” 如何用其他内容替换组码(49st),使其显示:“user:jim;id=23;group:76pm;” 对不起,如果这个问题很简单,但我还没有找到一个具体的答案,只是案例不同于我的 您可以像这样使用“组”索引如何替换c#字符串中的特定字符?,c#,string,replace,C#,String,Replace,如果我有一个大致如下的字符串:“user:jim;id:23;group:49st;” 如何用其他内容替换组码(49st),使其显示:“user:jim;id=23;group:76pm;” 对不起,如果这个问题很简单,但我还没有找到一个具体的答案,只是案例不同于我的 您可以像这样使用“组”索引 string a = "user:jim;id:23;group:49st"; string b = a.Replace("49st", "76pm"); Console.Write(b); st
string a = "user:jim;id:23;group:49st";
string b = a.Replace("49st", "76pm");
Console.Write(b);
string s = "user:jim;id:23;group:49st;";
string newS = s.Substring(0,s.IndexOf("group:") + 6);
string restOfS = s.IndexOf(";",s.IndexOf("group:") + 6) + 1 == s.Length
? ""
: s.Substring(s.IndexOf(";",s.IndexOf("group:") + 6) + 1);
newS += "76pm;";
s = newS + restOfS;
带有s=标准的行?true:false本质上是一个if,但它使用三元运算符放在一行上
或者,如果您知道已经存在哪些文本以及应该替换哪些文本,您可以使用Replace
s = s.Replace("49st","76pm");
作为额外的预防措施,如果您不总是在字符串中包含此“group:”部分,为了避免错误,请将此部分放在首先检查的if
中
if(s.Contains("group:"))
{
//Code
}
您可以像这样使用“group”索引
string s = "user:jim;id:23;group:49st;";
string newS = s.Substring(0,s.IndexOf("group:") + 6);
string restOfS = s.IndexOf(";",s.IndexOf("group:") + 6) + 1 == s.Length
? ""
: s.Substring(s.IndexOf(";",s.IndexOf("group:") + 6) + 1);
newS += "76pm;";
s = newS + restOfS;
带有s=标准的行?true:false本质上是一个if,但它使用三元运算符放在一行上
或者,如果您知道已经存在哪些文本以及应该替换哪些文本,您可以使用Replace
s = s.Replace("49st","76pm");
作为额外的预防措施,如果您不总是在字符串中包含此“group:”部分,为了避免错误,请将此部分放在首先检查的if
中
if(s.Contains("group:"))
{
//Code
}
使用正则表达式查找匹配项,并将其替换为原始字符串中的新值,如下所述:
string str = "user:jim;id=23;group:49st;";
var match = Regex.Match(str, "group:.*;").ToString();
var newGroup = "group:76pm;";
str = str.Replace(match, newGroup);
使用正则表达式查找匹配项,并将其替换为原始字符串中的新值,如下所述:
string str = "user:jim;id=23;group:49st;";
var match = Regex.Match(str, "group:.*;").ToString();
var newGroup = "group:76pm;";
str = str.Replace(match, newGroup);
无论组在字符串中出现在何处,此解决方案都应有效:
string input = "user:jim;id:23;group:49st;";
string newGroup = "76pm";
string output = Regex.Replace(input, "(group:)([^;]*)", "${1}"+newGroup);
无论组在字符串中出现在何处,此解决方案都应有效:
string input = "user:jim;id:23;group:49st;";
string newGroup = "76pm";
string output = Regex.Replace(input, "(group:)([^;]*)", "${1}"+newGroup);
下面是一个非常通用的方法,用于拆分输入、更改项,然后将项重新连接到字符串。在您的示例中,它不是用于单个替换,而是用于显示如何拆分和连接字符串中的项
我使用正则表达式拆分项目,然后将结果放入字典
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string pattern = "(?'name'[^:]):(?'value'.*)";
string input = "user:jim;id:23;group:49st";
Dictionary<string,string> dict = input.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => new
{
name = Regex.Match(x, pattern).Groups["name"].Value,
value = Regex.Match(x, pattern).Groups["value"].Value
}).GroupBy(x => x.name, y => y.value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
dict["group"] = "76pm";
string output = string.Join(";",dict.AsEnumerable().Select(x => string.Join(":", new string[] {x.Key, x.Value})).ToArray());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串模式=(?'name'[^:]):(?'value'.*);
string input=“用户:jim;id:23;组:49st”;
Dictionary dict=input.Split(新字符[]{';'},StringSplitOptions.RemoveEmptyEntries)。选择(x=>new
{
name=Regex.Match(x,pattern).Groups[“name”].Value,
value=Regex.Match(x,pattern).Groups[“value”].value
}).GroupBy(x=>x.name,y=>y.value)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
dict[“group”]=“76pm”;
字符串输出=string.Join(;),dict.AsEnumerable().Select(x=>string.Join(:),newstring[]{x.Key,x.Value})).ToArray();
}
}
}
这是一种非常通用的方法,用于拆分输入、更改项目,然后将项目重新连接到字符串。在您的示例中,它不是用于单个替换,而是用于显示如何拆分和连接字符串中的项
我使用正则表达式拆分项目,然后将结果放入字典
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string pattern = "(?'name'[^:]):(?'value'.*)";
string input = "user:jim;id:23;group:49st";
Dictionary<string,string> dict = input.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => new
{
name = Regex.Match(x, pattern).Groups["name"].Value,
value = Regex.Match(x, pattern).Groups["value"].Value
}).GroupBy(x => x.name, y => y.value)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
dict["group"] = "76pm";
string output = string.Join(";",dict.AsEnumerable().Select(x => string.Join(":", new string[] {x.Key, x.Value})).ToArray());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串模式=(?'name'[^:]):(?'value'.*);
string input=“用户:jim;id:23;组:49st”;
Dictionary dict=input.Split(新字符[]{';'},StringSplitOptions.RemoveEmptyEntries)。选择(x=>new
{
name=Regex.Match(x,pattern).Groups[“name”].Value,
value=Regex.Match(x,pattern).Groups[“value”].value
}).GroupBy(x=>x.name,y=>y.value)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
dict[“group”]=“76pm”;
字符串输出=string.Join(;),dict.AsEnumerable().Select(x=>string.Join(:),newstring[]{x.Key,x.Value})).ToArray();
}
}
}
这只是一种方法。我希望它能帮助你
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace stringi
{
class Program
{
static void Main(string[] args)
{
//this is your original string
string s = "user:jim;id:23;group:49st";
//string with replace characters
string s2 = "76pm";
//convert string to char array so you can rewrite character
char[] c = s.ToCharArray(0, s.Length);
//asign characters to right place
c[21] = s2[0];
c[22] = s2[1];
c[23] = s2[2];
c[24] = s2[3];
//this is your new string
string new_s = new string(c);
//output your new string
Console.WriteLine(new_s);
Console.ReadLine();
}
}
}
这只是一种方法。我希望它能帮助你
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace stringi
{
class Program
{
static void Main(string[] args)
{
//this is your original string
string s = "user:jim;id:23;group:49st";
//string with replace characters
string s2 = "76pm";
//convert string to char array so you can rewrite character
char[] c = s.ToCharArray(0, s.Length);
//asign characters to right place
c[21] = s2[0];
c[22] = s2[1];
c[23] = s2[2];
c[24] = s2[3];
//this is your new string
string new_s = new string(c);
//output your new string
Console.WriteLine(new_s);
Console.ReadLine();
}
}
}
拆分“;”上的字符串。然后在“:”上拆分每个字符串。替换第一个字符串部分为“组”的位置。最后重新生成字符串。是否要将49st
更改为76pm
或group:x
更改为group:76pm
?只需替换这两个。在这些地方的变量,是的,组:x到组:y@stuartd如果它是一个动态构建的字符串,那么这就行不通了。卡拉已经给出了适当的解决方案。由于小组发生变化,我无法手动执行“49街”、“76街”。不,那23只不过是个误会,把绳子在“;”上分开。然后在“:”上拆分每个字符串。替换第一个字符串部分为“组”的位置。最后重新生成字符串。是否要将49st
更改为76pm
或group:x
更改为group:76pm
?只需替换这两个。在这些地方的变量,是的,组:x到组:y@stuartd如果它是一个动态构建的字符串,那么这就行不通了。卡拉已经给出了适当的解决方案。由于小组发生变化,我无法手动执行“49街”、“76街”。不,23只是一个错误OP澄清说他们想用一个新值替换“group”后面的任何值,而不仅仅是示例中所示的具体情况。然后应该很容易理解:string b=a.replace(“group:49st”,“group:76pm”)代码>@Gustav不,因为你不知道“组”后面是什么。@Gustav动态字符串?OP可能不知道接下来会发生什么。OP明确表示,他们希望用新值替换“group”后面的任何值,而不仅仅是示例中所示的特定情况。然后应该很容易理解:string b=a。