C# 如何删除字符串中的前两个和最后两个字符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1597583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:11:50  来源:igfitidea点击:

How to remove first two and last two chars in a string?

c#string

提问by Sergio Tapia

Is there an easy way to remove the first 2 and last 2 chars in a string?

有没有一种简单的方法可以删除字符串中的前 2 个和最后 2 个字符?

I have this string:

我有这个字符串:

\nTESTSTRING\n

How could I easily delete them?

我怎样才能轻松删除它们?

采纳答案by Manu

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

当然,在执行此操作之前,您必须测试字符串是否包含 4 个以上的字符。同样在您的情况下, \n 似乎是一个换行符。如果您只想删除前导和尾随空格,则应使用

str.Trim()

as suggested by Charles

正如查尔斯所建议的

回答by Chuck Conway

Did you try:

你试过了吗:

 myString.Trim();

回答by André Pena

myString = myString.SubString(2, myString.Length - 4);

回答by cdmckay

// 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"

回答by Ricardo Lacerda Castelo Branco

Papuccino1,

帕布奇诺1,

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

里卡多·拉塞尔达 Castelo Branco

回答by Muhammad Hussain

    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;
    }

回答by Muhammad Hussain

string Origional = TextBox1.Text.Replace(TextBox1.Text.Substring(0, 2), "");
Origional += Origional.Replace(Origional.Substring((Origional.Length - 2), 2), "");

回答by user2248133

Its Simple with Substringand Removemethods, as detailed in this link:

其简单的方法SubstringRemove方法,详见此链接

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