当前位置: 首页>>代码示例>>C#>>正文


C# Regex.ToCharArray方法代码示例

本文整理汇总了C#中System.Text.RegularExpressions.Regex.ToCharArray方法的典型用法代码示例。如果您正苦于以下问题:C# Regex.ToCharArray方法的具体用法?C# Regex.ToCharArray怎么用?C# Regex.ToCharArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.RegularExpressions.Regex的用法示例。


在下文中一共展示了Regex.ToCharArray方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CompressedClassTimes

        /*
         * Method Name: CompressedClassTimes
         * Parameters: filename - The name of the file containing the data to 
         *                        be processed.
         * Output: No explicit output.
         * 
         * Author: Joshua Ford.
         * Date: 3/28/15
         * Modified by: Joshua Ford.
         * Description: Reads in line by line from the file and then validates 
         *              that the line is in proper format, checks for valid 
         *              data, and compresses data.
         */
        public CompressedClassTimes(String filename)
        {
            // Creates a temparary Dict to hold the compressed day and time.
            var tmpCompressedClassTimes = new Dictionary<String, 
                CompressedClassTime>();
            
            // Read in the file line by line.
            using (var sr = new StreamReader(filename))
            {
                // Line count (for giving a line number with errors).
                int lineCounter = 0;
                
                // Read until end of file (AKA read the entire file).
                while (!sr.EndOfStream)
                {
                    lineCounter++;

                    // Read one line.
                    String line = sr.ReadLine();

                    // Check that line format is correct.
                    Match validRow = new Regex("^[A-Z]+\\s[0-9]{4}\\s-\\s" +
                    "[0-9]{4},[0-9]+$").Match(line);
                    if (validRow.Success)
                    {
                        // Attempt to put each component of 
                        // line into a respective variable.
                        try
                        {
							String dayOfTheWeek = new Regex
								("[A-Z]+").Match(line).Value;
							
							if(dayOfTheWeek.Equals(false))
							{
								throw new Exception("Error - Invalid day " + 
									"of week");
							}

                            int classStartTime = int.Parse
                                (new Regex("[0-9]+").Match(line).Value);

                            if (classStartTime.Equals(false))
							{
								throw new Exception("Error - Invalid class " +
									"start time");
							}
                            int classEndTime = int.Parse(new 
                                Regex("[0-9]+").Match(line).NextMatch().Value);
                            int studentsEnrolled = 
                                int.Parse(new Regex("[0-9]+").Match(line).
                                NextMatch().NextMatch().Value);

                            if (studentsEnrolled.Equals(false))
                            {
                                throw new Exception("Error - Student Enrolled");
                            }
                            // Create new instance of ClassTime and pass line
                            // components.
                            var classTime = 
                                new ClassTime(dayOfTheWeek, classStartTime, 
                                    classEndTime, studentsEnrolled);

                            foreach (var c in dayOfTheWeek.ToCharArray())
                            {
                                // Round class start time down to the hour of.
                                int classStartTimeHour = classStartTime/100;
                                String key = c.ToString() + classStartTimeHour;

                                // If tmpCompressedClassTimes does not have a 
                                // key, create one.
                                if (!tmpCompressedClassTimes.ContainsKey(key))
                                {
                                    tmpCompressedClassTimes.Add(key, 
                                        new CompressedClassTime(c.ToString(), 
                                            classStartTimeHour));
                                }
                                // Add the class time to 
                                // tmpCompressedClassTimes.
                                tmpCompressedClassTimes[key].
                                    AddClassTime(classTime);
                            }
                        }
                        // Raise an error if any of the portions of the line 
                        // were not correct.
                        catch (Exception e)
                        {
                            if (e.Message != "")
//.........这里部分代码省略.........
开发者ID:ssmoke,项目名称:Portfolio,代码行数:101,代码来源:CompressedClassTimes.cs


注:本文中的System.Text.RegularExpressions.Regex.ToCharArray方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。