C# 如何从字符串中提取字符范围

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

How to extract range of characters from a string

c#.netstring

提问by BigBug

If I have a string such as the following:

如果我有一个如下所示的字符串:

 String myString = "SET(someRandomName, \"hi\", u)"; 

where I know that "SET("will always exists in the string, but the length of "someRandomName"is unknown, how would I go about deleting all the characters from "("to the first instance of """? So to re-iterate, I would like to delete this substring: "SET(someRandomName, \""from myString.

我知道"SET("将始终存在于字符串中,但 的长度"someRandomName"未知,我将如何删除从"("到第一个实例的所有字符"""?所以重新迭代,我想删除这个子字符串:"SET(someRandomName, \""from myString

How would I do this in C#.Net?

我将如何在 C#.Net 中做到这一点?

EDIT: I don't want to use regex for this.

编辑:我不想为此使用正则表达式。

采纳答案by Ondrej Tucny

Providing the string will alwayshave this structure, the easiest is to use String.IndexOf()to look-up the index of the first occurence of ". String.Substring()then gives you appropriate portion of the original string.

提供字符串将始终具有这种结构,最简单的方法是使用String.IndexOf()查找 的第一次出现的索引"String.Substring()然后为您提供原始字符串的适当部分。

Likewise you can use String.LastIndexOf()to find the index of the first "from the endof the string. Then you will be able to extract just the value of the second argument ("hi"in your sample).

同样,您可以使用从字符串末尾String.LastIndexOf()查找第一个的索引。然后您将能够仅提取第二个参数的值(在您的示例中)。""hi"

You will end up with something like this:

你最终会得到这样的结果:

int begin = myString.IndexOf('"');
int end = myString.LastIndexOf('"');
string secondArg = myString.Substring(begin, end - begin + 1);

This will yield "\"hi\""in secondArg.

这将"\"hi\""secondArg.

UPDATE: To remove a portion of the string, use the String.Remove()method:

更新:要删除字符串的一部分,请使用以下String.Remove()方法:

int begin = myString.IndexOf('(');
int end = myString.IndexOf('"');
string altered = myString.Remove(begin + 1, end - begin - 1);

This will yield "SET(\"hi\", u)"in altered.

这将"SET(\"hi\", u)"altered.

回答by Douglas

I assume you want to transform

我假设你想转型

SET(someRandomName, "hi", u)

into:

进入:

SET(u)

To achieve that, you can use:

为此,您可以使用:

String newString = "SET(" + myString.Substring(myString.LastIndexOf(',') + 1).Trim();

To explain this bit by bit:

一点一点地解释这一点:

myString.LastIndexOf(',') 

will give you the index (position) of your last ,character. Increment it by 1 to get the start index of the third argument in your SETfunction.

将为您提供最后一个,字符的索引(位置)。将其增加 1 以获取SET函数中第三个参数的起始索引。

myString.Substring(myString.LastIndexOf(',') + 1)

The Substringmethod will eliminate all characters up to the specified position. In this case, we're eliminating everything up to (and including) the last ,. In the example above, this would eliminate the SET(someRandomName, "hi",part, and leave us with u).

Substring方法将消除指定位置之前的所有字符。在这种情况下,我们将消除(包括)最后一个,. 在上面的示例中,这将消除该SET(someRandomName, "hi",部分,并为我们留下u).

The Trimis necessary simply to remove the leading space character before your u.

Trim是必要的仅仅是你之前删除前导空格字符u

Finally, we prepend SET(to our substring (since we had formerly removed it due to our Substring).

最后,我们添加SET(到我们的子字符串(因为我们之前由于我们的 删除了它Substring)。

Edit: Based on your comment below (which contradicts what you asked in your question), you can use:

编辑:根据您在下面的评论(这与您在问题中提出的问题相矛盾),您可以使用:

String newString = "SET(" + myString.Substring(myString.IndexOf(',') + 1).Trim();

回答by Brandon Moretz

This is pretty awful, but this will accomplish what you want with a simple linq statement. Just presenting as an alternative to the IndexOfanswers.

这非常糟糕,但这将通过简单的 linq 语句完成您想要的。只是提出作为IndexOf答案的替代方案。

string myString = "SET(someRandomName, \"hi\", 0)";
string fixedStr = new String( myString.ToCharArray().Take( 4 ).Concat( myString.ToCharArray().SkipWhile( c => c != '"' ) ).ToArray() );

yields: SET("hi", 0)

产量:SET(“嗨”,0)

Note: the skip is hard-coded for 4 characters, you could alter it to skip over the characters in an array that contains them instead.

注意:skip 是硬编码的 4 个字符,您可以更改它以跳过包含它们的数组中的字符。