C# 在打开和关闭括号之前检查是否存在空白
我的字符串可能有多个开括号和闭括号,在我们使用这样的字符串发送电子邮件之前,我希望检查它的语法是否正确。字符串来自供应商 如果之前和/或之后没有空格,我想添加一个空格 当前,代码将遍历并检查或全部C# 在打开和关闭括号之前检查是否存在空白,c#,C#,我的字符串可能有多个开括号和闭括号,在我们使用这样的字符串发送电子邮件之前,我希望检查它的语法是否正确。字符串来自供应商 如果之前和/或之后没有空格,我想添加一个空格 当前,代码将遍历并检查或全部(并检查索引-1处的字符是否为空白,如果不是,则将其添加,然后对执行类似操作) 这是我们收到的其中一个供应商代码,我们被告知它可以工作,因为字符串最多可以包含40个字符 我可以使用正则表达式检查和添加空间吗?我查看了SO,到目前为止还没有找到任何内容,找到了使用正则表达式从某些字符中提取文本的文章。这里
(
并检查索引-1
处的字符是否为空白,如果不是,则将其添加,然后对执行类似操作)
这是我们收到的其中一个供应商代码,我们被告知它可以工作,因为字符串最多可以包含40个字符
我可以使用正则表达式检查和添加空间吗?我查看了SO,到目前为止还没有找到任何内容,找到了使用正则表达式从某些字符中提取文本的文章。这里有一个非正则表达式的组合,用于插入空格,正则表达式用于将2个空格减少为1个空格
string text = " ( ( (abc ) def)ghi)";
text = Regex.Replace(text.Replace("(", " ( ").Replace(")", " ) "), @"[ ]{2,}", @" ");
Console.WriteLine(text);
这里有一个小的清理器,有一个String.Replace()
&一个Regex.Replace()
结果:
( ( ( abc ) def ) ghi )
( ( ( abc ) def ) ghi )
00:00:00.0000383
( ( ( abc ) def ) ghi )
00:00:00.0000333
( ( ( abc ) def ) ghi )
00:00:00.0000114
( ( ( abc ) def ) ghi )
00:00:00.0000080
'' => ''
'(' => ' ( '
')' => ' ) '
'()' => ' ( ) '
'( ' => ' ( '
' )' => ' ) '
'( )' => ' ( ) '
' ( ' => ' ( '
' ( ( ' => ' ( ( '
更新
Regex
和非Regex
之间的性能一直在我的脑海中萦绕,因此我决定用纯非Regex
方法测试我已经提供的两个示例
您将在下面的代码中看到,InsertSpaces1()
是我的第一个示例,InsertSpaces2()
是我的第二个示例InsertSpaces3()
&InsertSpaces4()
和在括号前后插入空格的纯非Regex
方法InsertSpaces3()
将数据保存在StringBuilder
中,而InsertSpaces4()
将数据保存在字符串中。结果表明,平均而言,InsertSpaces4()
是实现结果的最快方法,尽管它可能不是内存效率最高的方法,因为每次调用Insert()
都会生成新字符串InsertSpaces3()
排在第二位,但在内存使用方面可能更有效
using System;
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
InsertSpaces1();
InsertSpaces2();
InsertSpaces3();
InsertSpaces4();
}
private static void InsertSpaces1()
{
Stopwatch w = new Stopwatch();
w.Start();
string text = " ( ((abc ) def)ghi)";
text = Regex.Replace(text.Replace("(", " ( ").Replace(")", " ) "), @"[ ]{2,}", @" ");
Console.WriteLine(text);
w.Stop();
Console.WriteLine(w.Elapsed);
}
private static void InsertSpaces2()
{
Stopwatch w = new Stopwatch();
w.Start();
string text = " ( ((abc ) def )ghi)";
text = Regex.Replace(text.Replace(" ", String.Empty), "\\w+|[()]", "$0 ");
Console.WriteLine(text);
w.Stop();
Console.WriteLine(w.Elapsed);
}
private static void InsertSpaces3()
{
Stopwatch w = new Stopwatch();
w.Start();
StringBuilder text = new StringBuilder("( ((abc ) def )ghi)");
for (int i = 0; i < text.Length; i++)
{
if (
// Insert a space to the left even at the beginning of the string
(text[i] == '(' && ((i - 1 >= 0 && text[i - 1] != ' ') || i == 0)) ||
(text[i] == ')' && ((i - 1 >= 0 && text[i - 1] != ' ') || i == 0))
)
{
text.Insert(i, ' ');
}
else if (
// Insert a space to the right
(text[i] == '(' && (i + 1 < text.Length && text[i + 1] != ' ')) ||
(text[i] == ')' && (i + 1 < text.Length && text[i + 1] != ' '))
)
{
text.Insert(i + 1, ' ');
}
else if (
// Insert a space to the right even at the end
(text[i] == '(' && (i + 1 == text.Length)) ||
(text[i] == ')' && (i + 1 == text.Length))
)
{
text.Append(" ");
}
}
Console.WriteLine(text);
w.Stop();
Console.WriteLine(w.Elapsed);
}
private static void InsertSpaces4()
{
Stopwatch w = new Stopwatch();
w.Start();
string text = "( ((abc ) def )ghi)";
for (int i = 0; i < text.Length; i++)
{
if (
// Insert a space to the left even at the beginning of the string
(text[i] == '(' && ((i - 1 >= 0 && text[i - 1] != ' ') || i == 0)) ||
(text[i] == ')' && ((i - 1 >= 0 && text[i - 1] != ' ') || i == 0))
)
{
text = text.Insert(i, " ");
}
else if (
// Insert a space to the right
(text[i] == '(' && (i + 1 < text.Length && text[i + 1] != ' ')) ||
(text[i] == ')' && (i + 1 < text.Length && text[i + 1] != ' '))
)
{
text = text.Insert(i + 1, " ");
}
else if (
// Insert a space to the right even at the end
(text[i] == '(' && (i + 1 == text.Length)) ||
(text[i] == ')' && (i + 1 == text.Length))
)
{
text += " ";
}
}
Console.WriteLine(text);
w.Stop();
Console.WriteLine(w.Elapsed);
}
}
请参阅此处的工作示例 这似乎有效。如果有两个相邻的,你会得到两个空格
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
public void Run()
{
this.Attempt("");
this.Attempt("(");
this.Attempt(")");
this.Attempt("()");
this.Attempt("( ");
this.Attempt(" )");
this.Attempt("( )");
this.Attempt(" ( ");
this.Attempt(" ( ( ");
}
private void Attempt(string original)
{
System.Console.WriteLine("'" + original + "' => '" + this.Replace(original) + "'" );
}
private string Replace(string original)
{
string regex = @"(\s*(\)|\()\s*)";
Regex replacer = new Regex(regex);
string replaced = replacer.Replace(original, " $2 ");
return replaced;
}
结果:
( ( ( abc ) def ) ghi )
( ( ( abc ) def ) ghi )
00:00:00.0000383
( ( ( abc ) def ) ghi )
00:00:00.0000333
( ( ( abc ) def ) ghi )
00:00:00.0000114
( ( ( abc ) def ) ghi )
00:00:00.0000080
'' => ''
'(' => ' ( '
')' => ' ) '
'()' => ' ( ) '
'( ' => ' ( '
' )' => ' ) '
'( )' => ' ( ) '
' ( ' => ' ( '
' ( ( ' => ' ( ( '
您可以这样做:
string text = " ( ((abc ) def)ghi)";
text = text.Replace("(","( ").Replace(")"," )");//Add aditional space no matter if this one already have it.
while(text.Contains(" ")){//===>To while text contains double spaces
text = text.Replace(" "," ");//===>Replace spaces for a single space;
}
如果左括号在这行的开头呢?你还想在之前添加空格吗?你能举一个这样的字符串的例子吗?那么,如果在左/右括号前后添加空格,字符串超过40个字符,该怎么办?如果已经存在空格,该解决方案将添加空格,以便存在多个空格。或者我可以删除前后的所有空格然后再把它们加进去,虽然不优雅,但job@GeorgeChond更新该场景如果您关心内存使用情况,则每次调用“Replace”时都会创建一个新字符串