一个用c#处理字符串的问题,貌似很简单但是我想了半天还是没想出来比较好的办法。。。

owenxin26 2010-09-06 08:57:18
假如说现在有一个字符串:
{someone}'s wallet was stolen by {someone else} but the police who called {anyone} caught him.

然后还有一个字符集合:List<string> people = new List<string> { "Tom", "Jack", "Charlie"};


要做的就是创建一个函数,把字符串中用{xxx}包围的地方替换成List里面的字符。
public string ReplaceItems(string str, List<string> items)
{
//....
}

输出结果为:
Tom's wallet was stolen by Jack but the police who called Charlie caught him.



字符串中{}数量不一定是3个,但{}的数量和对应的List集合size是相同的,{xxx}中的字符也不固定。
...全文
145 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
owenxin26 2010-09-06
  • 打赏
  • 举报
回复
谢谢各位的回答,我最后用的方法:



public string ReplaceItems(string str, List<string> items)
{
string textToReplace = str;
Regex reg = new Regex(@"\{([^{}]+)\}");

foeach(string item in items)
{
textToReplace = reg.Replace(textToReplace, item, 1);
}

return textToReplace;
}

yyd021 2010-09-06
  • 打赏
  • 举报
回复


List<string> people = new List<string> { "Tom", "Jack", "Charlie"};
int i= 0;
while (a.Contains("{")) //循环替换所有的“{”及内部内容。
{

string oldStr = strTemp.Substring(a.IndexOf("{"), a.IndexOf("}") - a.IndexOf("{") + 1); //获取第一个 {xxx}

a= a.Replace(oldStr, people[i]);
i++;
}
wuyi8808 2010-09-06
  • 打赏
  • 举报
回复
  public string ReplaceItems(string str, List<string> items)
{
int k = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '{')
{
while (i < str.Length && str[i] != '}') i++;
sb.Append(items[k++]);
}
else if (str[i] != '}') sb.Append(str[i]);
}
return sb.ToString();
}
兔子-顾问 2010-09-06
  • 打赏
  • 举报
回复
你只需要自己配置rule这个字典就可以了。
兔子-顾问 2010-09-06
  • 打赏
  • 举报
回复
给你写了个例子。

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Program
{
static void Main(string[] args)
{
string s = "{someone}'s wallet was stolen by {someone else} but the police who called {anyone} caught him.";
Dictionary<string, string> rule = new Dictionary<string, string>();
rule.Add("someone", "Tom");
rule.Add("someone else", "Jack");
rule.Add("anyone", "Charlie");
string result = RuleReplace(s, rule);
Console.WriteLine(result);
Console.ReadKey();
}

/// <summary>
/// 特定的字符串替换方法
/// </summary>
/// <param name="src">原字符串</param>
/// <param name="rule">替换规则</param>
/// <returns>替换后的结果字符串</returns>
public static string RuleReplace(string src, Dictionary<string, string> rule)
{
return Regex.Replace(src, @"\{([^{}]+)\}", delegate(Match m)
{
if (rule.ContainsKey(m.Groups[1].Value))
{
return rule[m.Groups[1].Value];//返回替换掉的字符串
}
return m.Value;//原样返回
});
}
}
jointan 2010-09-06
  • 打赏
  • 举报
回复
        public string ReplaceItems(string str, List<string> items)
{
int l = 0, r = 0;
int n = items.Count;
string temp = "";
for (int i = 0; i < n; i++)
{
l = str.IndexOf("{", r);
temp += str.Substring(r, l - r);
temp += items[i];
r = str.IndexOf("}", l) + 1;
}
temp += str.Substring(r, str.Length - r);
return temp;
}
liysky 2010-09-06
  • 打赏
  • 举报
回复
当然发现{前的字符也需要往stringbuilder里加
liysky 2010-09-06
  • 打赏
  • 举报
回复
将字符串转换成数组,然后遍历一次,从发现{开始将要替换的对象往stringbuilder里装,直到发现第一个}。需要设置一个flag来检查{ }是否成对出现。如果不成对出现或先遇到}都要返回提示信息。即可实现
owenxin26 2010-09-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 net_lover 的回复:]

string a = "{someone}'s wallet was stolen by {someone else} but the police who called {anyone} caught him.";
List<string> people = new List<string> { "Tom", "Jack", "Charlie"};
a =……
[/Quote]


谢谢你的回答,这个方法对我举的例子是可行的,但问题是{xxx}中的字符并不一定就是{someone},{anyone},也可能是{abc}, {def}等其它的,所以并不适用于所有情况。
孟子E章 2010-09-06
  • 打赏
  • 举报
回复
string a = "{someone}'s wallet was stolen by {someone else} but the police who called {anyone} caught him.";
List<string> people = new List<string> { "Tom", "Jack", "Charlie"};
a = a.Replace("{someone}",people[0]).Replace("{someone else}",people[1]).Replace("{anyone}",people[2]);
Response.Write(a);
wuyq11 2010-09-06
  • 打赏
  • 举报
回复
Regex reg = new Regex(@"(?<=\{)[^{}]+(?=\})");
reg.Replace(input, "");
Tim_Wu6 2010-09-06
  • 打赏
  • 举报
回复
Replace 函数

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧