2010-02-15 85 views
1

我想將下面的javascript函數轉換爲c# 任何人都可以幫忙嗎?將Javascript函數轉換爲c#

function parseCoordinate(coordinate,type,format,spaced) { 
    coordinate = coordinate.toString(); 
    coordinate = coordinate.replace(/(^\s+|\s+$)/g,''); // remove white space 
    var neg = 0; if (coordinate.match(/(^-|[WS])/i)) { neg = 1; } 
    if (coordinate.match(/[EW]/i) && !type) { type = 'lon'; } 
    if (coordinate.match(/[NS]/i) && !type) { type = 'lat'; } 
    coordinate = coordinate.replace(/[NESW\-]/gi,' '); 
    if (!coordinate.match(/[0-9]/i)) { 
     return ''; 
    } 
    parts = coordinate.match(/([0-9\.\-]+)[^0-9\.]*([0-9\.]+)?[^0-9\.]*([0-9\.]+)?/); 
    if (!parts || parts[1] == null) { 
     return ''; 
    } else { 
     n = parseFloat(parts[1]); 
     if (parts[2]) { n = n + parseFloat(parts[2])/60; } 
     if (parts[3]) { n = n + parseFloat(parts[3])/3600; } 
     if (neg && n >= 0) { n = 0 - n; } 
     if (format == 'dmm') { 
      if (spaced) { 
       n = Degrees_to_DMM(n,type,' '); 
      } else { 
       n = Degrees_to_DMM(n,type); 
      } 
     } else if (format == 'dms') { 
      if (spaced) { 
       n = Degrees_to_DMS(n,type,' '); 
      } else { 
       n = Degrees_to_DMS(n,type,''); 
      } 
     } else { 
      n = Math.round(10000000 * n)/10000000; 
      if (n == Math.floor(n)) { n = n + '.0'; } 
     } 
     return comma2point(n); 
    } 
} 
+0

任何部分,特別是你有問題? – Fermin

+0

是的,要轉換「coordinate.match(/(^ - | [WS])/ i));」(.match&正則表達式) –

+0

示例座標將有幫助...只是說... –

回答

-1
Regex myPattern =new Regex("/(^-|[WS])/i)"); 
if(myPattern.isMatch(coordinate)) 
    { neg = 1; } 

看看,如果這個工程

1

退房Regex on MSDN或對谷歌咋一看爲正則表達式C#

if (coordinate.match(/(^-|[WS])/i)) { neg = 1; } 

將成爲:

using System.Text.RegularExpressions; 

Regex myRegex = new Regex("/(^-|[WS])/i)"); 
if (coordinate.IsMatch(myRegex)) 
{ 
    neg=1; 
} 

如果將永遠像你的ab例如'N27 53.4891',那麼你可以將它存儲爲一個字符串。如果上述緯度分爲兩部分('N27'和53.4891),並且您需要單獨訪問它們,則可以使用自定義的座標類,例如,

public class coordinate 
{ 
    public string otherPart {get; set;} // N27 
    public float coordPart {get; set;} // 53.4891 
} 

然後,您可以覆蓋.toString()方法以獲得'N27 53.4891'。

+0

感謝Fermin.but你認爲什麼數據類型的座標? –

+0

已更新的答案。 – Fermin