简体   繁体   中英

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.

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".

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".

I get the following error: "Index and length must refer to a location within the string.Parameter name: 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

but for some reason i get the values 41 in start and 71 in end.

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. parameter name 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 ). Since you are passing in 65 for the second parameter, your call is trying to get the characters between 36 and 101 (36+65) . Your string does not have 101 characters in it, so that error is thrown. 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. 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. 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. 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

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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