Reputation: 939
I'm having a string "Kishore kumar karaoke tracks", from which I want to remove the substring 'karaoke tracks'. I want the output as 'Kishore kumar'.
For this purpose, I'm using the following line of code:
artist = artist.TrimEnd(new char[] {'k','a','r','a','o','k','e', ' ', 't','r','a','c','k','s'});
But, I'm getting the output as 'Kishore kum' i.e. i'm not getting the last 2 characters 'ar'.
Where I'm going wrong?
Upvotes: 0
Views: 98
Reputation: 3600
in Trim, you defined which chars you want to delete from artist
. But it has r
and a
chars too.
So when karaoke tracks
ends, next is kumar's
ar
, if you understood what I mean.
You can use replace function as others explained.
Upvotes: 1
Reputation: 1832
artist = artist.Replace("karaoke tracks", string.Empty).Trim();
You could also do this if you wanted to ignore case:
artist = Regex.Replace(artist, "karaoke tracks", string.Empty, RegexOptions.IgnoreCase).Trim();
Upvotes: 2
Reputation: 3681
You can also do like this
artist.Replace(" karaoke tracks",string.Empty);
Upvotes: 2
Reputation: 3170
Using a simple String.Replace
works too:
artist = artist.Replace(" karaoke tracks", "");
Note I added a space in the front, so the result is "Kishore kumar"
, rather than "Kishore kumar "
.
Upvotes: 2
Reputation: 13399
You can just do artist = artist.Replace("karaoke tracks", "").Trim()
Upvotes: 2