I have a string which would be delivered to my application in the format below:
我有一个字符串,将以下面的格式传递给我的应用程序:
ece4241692a1c7434da51fc1399ea2fa155d4fc983084ea59d1455afc79fafed
What I need to do is format it for my database so it reads as follows:
我需要做的是为我的数据库格式化它,如下所示:
<ece42416 92a1c743 4da51fc1 399ea2fa 155d4fc9 83084ea5 9d1455af c79fafed>
I assume the easiest way to do this would be using regular expressions, but I have never used them before, and this is the first time I have ever needed to, and to be honest, I simply don't have the time to read up on them at the moment, so if anyone could help me with this I would be eternally grateful.
我认为最简单的方法是使用正则表达式,但我以前从未使用过它们,这是我第一次需要,而且说实话,我根本没有时间阅读此刻,如果有人能帮助我,我会永远感激。
3 个解决方案
#1
2
What about:
string input ="ece4241692a1c7434da51fc1399ea2fa155d4fc983084ea59d1455afc79fafed";
string target = "<" + Regex.Replace(input, "(.{8})", "$1 ").Trim() + ">";
Or
string another = "<" + String.Join(" ", Regex.Split(input, "(.{8})")) + ">";
#2
2
You might just be better served having a small static string parsing method to handle it. A regular expression might get it done, but unless you're doing a bunch in a batch you won't save enough in system resources for it to be worth the maintenance of a RegEx (if you're not already familiar with them I mean). Something like:
您可能只是更好地使用一个小的静态字符串解析方法来处理它。正则表达式可能会完成它,但除非你在批处理中做了很多事情,否则你将无法在系统资源上保存足够的值来维护RegEx(如果你还不熟悉它们我的意思是)。就像是:
private string parseIt(string str)
{
if(str.Length % 8 != 0) throw new Exception("Bad string length");
StringBuilder retVal = new StringBuilder(str)
for (int i = str.Length - 1; i >=0; i=i-8)
{
retVal.Insert(i, " ");
}
return "<" + retVal.ToString() + ">";
}
#3
2
Try
Regex.Replace(YOURTEXT, "(.{8})", "$1 ");
#1
2
What about:
string input ="ece4241692a1c7434da51fc1399ea2fa155d4fc983084ea59d1455afc79fafed";
string target = "<" + Regex.Replace(input, "(.{8})", "$1 ").Trim() + ">";
Or
string another = "<" + String.Join(" ", Regex.Split(input, "(.{8})")) + ">";
#2
2
You might just be better served having a small static string parsing method to handle it. A regular expression might get it done, but unless you're doing a bunch in a batch you won't save enough in system resources for it to be worth the maintenance of a RegEx (if you're not already familiar with them I mean). Something like:
您可能只是更好地使用一个小的静态字符串解析方法来处理它。正则表达式可能会完成它,但除非你在批处理中做了很多事情,否则你将无法在系统资源上保存足够的值来维护RegEx(如果你还不熟悉它们我的意思是)。就像是:
private string parseIt(string str)
{
if(str.Length % 8 != 0) throw new Exception("Bad string length");
StringBuilder retVal = new StringBuilder(str)
for (int i = str.Length - 1; i >=0; i=i-8)
{
retVal.Insert(i, " ");
}
return "<" + retVal.ToString() + ">";
}
#3
2
Try
Regex.Replace(YOURTEXT, "(.{8})", "$1 ");