C# 删除字段的最后某字符 String Remove() 和 Replace()
来源:互联网 发布:在线app制作软件 编辑:程序博客网 时间:2024/06/10 16:12
StringBuilder strSql = new StringBuilder("delete from tableName where propertyName IN (1,2,3,4,) ");
删除4后面的逗号
strSql.Remove(strSql.ToString().LastIndexOf(','), 1);
一下内容为转载
当对字符串进行操作时,我们经常要删除或者是替换一部分子字符串。 Remove() 和 Replace() 两个函数在这种情况就派上用场了。
Remove() – 删除一部分子字符串
我们都知道 substring 可以返回字符串的一部分。 当我们想保留字符串中的一部分substring丢弃其它部分时,就可以使用substring,当我们想删除字符串的一部分并保留其它部分时,就使用Remove。
Remove 有两种形式:
- Remove(int startIndex)
- 删除此字符串中从指定位置到最后位置的所有字符。
- Remove(int startIndex, int length)
- 从此实例中的指定位置开始删除指定数目的字符。
Remove 会进行区域的检查,
对于第一种形式 当
1. startIndex 小于零 或
2. startIndex 指定的位置不在此字符串内;
对于第二种形式当
1. startIndex 或 count 小于零 或
2. startIndex 加 count 之和指定一个此实例外的位置。
会抛出异常。
在Remove() 和 substring()两者中,选择哪一个应该是很容易的,看下面的实例:
string test = "Now is the time for all good men to come to the aid of their country.";
// 取头 10 个字符
var sliceUsingSubstring = test.Substring(0, 10);
// 删除第10个字符后的所有字符
var sliceUsingRemove = test.Remove(10);
string test = "Now is the time for all good men to come to the aid of their country.";
// 获取从 length - 10 到最后的部分.
var sliceUsingSubstring = test.Substring(test.Length - 10);
// 删除从头到 length - 10, 其余留下
var sliceUsingRemove = test.Remove(0, test.Length - 10);
在这种情况下,substring 看上去更加的简洁了。
现在看来,它们的确是八斤八两,但是.net 在这一类的情况下提供了两种方式,可以完全凭你的喜好来使用。
但是当我们要保留或者删除字符串中间一段时,就可以看出它们的不同了:
// 取从第 10 个开始长度为 10 的子串
var sliceUsingSubstring = test.Substring(10, 10);
// 删除从第20个字符之后的部分, 再删除头 10个
var sliceUsingRemove = test.Remove(20).Remove(0, 10);
在这种情况下,很明显substring简便易读,而且只需要一次操作。
但是如果我们想要删除中间的一段字符串:
// 取从0开始,长度为10的子串, 再取从第20 开始到末尾的部分
var sliceUsingSubstring = test.Substring(0, 10) + test.Substring(20);
// 从第10开始删除 10 个字符
var sliceUsingRemove = test.Remove(10, 10);
当使用于这种情况时, remove 明显更简单,更易读。
Replace() – 替换 char 或 String
将此实例中的指定 Unicode 字符或 String 的所有匹配项替换为其他指定的 Unicode 字符或 String。
两种重载的形式
- Replace(char oldChar, char newChar)
将此实例中的指定 Unicode 字符的所有匹配项替换为其他指定的 Unicode 字符。
Replace(string oldValue, string newValue)
将此实例中的指定 String 的所有匹配项替换为其他指定的 String。
string test = "Now is the time for all good men to come to the aid of their country.";
var politicallyCorrect = test.Replace("men", "people");
var spacesToPipes = test.Replace(' ', '|');
var withoutThe = test.Replace("the ", string.Empty);
下一个例子是当你有一块数据包含 “<BR/>” HTML 的代码时,想将它换成 Environment.NewLine:
string test = "Some data & markup was loaded from a data source.<BR/> Oh look, we started a new line!";
var cleansedData = test.Replace("<BR/>", Environment.NewLine);
var moreCleansedData = test.Replace("&", "&")
.Replace(" ", " ")
.Replace("<BR/>", Environment.NewLine);
同时我对stringbuilder进行了相同的实验,发现在这种情况下stringbuilder 的运行速度非常的缓慢:
var morePerformantCleansedData = new StringBuilder(test)
.Replace("&", "&")
.Replace(" ", " ")
.Replace("<BR/>", Environment.NewLine)
.ToString();
- C# 删除字段的最后某字符 String Remove() 和 Replace()
- String Remove() 和 Replace()
- replace替换字符,remove删除字符
- 编写函数strdel,参量是字符串string, 和字符character。删除string中第一次出现的character,最后返回string。
- mysql删除某字段的最后一位
- C++删除string最后一个字符的几种方法
- C#删除字符串最后一个字符的几种方法
- C#删除字符串最后一个字符的几种方法
- C#删除字符串最后一个字符的几种方法
- sqoop导入时删除string类型字段的特殊字符
- 编写函数strndel,参量是一个字符串string和两个整数start、length。在string中删除start开始的length个字符,最后返回string。
- C# Remove 两种删除字符方法
- Python String 的replace()与List的remove()
- 基于string下标和replace函数替换string中的字符
- String.Replace 和 String.ReplaceAll 的区别
- 去掉String字符串的最后一个字符
- string 的一些用法——插入(insert)、删除(erase)、替换(replace)和查找(find)
- JQuery如何获得所有选中的checkbox与c#去掉string最后一个字符
- Android 上传文件,头像。流形式
- java必会的jar包
- 一个简单的卷积神经网络
- 第二十二篇 jQuery 学习4 内容和属性
- Android开发中isTaskRoot方法的使用情景
- C# 删除字段的最后某字符 String Remove() 和 Replace()
- Error:Execution failed for task ':app:processDevDebugManifest'. > Manifest merger failed with multip
- 兄弟连LinuxStudyNote(1)-Linux系统(CentOS6)安装(初级入门版)
- ThunderBird 突然收不到邮件
- AndroidStudio中出现Some file crunching failed, see logs for details的解决办法
- python2.7运行出现的Unicode equal comparison failed to convert both arguments to Unicode
- Python入门系列8:dict( 字典)
- jquery实现导航栏随鼠标点击跟随滑动动画
- sqluldr2的使用方法