c# - How can I get the IndexOf() method to return the correct values? -


i have been working googlemaps , looking format coordinates.

i coordinates in following format:

address(coordinates)zoomlevel.

i use indexof method start of "(" +1 first number of coordinate , store value in variable call "start".

i them same thing time index of ")" -2 last number of last coordinate , store value in variable call "end".

i following error: "index , length must refer location within string.parameter name: length"

i following string imparameter:

"loddvägen 155, 840 80 lillhärdal, sverige (61.9593214318303,14.0585965625)5" 

by calculations should value 36 in start variable , value 65 in end variable

but reason values 41 in start , 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 tried hardcoding correct values

string test = cord.substring(36,65); 

i following error:

startindex cannot larger length of string. parameter name startindex

i understand both of errors mean in case incorrect since im not going beyond strings length value.

thanks!

the second parameter of substring length (msdn source). since passing in 65 second parameter, call trying characters between 36 , 101 (36+65). string not have 101 characters in it, error thrown. data between ( 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: reason worked coordinates, because length of total string shorter, , since coordinates started @ first position, end coordinate last position. 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 @ 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 @ characters 1 through 31 

the second instance valid because 31 existed in string. once added address beginning of string, code no longer work.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -