简体   繁体   English

从C#中的字符串末尾删除单引号

[英]removing single quote from end of a string in C#

i am using the following code : 我使用以下代码:

  importTabs.Add(row["TABLE_NAME"].ToString().TrimEnd('$')

to remove a dollar from string stored in importTabs Array list. 从importTabs数组列表中存储的字符串中删除一美元。 how do i pass a parameter along with '$' so that it removes a single quote (') from the beginning ans well the end of the string? 我如何传递一个参数和'$',以便它从字符串的开头和结尾删除一个引号(')?

You could use another trim: 你可以使用另一个修剪:

importTabs.Add(row["TABLE_NAME"].ToString().Trim('\'').TrimEnd('$')

Or, if you don't mind removing the $ at the beginning too, you can do it all at once: 或者,如果你不介意在开头删除$ ,你可以一次完成所有操作:

importTabs.Add(row["TABLE_NAME"].ToString().Trim('\'', '$')

That saves you from creating one more string instance than you need to. 这样可以避免创建多于您需要的字符串实例。

我会使用修剪两次

importTabs.Add(row["TABLE_NAME"].ToString().Trim('\'').TrimEnd('$')

Not sure I quite understand your question. 不确定我完全理解你的问题。 Are you wanting to remove single quotes from the beginning and end and remove the $ from the end? 您想要从开头和结尾删除单引号并从结尾删除$吗? If so you can use this... 如果是这样,你可以使用这个......

importTabs.Add(row["TABLE_NAME"].ToString().TrimEnd('$').Trim('\''))

If the $ sign is before the ending tic mark then the Trims need to be reversed... 如果$符号在结束标记之前,那么Trims需要反转......

importTabs.Add(row["TABLE_NAME"].ToString()).Trim('\'').TrimEnd('$')

If you know there is not a $ sign at the beginning you can simplify it... 如果你知道开头没有$符号,你可以简化它...

importTabs.Add(row["TABLE_NAME"].ToString().Trim('$', '\''))

If you want to pass it in as a parameter the Trim takes an array of characters 如果要将其作为参数传递,Trim将获取一个字符数组

char[] charactersToRemove = new[] {'$', '\''};
importTabs.Add(row["TABLE_NAME"].ToString().Trim(charactersToRemove))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM