如何从字符串的最后一个索引中删除','
问题描述:
如何从字符串的最后一个索引中删除'',''
How to remove '','' from last index of the string
string text='Aluminium,Alternaria Tenius,Alcohol (ethyl alcohol),2-Hydroxyethyl,Beryllium,'
谢谢
答
使用字符串运算符LastIndexOf
,SubString
和Length
来获取不带最后一个.的字符串.
Use string operatorLastIndexOf
,SubString
andLength
to get the string without the last ,.
这和myString.Trim(new char[] {'',''}
一样简单,但是您能考虑一下如何避免首先添加多余的逗号吗?
因此,请参阅 http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx [ ^ ],但认为关于它.
请记住,字符串是不可变的,因此所有类似的字符串操作都意味着通过来回复制所有数据来创建全新的字符串.这就是System.Text.StringBuilder
对于字符串操作更好的原因,但是如果您已经生成了需要修复的字符串,它对您没有太大帮助.首先尝试避免它.或者-如果性能不成问题,请忽略它并使用Trim
.—SA
This is as simple asmyString.Trim(new char[] {'',''}
, but can you rather think about how to avoid adding redundant comma in first place?
So, please see http://msdn.microsoft.com/en-us/library/d4tt83f9.aspx[^], but think about it.
Remember that strings are immutable, so all string operations like that mean creation of the brand new string with copying all the data back and forth. That''s whySystem.Text.StringBuilder
is better for string manipulations, but it won''t help you much if you already produced the string which needs some fix. Try to avoid it in first place. Or — forget it and useTrim
if performance is not an issue.—SA
对于您的问题
Hi,
For your problem
text.Trim(',');
将解决您的问题.
谢谢
-Amit.
will resolve your issue.
thanks
-Amit.