C# - 子字符串:索引和长度必须引用字符串中的某个位置

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

C# - Substring: index and length must refer to a location within the string

c#.netstringexceptionsubstring

提问by Manto

I have a string that looks like

我有一个看起来像的字符串

string url = "www.example.com/aaa/bbb.jpg";

"www.example.com/" is 18 fixed in length. I want to get the "aaa/bbb" part from this string (The actual url is not example nor aaa/bbb though, the length may vary)

“www.example.com/”的长度固定为 18。我想从这个字符串中获取“aaa/bbb”部分(实际的 url 不是示例也不是 aaa/bbb,长度可能会有所不同)

so here's what I did:

所以这就是我所做的:

string newString = url.Substring(18, url.Length - 4);

Then I got the exception: index and length must refer to a location within the string. What's wrong with my code and how to fix it? Thanks in advance.

然后我得到了异常:索引和长度必须引用字符串中的一个位置。我的代码有什么问题以及如何修复它?提前致谢。

采纳答案by Tim S.

The second parameter in Substringis the length of the substring, not the end index.

in 的第二个参数Substring是子串的长度,而不是结束索引。

You should probably include handling to check that it does indeed start with what you expect, end with what you expect, and is at least as long as you expect. And then if it doesn't match, you can either do something else or throw a meaningful error.

您可能应该包括处理以检查它确实以您的期望开始,以您期望的结束,并且至少与您期望的一样长。然后如果它不匹配,你可以做其他事情或抛出一个有意义的错误。

Here's some example code that validates that url contains your strings, that also is refactored a bit to make it easier to change the prefix/suffix to strip:

这是一些验证 url 包含您的字符串的示例代码,该代码也进行了一些重构,以便更轻松地将前缀/后缀更改为条带:

var prefix = "www.example.com/";
var suffix = ".jpg";
string url = "www.example.com/aaa/bbb.jpg";

if (url.StartsWith(prefix) && url.EndsWith(suffix) && url.Length >= (prefix.Length + suffix.Length))
{
    string newString = url.Substring(prefix.Length, url.Length - prefix.Length - suffix.Length);
    Console.WriteLine(newString);
}
else
    //handle invalid state

回答by MAV

Your mistake is the parameters to Substring. The first parameter should be the start index and the second should be the length or offset from the startindex.

你的错误是 Substring 的参数。第一个参数应该是起始索引,第二个参数应该是起始索引的长度或偏移量。

string newString = url.Substring(18, 7);

If the length of the substring can vary you need to calculate the length.

如果子字符串的长度可以变化,则需要计算长度。

Something in the direction of (url.Length - 18) - 4(or url.Length - 22)

朝着(url.Length - 18) - 4(或url.Length - 22)方向的东西

In the end it will look something like this

最后它看起来像这样

string newString = url.Substring(18, url.Length - 22);

回答by Bishnu Paudel

Try This:

尝试这个:

 int positionOfJPG=url.IndexOf(".jpg");
 string newString = url.Substring(18, url.Length - positionOfJPG);

回答by Ken White

You need to find the position of the first /, and then calculate the portion you want:

你需要找到第一个的位置/,然后计算你想要的部分:

string url = "www.example.com/aaa/bbb.jpg";
int Idx = url.IndexOf("/");
string yourValue = url.Substring(Idx + 1, url.Length - Idx - 4);

回答by Bastardo

string newString = url.Substring(18, (url.LastIndexOf(".") - 18))

回答by Prashanth Thurairatnam

Here is another suggestion. If you can prepend http:// to your url string you can do this

这是另一个建议。如果你可以在你的 url 字符串前面加上 http:// 你可以这样做

  string path = "http://www.example.com/aaa/bbb.jpg";
  Uri uri = new Uri(path);            
  string expectedString = 
      uri.PathAndQuery.Remove(uri.PathAndQuery.LastIndexOf("."));

回答by HatSoft

How about something like this :

这样的事情怎么样:

string url = "http://www.example.com/aaa/bbb.jpg";
Uri uri = new Uri(url);
string path_Query = uri.PathAndQuery;
string extension =  Path.GetExtension(path_Query);

path_Query = path_Query.Replace(extension, string.Empty);// This will remove extension

回答by Pramod Mali

You can use padding right.

您可以正确使用填充。

string url = "www.example.com/aaa/bbb.jpg";
string newString=url.PadRight(4);
string newString = (url.Substring(18, url.Length - 4)).Trim();

Happy Coding!

快乐编码!