Reputation: 41128
Is there an easy way to remove the first 2 and last 2 chars in a string?
I have this string:
\nTESTSTRING\n
How could I easily delete them?
Upvotes: 9
Views: 64981
Reputation: 2994
The accepted answer can be generalized as
static string TrimMargins(string text, int margin)
{
if (text == null || text.Length < 2 * margin)
{
return string.Empty;
}
return text.Substring(margin, text.Length - 2 * margin);
}
Usage:
string result = TrimMargins("123456", 2);
// result is "34"
Upvotes: 0
Reputation: 2994
Here is a ready to use method. It takes the char count to trim from left and right, uses C# 8.0 Range operator and checks for invalid input:
static string TrimMargins(string text, int margin)
{
if (text == null || text.Length < 2 * margin)
{
return string.Empty;
}
return text[margin..^margin];
}
Usage:
string result = TrimMargins("123456", 2);
// result is "34"
Upvotes: 0
Reputation: 29143
str = str.Substring(2,str.Length-4)
Of course you must test that the string contains more than 4 chars before doing this. Also in your case it seems that \n is a single newline character. If all you want to do is remove leading and trailing whitespaces, you should use
str.Trim()
as suggested by Charles
Upvotes: 48
Reputation: 29
Its Simple with Substring
and Remove
methods, as detailed in this link:
string mystring = "122014";
mystring = mystring.Substring(mystring.Length - 4);
Response.Write(mystring.ToString());
//output:2014
mystring = "122014";
string sub = mystring.Remove(mystring.Length - 4);
Response.Write("<br>");
Response.Write(sub.ToString());
//output: 12
Upvotes: 0
Reputation: 1
public string RemoveFirstCharFromString(string Text)
{
string[] arr1 = new string[] { "The ", "A " };
string Original = Text.ToLower();
if (Text.Length > 4)
{
foreach (string match in arr1)
{
if (Original.StartsWith(match.ToLower()))
{
//Original = Original.Replace(match.ToLower(), "").TrimStart();
Original = Original.Replace(Original.Substring(0, match.Length), "").TrimStart();
return Original;
}
}
}
return Original;
}
Upvotes: 0
Reputation: 1
string Origional = TextBox1.Text.Replace(TextBox1.Text.Substring(0, 2), "");
Origional += Origional.Replace(Origional.Substring((Origional.Length - 2), 2), "");
Upvotes: 0
Reputation: 686
Papuccino1,
If you create an extension method like this:
public static class StringEnumerator {
public static IEnumerable<String> GetLines(this String source) {
String line = String.Empty;
StringReader stringReader = new StringReader(source);
while ((line = stringReader.ReadLine()) != null) {
if (!String.IsNullOrEmpty(line)) {
yield return line;
}
}
}
}
your code will be simplified and will be safer (not depending on dangerous index):
class Program {
static void Main(string[] args) {
String someText = "\nTESTSTRING\n";
String firstLine = someText.GetLines().First();
}
}
I hope this helps,
Ricardo Lacerda Castelo Branco
Upvotes: 0
Reputation: 32240
// Test string
var str = "\nTESTSTRING\n";
// Number of characters to remove on each end
var n = 2;
// Slimmed string
string slimmed;
if (str.Length > n * 2)
slimmed = str.Substring(n, str.Length - (n * 2));
else
slimmed = string.Empty;
// slimmed = "ESTSTRIN"
Upvotes: 6