能不能用正则表达式进行大小写替换?

Brunhild 2004-10-09 10:29:42
需要替换的项目已经找到,但不知道该如何将他们替换为大写。
...全文
592 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
saucer 2004-10-09
  • 打赏
  • 举报
回复
using System;
using System.Text.RegularExpressions;

class TestReg
{
static string MyMatchEvaluator(Match m)
{
return m.Value.ToUpper();
}

static void Main()
{
string s = "abdADB";
s = Regex.Replace(s, "需要替换的内容", new MatchEvaluator(MyMatchEvaluator));
Console.WriteLine(s);
}
}
Brunhild 2004-10-09
  • 打赏
  • 举报
回复
噢?试试先
Brunhild 2004-10-09
  • 打赏
  • 举报
回复
仅仅是STRING的REPLACE我会,但我的STRING是这样的:
"无关的内容 标记 需要替换的内容 标记 无关的内容, ..."
我需要将“需要替换的内容”全部替换为大写,其他的不变。

saucer 2004-10-09
  • 打赏
  • 举报
回复
using regular expression to convert is an overkill, String's ToUpper should be good enough, but if you insist, you could try something like

using System;
using System.Text.RegularExpressions;

class TestReg
{
static string MyMatchEvaluator(Match m)
{
char c = m.Value[0];
c = (char) (c-'a'+'A');
return c.ToString();
}

static void Main()
{
string s = "abdADB";
s = Regex.Replace(s, "[a-z]", new MatchEvaluator(MyMatchEvaluator));
Console.WriteLine(s);
}
}
CSTerry 2004-10-09
  • 打赏
  • 举报
回复
做个循环?

Yourstring.Replace("a","A");
t0h 2004-10-09
  • 打赏
  • 举报
回复
ToUpper()?? 不知道
khpcg 2004-10-09
  • 打赏
  • 举报
回复
恐怕不行吧

110,536

社区成员

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

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

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