简体   繁体   English

如何获取IndexOf()方法以返回正确的值?

[英]How can I get the IndexOf() method to return the correct values?

I have been working with googlemaps and i am now looking to format coordinates. 我一直在使用googlemaps,现在正在寻找格式化坐标的方法。

I get the coordinates in the following format: 我得到以下格式的坐标:

Address(coordinates)zoomlevel. 地址(坐标)缩放级别。

I use the indexof method to get the start of "(" +1 so that i get the first number of the coordinate and store this value in a variable that i call "start". 我使用indexof方法获取“(” +1)的开头,以便获取坐标的第一个数字,并将此值存储在我称为“开始”的变量中。

I then do them same thing but this time i get the index of ")" -2 to get the last number of the last coordinate and store this value in a variable that i call "end". 然后,我也做同样的事情,但是这次我得到“)”-2的索引,以获取最后一个坐标的最后一个数字,并将此值存储在我称为“结束”的变量中。

I get the following error: "Index and length must refer to a location within the string.Parameter name: length" 我收到以下错误:“索引和长度必须引用字符串中的位置。参数名称:length”

I get the following string as an imparameter: 我得到以下字符串作为参数:

"Loddvägen 155, 840 80 Lillhärdal, Sverige (61.9593214318303,14.0585965625)5"

by my calculations i should get the value 36 in the start variable and the value 65 in the end variable 通过我的计算,我应该在开始变量中获得值36,在结束变量中获得值65

but for some reason i get the values 41 in start and 71 in end. 但由于某种原因,我在开始时得到值41,在结束时得到值71。

why? 为什么?

public string RemoveParantheses(string coord)
        {
            int start = coord.IndexOf("(")+1;
            int end = coord.IndexOf(")")-2;

            string formated = coord.Substring(start,end);
            return formated;
        }

I then tried hardcoding the correct values 然后,我尝试对正确的值进行硬编码

string Test = cord.Substring(36,65);

I then get the following error: 然后,我得到以下错误:

startindex cannot be larger than length of string. startindex不能大于字符串的长度。 parameter name startindex 参数名称startindex

I understand what both of the errors mean but in this case they are incorrect since im not going beyond the strings length value. 我理解这两个错误的含义,但是在这种情况下,它们是不正确的,因为我没有超出字符串长度值。

Thanks! 谢谢!

The second parameter of Substring is a length ( MSDN source ). Substring的第二个参数是长度( MSDN源 )。 Since you are passing in 65 for the second parameter, your call is trying to get the characters between 36 and 101 (36+65) . 由于您要为第二个参数传递65 ,因此您的呼叫正在尝试使字符介于36101 (36+65) Your string does not have 101 characters in it, so that error is thrown. 您的字符串中没有101个字符,因此会引发错误。 To get the data between the ( characters, use this: 要获取(字符之间的数据,请使用以下命令:

public string RemoveParantheses(string coord)
{
    int start = coord.IndexOf("(")+1;
    int end = coord.IndexOf(")")-2;

    string formated = coord.Substring(start, end - start);
    return formated;
}

Edit: The reason it worked with only the coordinates, was because the length of the total string was shorter, and since the coordinates started at the first position, the end coordinate was the last position. 编辑:之所以只使用坐标,是因为总字符串的长度较短,并且由于坐标是从第一个位置开始,所以结束坐标是最后一个位置。 For example... 例如...

//using "Loddvägen 155, 840 80 Lillhärdal, Sverige (61.9593214318303,14.0585965625)5"
int start = coord.IndexOf("(") + 1;  // 36
int end = coord.IndexOf(")")-2;      // 65
coord.Substring(start, end);         //looks at characters 35 through 101

//using (61.9593214318303,14.0585965625)5
int start = coord.IndexOf("(") + 1;  // 1
int end = coord.IndexOf(")")-2;      // 30
coord.Substring(start, end);         //looks at characters 1 through 31

The second instance was valid because 31 actually existed in your string. 第二个实例有效,因为您的字符串中实际存在31个实例。 Once you added the address to the beginning of the string, your code would no longer work. 将地址添加到字符串的开头后,您的代码将不再起作用。

That overload of Substring() takes two parameters, start index and a length. Substring()的重载有两个参数,开始索引和一个长度。 You've provided the second value as the index of the occurance of ) when really you want to get the length of the string you wish to trim, in this case you could subtract the index of ) from the index of ( . For example: - 您所提供的第二个值的次数的指数)当你真的想要得到你想要修剪字符串的长度,在这种情况下,你可以减去的指数)从指数(例如: --

string foo = "Loddvägen 155, 840 80 Lillhärdal, Sverige (61.9593214318303,14.0585965625)5";
int start = foo.IndexOf("(") + 1;
int end = foo.IndexOf(")");
Console.Write(foo.Substring(start, end - start));
Console.Read();

Alternatively, you could parse the string using a regular expression, for example: - 或者,您可以使用正则表达式来解析字符串,例如:-

Match r = Regex.Match(foo, @"\(([^)]*)\)");
Console.Write(r.Groups[1].Value);

Which will probably perform a little better than the previous example 可能会比之前的示例表现更好

Extracting parts of a string is a good use for regular expressions: 提取字符串的一部分对于正则表达式非常有用:

var match = Regex.Match(locationString, @"\((?<lat>[\d\.]+),(?<long>[\d\.]+)\)");

string latitude = match.Groups["lat"].Value;
string longitude = match.Groups["long"].Value;

You probably forgot to count newlines and other whitespaces, a \\r\\n newline is 2 "invisible" characters. 您可能忘了数换行符和其他空格,\\ r \\ n换行符是2个“不可见”字符。 The other mistake is that you are calling Substring with (Start, End) while its (Start, Count) or (Start, End - Start) 另一个错误是您在使用(开始,结束)或(开始,结束-开始)时使用(开始,结束)调用子字符串

string input = 
  "Loddvägen 155, 840 80 Lillhärdal, Sverige (61.9593214318303,14.0585965625)5";

var groups = Regex.Match(input, 
                         @"\(([\d\.]+),([\d\.]+)\)(\d{1,2})").Groups;
var lat = groups[1].Value;
var lon = groups[2].Value;
var zoom = groups[3].Value;

by my calculations i should get the value 36 in the start variable and the value 65 in the end variable 通过我的计算,我应该在开始变量中获得值36,在结束变量中获得值65

Then your calculations are wrong. 那么您的计算是错误的。 With the string above I also see (and LinqPad confirms) that the open paren is at position 42 and the close paren is at index 73. 在上面的字符串中,我还看到(并且LinqPad确认)打开的括号位于位置42,关闭的括号位于索引73。

The error you're getting when using Substring is becuase the parameters to Substring are a beginning position and the length , not the ending position, so you should be using: 您正在使用的时候得到的错误Substring是监守的参数Substring是一个开始位置和长度 ,而不是结束位置,所以你应该使用:

string formated = coord.Substring(start,(end-start+1));

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

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