1

I have string like this:

string something = "12+3-1";
something.hereIwantToRemoveFirstElement();
Console.WriteLine(something); // i want to get "2+3-1"

Is it possible? Or maybe I should do like that to remove char from string, like it is in List?

something.RemoveAt(2);
Console.WriteLine(something);// i want to get "123-1"

Or maybe there is easy way to convert entire string to list, and then remove specific element?

something.ToList().
something.RemoveAt(0);// i want to get "2+3-1"

Can you please tell me how to do it efficiently?

10
  • 1
    Surely if you are using a string, and you use the already provided method RemoveAt, what is the issue? Commented May 25, 2021 at 12:45
  • something.Substring(1) will remove the first element. If you want to convert it to a list, something.ToList() will do it, then string.Concat(something) to turn it back into a string Commented May 25, 2021 at 12:46
  • 2
    @AndrewTruckle That there is no String.RemoveAt() Commented May 25, 2021 at 12:54
  • 1
    sadly when i click a '.' next to my string i only get Remove, not RemoveAt, and also i do not get ToList(), im working on visual studio, i shuld download some library for this? Commented May 25, 2021 at 12:55
  • 1
    @Maciek - ToList is a part of the System.Linq namespace, so just add a using declaration for that. Although I will say that would be an odd approach for such a simple string operation. Just use one of the built-in methods. Keep in mind that it's always a good idea to look through the docs to get an understanding of what these methods do and examples of proper usage. Commented May 25, 2021 at 12:58

4 Answers 4

2

You can use String.Remove method which returns a new string in which a specified number of characters from the current string are deleted:

var startIndex = 2;
var count = 1;
Console.WriteLine("12+3-1".Remove(startIndex, count)); prints "123-1"
Console.WriteLine("12+3-1".Remove(0, count)); prints "2+3-1"

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

Comments

2

Strings are immutable, so you can't edit them in place, but you can create a new one without the first char

string something = "12+3-1";
string result = something.Substring(1);

You could also assign the result back to the same variable of course

string something = "12+3-1";
something = something.Substring(1);

You can also use the same method for concatenating 2 substrings from the original

string result = something.Substring(0,2) + something.Substring(3); // 123-1

Comments

2

you can use Remove methode like this:

something = something.Remove(0, 1);

Comments

1

There are (at least) two possibilites.

  1. You use String.Remove

     var s = "1234";
     //removes 1 character at position 2
     s = s.Remove(2,1); 
     Console.WriteLine(s); //prints 124
    
  2. You use String.Substring

     var s = "1234";
    
     //takes a substring from index 0 and length 2 and 
     //concatenates it with a substring starting at index 3, 
     //thus effectifly removing one character at index 2
     s = s.Substring(0,2) + s.Substring(3);  
    
     Console.WriteLine(s); //prints 124
    

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.