首页 > 解决方案 > 试图创建简单的 js 缩小器,但它不会删除任何东西,即使我删除了空格

问题描述

我正在尝试创建一个简单的缩小器,因为我对在线工具不满意。我制作了一个控制台应用程序,但问题是没有任何内容被删除,即使我拆分了文本并删除了 /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();
        }

它应该打印输出一个缩小的文件,但它只输出与我输入的完全相同的文件。

标签: c#minify

解决方案


忽略任何其他问题,Replace它自己什么都不做

返回一个新字符串,其中当前字符串中出现的所有指定 Unicode 字符或字符串都替换为另一个指定的 Unicode 字符或字符串。

所以基本上你通过不保留返回值来忽略任何更改

至少你需要做这样的事情

text = text.Replace("\n", "");

推荐阅读