C# 剪切具有已知开始和结束索引的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9468950/
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-09 07:32:10  来源:igfitidea点击:

Cut a string with a known start & end index

c#string

提问by Jan W

When I have a string that I want to cut into a new string from a certain Index to a certain Index, which function do I use?

当我有一个字符串想要从某个索引剪切成一个新字符串到某个索引时,我使用哪个函数?

If the string was:

如果字符串是:

ABCDEFG

ABCDEFG

This would mean retrieving BCDwhen the two indexes specified were 1and 3.

这意味着在指定的两个索引为13时检索BCD

采纳答案by Olivier Jacot-Descombes

If endIndexpoints to the last character that you want to have included in the extracted substring:

如果endIndex指向要包含在提取的子字符串中的最后一个字符:

int length = endIndex - startIndex + 1;
string piece = s.Substring(startIndex, length);


If endIndexpoints to the first character following the desired substring (i.e. to the start of the remaining text):

如果endIndex指向所需子字符串后面的第一个字符(即指向剩余文本的开头):

int length = endIndex - startIndex;
string piece = s.Substring(startIndex, length);


See String.Substring Method (Int32,?Int32)for the official description on Microsoft Docs.

有关Microsoft Docs的官方说明请参阅String.Substring Method (Int32,?Int32)

回答by Sanjay Goswami

There is two way to substring string..

有两种方法可以对字符串进行子串..

1 )

1)

public string Substring(
    int startIndex
)

Retrieves a substring from this instance. The substring starts at a specified character position.

从此实例中检索子字符串。子字符串从指定的字符位置开始。

2)

2)

public string Substring(
    int startIndex,
    int length
)

Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.

从此实例中检索子字符串。子字符串从指定的字符位置开始并具有指定的长度。

回答by Rob

This becomes possible with the new Range feature of C# 8.0.

C# 8.0的新Range 特性使这成为可能。

An extension method on stringthat uses Rangeto achieve this is:

在扩展方法string使用Range,以实现这一目标是:

public static class StringExtensions
{
    public static string SubstringByIndexes(this string value, int startIndex, int endIndex)
    {
        var r = Range.Create(startIndex, endIndex + 1);
        return value[r];
        /*
        // The content of this method can be simplified down to:

        return value[startIndex..endIndex + 1];

        // by using a 'Range Expression' instead of constructing the Range 'long hand'
        */
    }
}

Note: 1 is added to endIndexwhen constructing the Range that's used as the end of the range is exclusive, rather than inclusive.

注意:endIndex在构造 Range 时添加 1作为范围的结尾是不包含的,而不是包含的。

Which can be called like this:

可以这样调用:

var someText = "ABCDEFG";

var substring = someText.SubstringByIndexes(1, 3);

Giving a value of BCDin substring.

给人一种价值BCDsubstring

回答by Manish Kumar

Unfortunately, C# doesn't natively have what you need. C# offers Substring(int startIndex, int length) instead. To achieve Substring(int startIndex, int endIndex), you will need custom implementation. Following extension method can make reusability easier/cleaner:

不幸的是,C# 本身并没有您需要的东西。C# 提供 Substring(int startIndex, int length) 代替。要实现 Substring(int startIndex, int endIndex),您将需要自定义实现。以下扩展方法可以使可重用性更容易/更清洁:

public static class Extensions
{
    public static string Substring2(this string value, int startIndex, int endIndex)
    {
        return value.Substring(startIndex, (endIndex - startIndex + 1));
    }
}