-4

I've this kind of string.

FullName:ae876ggfg777878848adgf877

And I want to remove "FullName:", so the output as follows:

ae876ggfg777878848adgf877

How can I do that?

I tried this:

var index = myText.IndexOf(":");
var result = myText.Remove(index);

But the output is like this:

FullName

Which I do not expect.

1
  • 2
    Did you read the documentation on IndexOf and Remove? Commented Jul 23, 2014 at 16:44

2 Answers 2

2

IndexOf returns the index of whatever string/character you give it, so in your case, the index of :.

Remove, according to the documentation:

Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.

So what's happening here is you're removing everything after and including the :

You should be using String.Replace:

string removed = myText.Replace("FullName:", "");
Sign up to request clarification or add additional context in comments.

1 Comment

Upvoted as you beat me to the punch by a couple minutes. Also scrapped my answer as it was duplicate.
1

Use String.Substring() and get the start index by using String.IndexOf('character') + 1.

  

    string s = "FullName:ae876ggfg777878848adgf877";

    Console.WriteLine(s.Substring(s.IndexOf(':')+1));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.