2017-06-03 36 views
-1
for(int i = 0; i < Rect.rectangles.Count; i++) 
     { 
      if (Rect.rectangles[i].transform != null && Rect.rectangles[i].transform != "") 
      { 
       string firstNumber = Rect.rectangles[i].transform.Substring(18, 10); 
       string secondNumber = Rect.rectangles[i].transform.Substring(7, 10); 
       double a = Convert.ToDouble(firstNumber); 
       double b = Convert.ToDouble(secondNumber); 
      } 
     } 

第一個數字是0.49052715及的是罰款Convert.ToDouble。 但是,當它變得前往路線:如何將字符串轉換爲double?我得到異常

double b = Convert.ToDouble(secondNumber); 

我得到異常:

出現FormatException:未知字符:,

但沒有任何字符/秒在secondNumber所有字符串是:0.87142591

這是整個串中變換:矩陣(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)

我提取前兩個數字:0.49052715和0.87142591,然後嘗試將它們轉換爲雙精度。但得到例外。

的代碼包括Rect.rectangles定義:

private void ParseSvgMap() 
    { 
     XDocument document = XDocument.Load(@"C:\Users\mysvg\Documents\my.svg"); 

     Rect.rectangles = document.Descendants().Where(x => x.Name.LocalName == "rect").Select(x => new Rect() 
     { 
      style = Rect.GetStyle((string)x.Attribute("style")), 
      id = (string)x.Attribute("id"), 
      width = (double)x.Attribute("width"), 
      height = (double)x.Attribute("width"), 
      x = (double?)x.Attribute("width"), 
      y = (double?)x.Attribute("width"), 
      transform = x.Attribute("transform") == null ? "" : (string)x.Attribute("transform") 
     }).ToList(); 

     for(int i = 0; i < Rect.rectangles.Count; i++) 
     { 
      if (Rect.rectangles[i].transform != null && Rect.rectangles[i].transform != "") 
      { 
       string firstNumber = Rect.rectangles[i].transform.Substring(18, 10); 
       string secondNumber = Rect.rectangles[i].transform.Substring(7, 10); 
       double a = Convert.ToDouble(firstNumber); 
       double b = Convert.ToDouble(secondNumber); 
       float degree = FindDegree(a, b); 
      } 
     } 
    } 

    public static float FindDegree(double a, double b) 
    { 
     float value = (float)((System.Math.Atan2(a, b)/System.Math.PI) * 180f); 
     if (value < 0) value += 360f; 

     return value; 
    } 

    public class Rect 
    { 
     public static List<Rect> rectangles { get; set; } 
     public Dictionary<string, string> style { get; set; } 
     public string id { get; set; } 
     public double width { get; set; } 
     public double height { get; set; } 
     public double? x { get; set; } 
     public double? y { get; set; } 
     public string transform { get; set; } 
     public double degree { get; set; } 

     public static Dictionary<string, string> GetStyle(string styles) 
     { 
      string pattern = @"(?'name'[^:]+):(?'value'.*)"; 
      string[] splitArray = styles.Split(new char[] { ';' }); 
      Dictionary<string, string> style = splitArray.Select(x => Regex.Match(x, pattern)) 
       .GroupBy(x => x.Groups["name"].Value, y => y.Groups["value"].Value) 
       .ToDictionary(x => x.Key, y => y.FirstOrDefault()); 
      return style; 
     } 
    } 
+0

你肯定'secondNumber'不包含無效字符?你有沒有在調試器中看到它的價值?因爲C#(通常)不會說謊 – niceman

+0

向我們展示'Rect.rectangles'的定義。 –

+2

在@niceman所說的調試器中檢查'secondNumber'的值。 – MiOnIs

回答

2

代碼像.Substring(18, 10)是無可救藥脆(如你發現)是一個等待發生的事故。數字不保證是相同的長度。看看你提供的樣本數據。

你應該使用類似正則表達式試圖解析它們之前提取從字符串的數字。

var regex = new Regex(@"matrix\((?<num>-?(?:\d|\.)+)(?:,(?<num>-?(?:\d|\.)+))*\)"); 
var data = "matrix(0.87142591,0.49052715,-0.49052715,0.87142591,0,0)"; 
var values = regex 
       .Matches(data) 
       .Cast<Match>() 
       .SelectMany(m => m.Groups["num"] 
           .Captures 
           .Cast<Capture>() 
           .Select(c=>c.Value)) 
       .Select(double.Parse) 
       .ToList(); 

然後挑出你想要values[0]values[1]的數字。

+1

似乎有點像對我來說矯枉過正。 – DaOnlyOwner

+1

@DaOnlyOwner可能。我喜歡我的代碼來驗證儘可能多的假設,可能的,但我可以看到你的POV – spender

+0

我明白;) – DaOnlyOwner

1

它註定要失敗的。 我會用以下的正則表達式:

 string test = "matrix(0.1234,0.23233433,0.34,-3343,-3.33)"; 
     Regex reg = new Regex("[-]?[0-9]+(\\.[0-9]+)?"); 
     MatchCollection coll = reg.Matches(test); 
+0

減號('-')的跡象? – spender

0

你可以寫的線沿線的一個方法:

public double fixString(string incoming) 
{ 
    Double a; 
    if(incoming.Contains(',')) 
    { 
    string fixedNum = incoming.Remove(incoming.IndexOf(',')); 
    a = Convert.ToDouble(fixedNum); 
    return a; 
    } 
    else 
    { 
    a = Convert.ToDouble(incoming); 
    return a; 
    } 
} 
相關問題