using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace TestReplate
{
class Replace
{
public bool tReplace(String subjectPath, String sourcePath)
{
//对两个文件的内容进行读取
try
{
StreamReader subjectStream = new StreamReader(subjectPath);
StreamReader sourceStream = new StreamReader(sourcePath);
//用字符串记录两个文件的内容
string subjectString = subjectStream.ReadLine();
string sourceString = sourceStream.ReadToEnd();
while (subjectString != null)
{
subjectString = subjectString.ToLower();
Console.WriteLine(subjectString);
String newRelaced = null;
String tobeRelaced = null;
newRelaced = charReplace(subjectString);
tobeRelaced = getFirstString(subjectString);
String RegexString = tobeRelaced + "_" + "\\w\\w\\w\\w";
sourceString = Regex.Replace(sourceString, RegexString, newRelaced);
Console.WriteLine(newRelaced);
Console.WriteLine(tobeRelaced);
subjectString = subjectStream.ReadLine();
//return true;
}
try
{
String name = getFileName(sourcePath);
StreamWriter sw = new StreamWriter("output\\"+name);
sw.Write(sourceString);
sw.Close();
}
catch (Exception x)
{
Console.WriteLine("无法存储");
return false;
}
return true;
}
catch (Exception x)
{
Console.WriteLine("您输入的路径是错误的!");
return false;
}
}
private String charReplace(String SubjectString)
{
String ResultString = null;
try
{
ResultString = Regex.Replace(SubjectString, "\\s{1,}", "_");
return ResultString;
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
return null;
}
private String getFirstString(String SubjectString)
{
String ResultString = null;
try
{
ResultString = Regex.Replace(SubjectString, "\\s{1,}\\w\\w\\w\\w", "");
return ResultString;
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
return null;
}
private String getFileName(String filePath)
{
String filename = "";
String[] arr = filePath.Split('\\');
filename = arr[arr.Length - 1];
return filename;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace TestReplate
{
class Replace
{
public bool tReplace(String subjectPath, String sourcePath)
{
//对两个文件的内容进行读取
try
{
StreamReader subjectStream = new StreamReader(subjectPath);
StreamReader sourceStream = new StreamReader(sourcePath);
//用字符串记录两个文件的内容
string subjectString = subjectStream.ReadLine();
string sourceString = sourceStream.ReadToEnd();
while (subjectString != null)
{
subjectString = subjectString.ToLower();
Console.WriteLine(subjectString);
String newRelaced = null;
String tobeRelaced = null;
newRelaced = charReplace(subjectString);
tobeRelaced = getFirstString(subjectString);
String RegexString = tobeRelaced + "_" + "\\w\\w\\w\\w";
sourceString = Regex.Replace(sourceString, RegexString, newRelaced);
Console.WriteLine(newRelaced);
Console.WriteLine(tobeRelaced);
subjectString = subjectStream.ReadLine();
//return true;
}
try
{
String name = getFileName(sourcePath);
StreamWriter sw = new StreamWriter("output\\"+name);
sw.Write(sourceString);
sw.Close();
}
catch (Exception x)
{
Console.WriteLine("无法存储");
return false;
}
return true;
}
catch (Exception x)
{
Console.WriteLine("您输入的路径是错误的!");
return false;
}
}
private String charReplace(String SubjectString)
{
String ResultString = null;
try
{
ResultString = Regex.Replace(SubjectString, "\\s{1,}", "_");
return ResultString;
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
return null;
}
private String getFirstString(String SubjectString)
{
String ResultString = null;
try
{
ResultString = Regex.Replace(SubjectString, "\\s{1,}\\w\\w\\w\\w", "");
return ResultString;
}
catch (ArgumentException ex)
{
// Syntax error in the regular expression
}
return null;
}
private String getFileName(String filePath)
{
String filename = "";
String[] arr = filePath.Split('\\');
filename = arr[arr.Length - 1];
return filename;
}
}
}