替换“字符串"构建器中的特定字符 [英] Replace specific characters in String builder

查看:74
本文介绍了替换“字符串"构建器中的特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个StringBuilder SB,其中我附加了来自文本区域的unicode字. െ"或േ"或്ര"必须使用C#将其向右移一个位置.

如果我有െത在stringbuilder中的任何位置都必须替换为തെ.我们不知道െ
之后会发生什么 我该怎么办?

问候

解决方案

将其转换为字符串,然后执行以下操作:

  var 结果= Regex.Replace(" " 代理(匹配m){
    返回 m.Value.Substring( 1  1 )+ m.Value.Substring( 0  1 );
})); 


假定小写字符"c"将向右移动一个字符(除非它已经是字符串中的最后一个字符,否则将被保留).

您可能要使用Regex构造函数,以便可以传递选项,例如忽略大小写或指定如何处理换行符.正则表达式非常灵活,因此您也可以执行以下操作:

  var 结果= Regex.Replace("  (?< FIRST> c)(?< SECOND> .)"  


{SECOND}

{FIRST}");


如果要考虑要向右移动一个字符组的话,则必须对其进行修改.

为了解决您在对此答案的评论中提到的情况,您可以尝试执行以下操作:

  var  val = "  var 结果= Regex.Replace(val,"   


I have a StringBuilder SB where i append unicode words from a textarea. Where ever "&#3398;" or "&#3399;" or "&#3405;&#3376;" comes it has to be moved one position to the right using C#.

Example if i have &#3398;&#3364; any where in the stringbuilder it has to be replaced as &#3364;&#3398; we dont have an idea of what will come after &#3398;
How can I do this?

Regards

解决方案

Convert it to a string, then do something like this:

var result = Regex.Replace("abcdedcba", "c.", new MatchEvaluator(delegate(Match m) {
    return m.Value.Substring(1, 1) + m.Value.Substring(0, 1);
}));


That assumes the lowercase character "c" is what will be moved one character to the right (except in the case that it is already the last character in the string, in which case it will be left alone).

You may want to use the Regex constructor so you can pass options, such as ignoring case or specifying how to handle newline characters. Regular expression are pretty flexible, so you could also do something like this too:

var result = Regex.Replace("abcdedcba", "(?<FIRST>c)(?<SECOND>.)", "


{SECOND}


{FIRST}");


You''d have to modify it if you want to account for groups of characters you want to move to the right by one.

EDIT: To account for the scenario you mentioned in a comment to this answer, you might try something like this:

var val = "F073 F058 F06D F073 F020 F068 F053 F0A1 F06D F02D F074 F073 F0A9";
var result = Regex.Replace(val, "(?<FIRST>F073) (?<SECOND>....)", "


这篇关于替换“字符串"构建器中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆