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?

Code Stranger
  • 103
  • 1
  • 10
Maciek
  • 181
  • 3
  • 12
  • 1
    Surely if you are using a `string`, and you use the already provided method `RemoveAt`, what is the issue? – Andrew Truckle May 25 '21 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 – canton7 May 25 '21 at 12:46
  • `string` is immutable so you have to assign the result back like this `something = somthing.Remove(2);` – juharr May 25 '21 at 12:49
  • 2
    @AndrewTruckle That there is no `String.RemoveAt()` – derpirscher May 25 '21 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? – Maciek May 25 '21 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. – Broots Waymb May 25 '21 at 12:58
  • @Maciek No, you don't have to download an additional library for that. Just look at the provided answers to this question. They will show some built-in possiblities how to solve your problem. – derpirscher May 25 '21 at 12:58
  • @derpirscher I gotcha. I guess he did not make that clear. His wording implied such a function. Sorry for the confusion. – Andrew Truckle May 25 '21 at 13:16
  • There are many questions this could have been closed as a duplicate of: [Fastest way to remove first char in a String](https://stackoverflow.com/q/3222125/215552); [How to remove first 10 characters from a string?](https://stackoverflow.com/q/7186648/215552) (since it would just be changing the number from 10 to 1), [C# substrings: Removing first three characters](https://stackoverflow.com/q/21909132/215552), etc. – Heretic Monkey May 25 '21 at 13:28
  • not quite true, i also ask about removing random char, but never the less i found my answer here – Maciek May 25 '21 at 17:55

4 Answers4

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
Jamiec
  • 133,658
  • 13
  • 134
  • 193
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"

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
2

you can use Remove methode like this:

something = something.Remove(0, 1);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
Alireza Ahmadi
  • 8,579
  • 5
  • 15
  • 42
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
    
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
derpirscher
  • 14,418
  • 3
  • 18
  • 35