本文整理汇总了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 != "")
//.........这里部分代码省略.........