如何通过位置来替换字符串的一部分? [英] How to replace part of string by position?
本文介绍了如何通过位置来替换字符串的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个字符串: ABCDEFGHIJ
我需要从第4位的字符串替换到位置5 ZX
I need to replace from position 4 to position 5 with the string ZX
它看起来像这样: ABCZXFGHIJ
但不会与与string.replace(DE,ZX)使用
- 我需要与使用位置
But not to use with string.replace("DE","ZX")
- I need to use with position
我该怎么办呢?
推荐答案
要在字符串中添加和删除范围最简单的方法就是使用<$c$c>StringBuilder$c$c>.
The easiest way to add and remove ranges in a string is to use the StringBuilder
.
var theString = "ABCDEFGHIJ";
var aStringBuilder = new StringBuilder(theString);
aStringBuilder.Remove(3, 2);
aStringBuilder.Insert(3, "ZX");
theString = aStringBuilder.ToString();
另一种方法是使用 String.Substring
,但我认为的StringBuilder
code变得更具可读性。
An alternative is to use String.Substring
, but I think the StringBuilder
code gets more readable.
这篇关于如何通过位置来替换字符串的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文