c# - 试图创建简单的 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();
}
它应该打印输出一个缩小的文件,但它只输出与我输入的完全相同的文件。
解决方案
忽略任何其他问题,Replace
它自己什么都不做
返回一个新字符串,其中当前字符串中出现的所有指定 Unicode 字符或字符串都替换为另一个指定的 Unicode 字符或字符串。
所以基本上你通过不保留返回值来忽略任何更改
至少你需要做这样的事情
text = text.Replace("\n", "");
推荐阅读
- python - 如何转置 3D 数组以制作 n 个特征向量?
- mysql - Mysql提取json数据并搜索多个值
- java - 如何在spring boot中以json格式返回一个对象数组?
- javascript - 使用 flexbox 对齐项目
- vue.js - 从子组件mounted() 函数(Vue.js 2)列表中只接收到一个发射
- sql - 在我的 oracle PL/SLQ 函数中返回了我的光标,但并非所有行都被返回。您只能在 Oracle pl/sql 函数中返回 1 行吗?
- angular - 类型字符串不可分配给“从不”Angular
- android - 扩展后如何检测工具栏搜索视图上的点击?
- unix - Linux 单行命令 gzip 和移动
- python - 使用python从网站下载svg图像