0

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?

0

5 Answers 5

2

You can just do artist = artist.Replace("karaoke tracks", "").Trim()

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

You can also do like this

artist.Replace(" karaoke tracks",string.Empty);

Comments

2
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();

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.