简体   繁体   English

String.Substring 解释

[英]String.Substring explanation

In my program I have a long string sent via another program.在我的程序中,我有一个通过另一个程序发送的长字符串。 This string is roughly 100 characters long and I only need a small portion of it.这个字符串大约有 100 个字符长,我只需要其中的一小部分。 For this, we'll say the string I'm sending is as follows:为此,我们会说我发送的字符串如下:

Hello, I am a test string.你好,我是测试字符串。 I am awfully long and full of things you don't want.我很长,而且充满了你不想要的东西。

So, to cut the string so I only get the portion I need I have used the following string method:所以,为了切断字符串,我只得到我需要的部分,我使用了以下字符串方法:

String.Substring(startPoint, Endpoint)

Now in my program I know that the bit of the string I need to use is exactly 38 characters from the start and is only 36 characters long.现在在我的程序中,我知道我需要使用的字符串位从一开始就正好是 38 个字符,并且只有 36 个字符长。 Thus my call looks something like this:因此,我的电话看起来像这样:

 String.Substring(38, 36)

And my program will happily read in test string from the above example.我的程序会很高兴地从上面的例子中读取test string

This doesnt show up any issues or bugs when I build the program.当我构建程序时,这不会显示任何问题或错误。 But when I run it, the computer complains that the end point can't be lower than the start point.但是当我运行它时,计算机抱怨终点不能低于起点。 The thing is, however, the part of the message I want, gets sent over.然而,问题是,我想要的消息部分被发送了。

I've tried modifying my call so that it would look similar to this:我试过修改我的电话,使其看起来类似于:

String.Substring(startPoint, startPoint + 36)

But that has returned a bunch of extra characters I don't need or want.但这返回了一堆我不需要或不需要的额外字符。

Now my thinking behind that method is that it works by taking my initial point, the 38th character from the beginning of the string, and then goes 36 characters along from the 38th.现在我对这种方法的想法是,它的工作原理是取我的初始点,从字符串开头的第 38 个字符,然后从第 38 个字符开始移动 36 个字符。 Is my thinking behind how this method works wrong?我的想法背后是这种方法是如何工作的吗?

Why does this computer complain about an issue but then does what I want anyway?为什么这台电脑会抱怨一个问题,但还是会做我想要的?

You keep saying "endpoint", but it's string length.你一直说“端点”,但它是字符串长度。 You're right in the way it's supposed to work.你的方式是正确的。 I'm not sure why you're getting a warning.我不确定你为什么会收到警告。 Here is the information on the method.这是有关该方法的信息。

http://msdn.microsoft.com/en-us/library/system.string.substring.aspx http://msdn.microsoft.com/en-us/library/system.string.substring.aspx

Is the warning coming from a plugin?警告是否来自插件?

From String.Substring Method (Int32, Int32)来自String.Substring Method (Int32, Int32)

Retrieves a substring from this instance.从此实例中检索子字符串。 The substring starts at a specified character position and has a specified length.子字符串从指定的字符位置开始并具有指定的长度。

So when you say所以当你说

String.Substring(startPoint, startPoint + 36)

That means, you can start the position at startPoint , and take startPoint + 36 character after that position.这意味着,您可以从startPoint开始该位置,并在该位置之后获取startPoint + 36字符。 Not only 36 character.不仅36字符。

If you want to take 36 character after the position at startPoint , you just need to;如果你想在startPoint的位置后取 36 个字符,你只需要;

String.Substring(startPoint, 36)

See this tutorial : http://www.dotnetperls.com/substring请参阅本教程: http : //www.dotnetperls.com/substring
The .NET Framework 4.5 documentation : http://msdn.microsoft.com/en-us/library/system.string.substring(v=vs.110).aspx .NET Framework 4.5 文档: http : //msdn.microsoft.com/en-us/library/system.string.substring(v=vs.110).aspx

The syntax actually is : String.Substring(Int32 startPosition,Int32 length)语法实际上是: String.Substring(Int32 startPosition,Int32 length)
Retrieves a substring from this instance.从此实例中检索子字符串。 The substring starts at a specified character position and has a specified length.子字符串从指定的字符位置开始并具有指定的长度。

Another overloaded version is String.Substring(Int32 startPosition)另一个重载版本是String.Substring(Int32 startPosition)
Retrieves a substring from this instance.从此实例中检索子字符串。 The substring starts at a specified character position and continues to the end of the string.子字符串从指定的字符位置开始,一直到字符串的末尾。

the right way to use the Substring Method anyway is:无论如何,使用 Substring 方法的正确方法是:

String yourstring=<whatevere you like>;
String destinationstring;
destinationstring=yourstring.Substring(38,36);

in this way you retrieve the string from char 38 up to 38+36通过这种方式,您可以检索从 char 38 到 38+36 的字符串

In case of string:在字符串的情况下:

startIndex: inclusive
endIndex: exclusive

Let's understand the startIndex and endIndex by the code given below.让我们通过下面给出的代码来理解 startIndex 和 endIndex。

String s="hello";  
System.out.println(s.substring(0,2));//he  

In the above substring, 0 points to h but 2 points to e (because the end index is exclusive).在上面的子串中,0 指向 h 但 2 指向 e(因为结束索引是独占的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM