CarbonD1225
CarbonD1225

Reputation: 869

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. 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?

Upvotes: 4

Views: 12423

Answers (3)

Gene S
Gene S

Reputation: 2773

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...

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

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

Upvotes: 0

Aeropher
Aeropher

Reputation: 117

I would use trim twice

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

Upvotes: 2

vcsjones
vcsjones

Reputation: 141638

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.

Upvotes: 13

Related Questions