我正在尝试创建一个简单的缩小器,因为我对在线工具不满意。我已经制作了一个控制台应用程序,但问题是即使我拆分文本并删除/n 和/t 字符,也没有删除任何内容。
我尝试了不同的方法来删除空格。
static string restrictedSymbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ,0123456789";
...
static void Compress(string command)
{
string[] commandParts = command.Split(' ');
string text = String.Empty;
try
{
using (StreamReader sr = new StreamReader(commandParts[1]))
{
text = sr.ReadToEnd();
text.Replace("n", "");
text.Replace("t", "");
string formattedText = text;
string[] splitText = text.Split(' ');
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splitText.Length - 1; i++)
{
splitText[i].TrimStart();
StringBuilder tSB = new StringBuilder(splitText[i]);
if (splitText[i].Length > 1 && splitText[i + 1].Length > 1)
{
int textLength = splitText[i].Length - 1;
if (restrictedSymbols.Contains(splitText[i + 1][0]) && restrictedSymbols.Contains(splitText[i][textLength]))
{
tSB.Append(" ");
}
}
sb.Append(tSB.ToString());
}
sb.Append(splitText[splitText.Length - 1]);
text = sb.ToString();
Console.WriteLine(text);
}
} catch (IOException e)
{
Console.WriteLine(e.ToString());
}
if (text != String.Empty)
{
try
{
using (StreamWriter stream = File.CreateText(commandParts[2] + commandParts[3]))
{
stream.Write(text);
}
}
catch (IOException e)
{
Console.WriteLine(e.ToString());
}
}
Console.WriteLine("Process Complete...");
GetCommand();
}
它应该打印输出一个缩小的文件,但它只是输出与我放入的完全相同的文件。
忽略任何其他问题,Replace
自己什么都不做
返回一个新字符串,其中当前字符串中指定 Unicode 字符或字符串的所有匹配项都将替换为 另一个指定的 Unicode 字符或字符串。
所以基本上你通过不保留返回值来忽略任何更改
至少你需要做这样的事情
text = text.Replace("n", "");
您正在替换字符,但随后什么都不做。
您的代码应该是:
text = text.Replace("n", "");
text = text.Replace("t", "");