尝试创建简单的 js 缩小器,但它不会删除任何内容,即使我删除了白色 space
Trying to create simple js minifier, but it doesn't remove anything, even though I delete the white space
我正在尝试创建一个简单的缩小器,因为我对在线工具不满意。我制作了一个控制台应用程序,但问题是没有任何内容被删除,即使我拆分了文本并删除了 /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
它自己什么都不做
Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with
another specified Unicode character or String.
所以基本上你通过不保留 return 值
来忽略任何更改
至少你需要做这样的事情
text = text.Replace("\n", "");
您正在替换字符,但没有对其进行任何操作。
您的代码应该是:
text = text.Replace("\n", "");
text = text.Replace("\t", "");
我正在尝试创建一个简单的缩小器,因为我对在线工具不满意。我制作了一个控制台应用程序,但问题是没有任何内容被删除,即使我拆分了文本并删除了 /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
它自己什么都不做
Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another specified Unicode character or String.
所以基本上你通过不保留 return 值
来忽略任何更改至少你需要做这样的事情
text = text.Replace("\n", "");
您正在替换字符,但没有对其进行任何操作。
您的代码应该是:
text = text.Replace("\n", "");
text = text.Replace("\t", "");