update Joey tools

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace JoeyTools
{
    
public class Tools
    {
        
#region FindMatches
        
public static List<string> FindMatches(string pattern, string target)
        {
            List
<string> l = new List<string>();
            Regex r 
= new Regex(pattern);
            MatchCollection mc 
= r.Matches(target);
            
foreach (Match m in mc)
            {
                l.Add(m.Value);
            }
            
return l;
        }
        
#endregion

        
#region FindMatch
        
public static string FindMatch(string pattern, string target)
        {
            Regex r 
= new Regex(pattern);
            Match m 
= r.Match(target);
            
return m.Value;
        }
        
#endregion

        
#region FindMatchCount
        
public static int FindMatchCount(string pattern, string target)
        {
            
return FindMatches(pattern, target).Count;
        }
        
#endregion

        
#region FindAndReplaceMatches
        
public static List<string> FindAndReplaceMatches(string pattern, string replacement, string target)
        {
            List
<string> l = new List<string>();
            Regex r 
= new Regex(pattern);
            MatchCollection mc 
= r.Matches(target);
            
foreach (Match m in mc)
            {
                l.Add(Regex.Replace(m.Value, pattern, replacement));
            }
            
return l;
        }
        
#endregion

        
#region ReplaceAllMatches
        
public static string ReplaceAllMatches(string pattern, string replacement, string target)
        {
            
return Regex.Replace(target, pattern, replacement);
        }
        
#endregion

        
#region ReplaceAllMatches
        
public static string ReplaceAllMatches(string pattern, string replacement, string target, bool ignoreCase)
        {
            
return ignoreCase ? Regex.Replace(target, pattern, replacement, RegexOptions.IgnoreCase) : Regex.Replace(target, pattern, replacement);
        }
        
#endregion

        
#region ReadAllText
        
public static string ReadAllText(string path)
        {
            
return File.ReadAllText(path);
        }
        
#endregion

        
#region ReadLargeFile
        
public static string ReadLargeFile(string path)
        {
            StringBuilder sb 
= new StringBuilder();
            
using (StreamReader reader = new StreamReader(path))
            {
                
string line = null;
                
while ((line = reader.ReadLine()) != null)
                {
                    sb.AppendLine(line);
                }
            }
            
return sb.ToString();
        }
        
#endregion

        
#region WriteAllText
        
public static void WriteAllText(string path, string contents)
        {
            path 
= path.Replace('/''\\');
            DirectoryInfo di 
= new DirectoryInfo(path.Substring(0, path.LastIndexOf(@"\")));
            
if (!di.Exists)
            {
                di.Create();
            }
            File.WriteAllText(path, contents);
        }
        
#endregion
    }
}
posted @ 2009-05-08 03:02  N/A2011  阅读(239)  评论(0编辑  收藏  举报