帮助字符串 [英] Help on string
问题描述
我想将字符串修改为112233445566在
" 11:22:33:44:55:66"
哪种方法最好?
使用RegExp?如果是的话怎么样?
提前致谢
Hi,
I want modify a string as "112233445566" in
"11:22:33:44:55:66"
which is the best method?
Using a RegExp? and if yes how?
Thanks in advance
推荐答案
>
字符串是否始终具有相同的长度?
包含:的标准是什么?
从这些问题的答案取决于解决方案。
如果你总是希望分成两个连续字符你可以做点什么
这样的
StringBuilder sb = new StringBuilder()
int start = 0;
string str =" 112233445566" ;;
while(str.Length - 开始> 2)
{
if(sb.Length == 0)
sb.Append(str.substring(start,2 ));
其他
{
sb.Append(":");
sb。追加(str.substring(start,2));
}
start + = 2;
}
这可能会给你一些想法,只要注意我在这里写的文字就可以了。
可能有错误
Chee rs,
-
Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通部
" SnakeS" <锡*********** @ hotmail.com>在消息中写道
news:Cc ******************* @ tornado.fastwebnet.it ..。
Hi,
Does the string has the same length always?
what is the criteria for include the ":"
From the answers to these question depends the solutions.
if you always want to split in two continuos chars you could do something
like this
StringBuilder sb = new StringBuilder()
int start = 0;
string str = "112233445566";
while ( str.Length - start > 2 )
{
if ( sb.Length == 0 )
sb.Append( str.substring( start, 2 ) );
else
{
sb.Append( ":" );
sb.Append( str.substring( start, 2 ) );
}
start +=2;
}
This may give you some ideas, just notice that I wrote the text here and it
may have errors
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"SnakeS" <Sn***********@hotmail.com> wrote in message
news:Cc*******************@tornado.fastwebnet.it.. .
我想修改一个字符串为112233445566。在
11:22:33:44:55:66
哪种方法最好?
使用RegExp?如果是的话怎么样?
提前致谢
Hi,
I want modify a string as "112233445566" in
"11:22:33:44:55:66"
which is the best method?
Using a RegExp? and if yes how?
Thanks in advance
是的,它是完美的!
我认为这可能是一个更短的代码。
感谢所有
" Ignacio Machin(.NET / C# MVP) < ignacio.machin AT dot.state.fl.us>在消息新闻中写了
:%2 *************** @ TK2MSFTNGP11.phx.gbl ...
Yes it''s perfect!
I thinked that could be a code more short.
Thanks for all
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2***************@TK2MSFTNGP11.phx.gbl...
字符串总是有相同的长度吗?
包含:的标准是什么?
从这些问题的答案中取决于解决方案。
如果你总是希望分成两个连续字符你可以做点什么
StringBuilder sb = new StringBuilder()
int start = 0;
string str =" 112233445566" ;;
while(str.Length - start> 2)
{
if(sb.Length == 0)
sb.Append (str.substring(start,2));
{
sb.Append(":);
sb.Append(str.substring(start, 2));
}
开始+ = 2;
}
这可能会给你一些想法,只需注意我在这里写的文字和
它可能有错误
干杯,
-
Ignacio Machin,
ignacio.machin at dot.state.fl.u s
佛罗里达州交通局
SnakeS <锡*********** @ hotmail.com>在消息中写道
新闻:抄送******************* @ tornado.fastwebnet.it ..
Hi,
Does the string has the same length always?
what is the criteria for include the ":"
From the answers to these question depends the solutions.
if you always want to split in two continuos chars you could do something
like this
StringBuilder sb = new StringBuilder()
int start = 0;
string str = "112233445566";
while ( str.Length - start > 2 )
{
if ( sb.Length == 0 )
sb.Append( str.substring( start, 2 ) );
else
{
sb.Append( ":" );
sb.Append( str.substring( start, 2 ) );
}
start +=2;
}
This may give you some ideas, just notice that I wrote the text here and it may have errors
Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"SnakeS" <Sn***********@hotmail.com> wrote in message
news:Cc*******************@tornado.fastwebnet.it.. .
我想将字符串修改为112233445566在
11:22:33:44:55:66
哪种方法最好?
使用RegExp?如果是的话怎么样?
提前致谢
Hi,
I want modify a string as "112233445566" in
"11:22:33:44:55:66"
which is the best method?
Using a RegExp? and if yes how?
Thanks in advance
C Addison Ritchie < CA ************* @ discussions.microsoft.com>写道:
C Addison Ritchie <CA*************@discussions.microsoft.com> wrote:
这似乎运作良好。
使用System.Text;
使用System.Text.RegularExpressions;
//你的字符串112233445566;
字符串s =" 112233445566" ;;
//创建正则表达式
Regex re = new Regex("(\\ d \\\\)");
//找到比赛
MatchCollection matches = re.Matches();
//构建字符串
string result = String.Format(" {0}:{1}:{2}:{3}:{4}:{5}",
匹配[0] .Value,
匹配[1] .Value,
匹配[2] .Value,
匹配[3] .Value,
匹配[4] .Value,
匹配[ 5] .Value);
返回结果;
输入字符串长度必须为12个字符且所有数字。在执行上面的代码之前我会断言这是真的。
This seems to work well.
using System.Text;
using System.Text.RegularExpressions;
// your string "112233445566";
string s = "112233445566";
// create the regular expression
Regex re = new Regex("(\\d\\d)");
// find the matches
MatchCollection matches = re.Matches();
// build the string
string result = String.Format("{0}:{1}:{2}:{3}:{4}:{5}",
matches[0].Value,
matches[1].Value,
matches[2].Value,
matches[3].Value,
matches[4].Value,
matches[5].Value);
return result;
The input string must be 12 characters long and all numbers. I would
assert that this is true before executing the above code.
虽然性能方面很糟糕 - 但实际上并非如此需要
才能在这里使用正则表达式,IMO。这里的代码我相信会比b $ b快得多:
if(s.Length!= 12)
{
//无论你的错误处理是什么
}
foreach(char c in s)
{
if(c<''0''|| c>''9'')
{
//无论你的错误是什么处理是
}
}
返回String.Concat(s.Substring(0,2),":" ;,
s.Substring(2,2),":",
s.Substring(4,2),":",
s.Substring(6,2),":",
s.Substring(8,2),":",
s.Substring(10,2));
或者,最后一位:
char [] result = new char [17];
int resultIndex = 0;
int origIndex = 0;
for(int i = 0; i< 6; i ++)
{
结果[resultIndex ++] = s [origIndex ++];
结果[resultIndex ++] = s [origIndex ++];
if(i!= 5)
{
结果[resultIndex ++] ='':'';
}
}
返回新字符串(结果);
(这可以避免创建太多额外的字符串。)
我不知道哪个会更快 - 或者使用StringBuilder会更好 - 但是我不认为在这里使用正则表达式是个好主意。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
That''s terrible in terms of performance though - there''s really no need
to use a regex here, IMO. Here''s code which I believe would be
significantly faster:
if (s.Length != 12)
{
// Whatever your error handling is
}
foreach (char c in s)
{
if (c < ''0'' || c > ''9'')
{
// Whatever your error handling is
}
}
return String.Concat(s.Substring(0, 2), ":",
s.Substring(2, 2), ":",
s.Substring(4, 2), ":",
s.Substring(6, 2), ":",
s.Substring(8, 2), ":",
s.Substring(10, 2));
Alternatively, for the last bit:
char[] result = new char[17];
int resultIndex=0;
int origIndex=0;
for (int i=0; i < 6; i++)
{
result[resultIndex++]=s[origIndex++];
result[resultIndex++]=s[origIndex++];
if (i != 5)
{
result[resultIndex++]='':'';
}
}
return new string(result);
(That avoids creating too many extra strings.)
I don''t know which would be faster - or using a StringBuilder would be
better - but I don''t think using a Regex here is a good idea.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于帮助字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!