我正在尝试创建一个简单的缩小器,因为我对在线工具不满意。我已经制作了一个控制台应用程序,但是问题是什么都没有删除,即使我拆分了文本并删除了/ 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();
}
它应该打印输出一个缩小的文件,但它只输出我放入的相同文件。
答案 0 :(得分:3)
忽略任何其他问题,Replace
本身不会执行任何操作
返回一个新字符串,在该字符串中,当前字符串中所有出现的指定Unicode字符或字符串都替换为 另一个指定的Unicode字符或字符串。
因此,基本上,您不保留返回值就忽略了任何更改
至少您需要做类似的事情
text = text.Replace("\n", "");
答案 1 :(得分:2)
您要替换字符,但随后不执行任何操作。
您的代码应为:
text = text.Replace("\n", "");
text = text.Replace("\t", "");