How to remove the first 10 characters from a string?

How to ignore the first 10 characters of a string?

Input:

str = "hello world!"; 

Output:

 d! 
+56
string substring c #
Aug 25 2018-11-11T00:
source share
10 answers
 str = "hello world!"; str.Substring(10, str.Length-10) 

you will need to perform a length check, otherwise it will cause an error

+60
Aug 25 '11 at 7:40
source share

str = str.Remove(0,10); Deletes the first 10 characters

or

str = str.Substring(10); Creates a substring from the 11th character to the end of the string.

For your purposes, they should work the same.

+136
Aug 25 2018-11-11T00:
source share

A substring is probably what you want, as others have pointed out. But just add another option to the mix ...

 string result = string.Join(string.Empty, str.Skip(10)); 

You donโ€™t even need to check the length on this! :) If its less than 10 characters, you get an empty string.

+11
Aug 25 '11 at 7:44
source share

Substring has a startIndex parameter. Set it according to the index you want to start.

+4
Aug 25 2018-11-11T00:
source share

SubString has two overload methods:

 public string Substring(int startIndex);//The substring starts at a specified character position and continues to the end of the string. public string Substring(int startIndex, int length);//The substring starts at a specified character position and taking length no of character from the startIndex. 

So, for this scenario, you can use the first method, similar to the one below:

 var str = "hello world!"; str = str.Substring(10); 

Here's the conclusion:

 d! 

If you can apply security coding by checking its length.

+2
Sep 28 '17 at 9:22 on
source share

Use the substring method.

 string s = "hello world"; s=s.Substring(10, s.Length-10); 
+1
Aug 25 2018-11-11T00:
source share

You can use the Substring method, which takes one parameter, which is an index to start with.

In my code below, I understand that the length is less than your desired start index and when the length is zero.

 string s = "hello world!"; s = s.Substring(Math.Max(0, Math.Min(10, s.Length - 1))); 
+1
Aug 25 2018-11-11T00:
source share

For:

 var str = "hello world!"; 

To get the resulting string without the first 10 characters and an empty string if the string is less than or equal to a length of up to 10, you can use:

 var result = str.Length <= 10 ? "" : str.Substring(10); 

or

 var result = str.Length <= 10 ? "" : str.Remove(0, 10); 

The first option is preferable, since it needs only one parameter of the method.

+1
Aug 25
source share

There is no need to specify the length in the Substring method. Therefore:

 string s = hello world; string p = s.Substring(3); 

p will be:

"peace".

The only exception you need to handle is ArgumentOutOfRangeException if startIndex less than zero or greater than the length of this instance.

0
Jul 18 '17 at 10:25
source share

You can remove Char using the line below,

: - First check that String is enough Char to delete , for example

  string temp="Hello Stack overflow"; if(temp.Length>10) { string textIWant = temp.Remove(0, 10); } 
0
Jul 18 '17 at 11:06
source share



All Articles