比如输入一个字符串24度37’45″,如何在C#中分别获取度,分,秒?还有比如输入的不标准呢?比如24度,或者37’45″,或者24度45″这时怎么获取?获取同时能不能判断度数大于0小于360,分数大于0小于60?我不动正则表达式,C#也是菜鸟,请各位大神写详细点儿,谢谢大家啦!
—- 5分
(([1-9]\d?|[12]\d{2}|3[1-5]\d)度)?(([1-9]|[1-5]\d)’)?(([1-9]|[1-5]\d)”)?
—- 50分
比较笨的实现:自己再优化
string str = "37'45\""; int du = 0, fen = 0, miao = 0; if (str.Contains("度")) { string[] arrStr = str.Split(new string[] { "度" }, StringSplitOptions.None); int.TryParse(arrStr[0], out du); str = arrStr[1]; } if (str.Contains("'")) { string[] arrStr = str.Split(new string[] { "'" }, StringSplitOptions.None); int.TryParse(arrStr[0], out fen); str = arrStr[1]; } if (str.Contains("\"")) { string[] arrStr = str.Split(new string[] { "\"" }, StringSplitOptions.None); int.TryParse(arrStr[0], out miao); str = arrStr[1]; } MessageBox.Show(string.Format("{0}度{1}分{2}秒", du, fen, miao)); ---- 10分
void Main() { var list=new string[]{"4度37'45\"", "24度", "37'45\"", "24度45\""}; var reg=new Regex("(([1-9]\\d?|[12]\\d{2}|3[1-5]\\d)度)?(([1-9]|[1-5]\\d)')?(([1-9]|[1-5]\\d)\")?"); foreach(var str in list) { Match m =reg.Match(str); Console.WriteLine("{0}\t{1}\t{2}",m.Groups[1].Value,m.Groups[3].Value,m.Groups[5].Value); } }
—- 5分
using System; using System.Text.RegularExpressions; namespace ConsoleApplication14 { internal class Program { private static void Main(string[] args) { string[] tests = { "24度37'45\"", "37'45\"", "37'45\"", "361度37'45\"", "20度99'45\"", "20度19'88\"", "20.88度19'20\"" }; foreach (string rawInput in tests) { Console.WriteLine("Processing: {0}", rawInput); try { Tuple<double, double, double> parsed = ParseRawString(rawInput); Console.WriteLine(parsed.Item1); Console.WriteLine(parsed.Item2); Console.WriteLine(parsed.Item3); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } Console.WriteLine(); } Console.ReadLine(); } /// <summary> /// Parse a raw string and extract the angle related info out. /// </summary> /// <param name="input"> /// </param> /// <exception cref="ArgumentNullException">Thrown if invalid input is received</exception> /// <returns> /// A tuple with degree as the first value, arc as the second value and second as the last value. /// </returns> public static Tuple<double, double, double> ParseRawString(string input) { if (string.IsNullOrEmpty(input)) { throw new ArgumentNullException("Null or empty input is not allowed."); } string floatNumberRegex = "([+-]?\\d+)(\\.\\d+)?"; var regex = new Regex(string.Format("^(?<degree>({0}度)?)(?<arc>({0}')?)(?<second>({0}\")?)$", floatNumberRegex)); Match match = regex.Match(input); if (!match.Success) { return null; } Func<string, double, double, double> sanityCheck = (groupName, min, max) => { Group g = match.Groups[groupName]; double result; if (g == null || g.Length <= 1) { return 0; } if (!double.TryParse(g.Value.Substring(0, g.Value.Length - 1), out result)) { throw new ArgumentException(string.Format("The value provided for {0} is not a number: {1}", groupName, g.Value)); } if (!(result >= min && result <= max)) { throw new ArgumentException(string.Format("The value provided for {0} is out of range([{1},{2}]): {3}", groupName, min, max, g.Value)); } return result; }; return new Tuple<double, double, double>(sanityCheck("degree", 0, 360), sanityCheck("arc", 0, 60), sanityCheck("second", 0, 60)); } } }
—-
给二楼是因为他的我能看懂,你们的看不懂,也没说明,不给我解释啊
CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#分离字符串中的度分秒!