用“/”分隔字符串。角色,并调整它的任何一面 [英] Splitting a string by the "/" character, and adjusting either side of it
问题描述
大家好,
我有一个字符串,中间有一个/。这个角色的任何一边都有数字,但是这个字符串可以随时改变大小,但在它的中间总是有一个/。
我需要找到/之前的字符数和/之后的字符数。
/之前的最大字符数是4,/后的最大字符数为2.
我需要查看字符串第一部分的字符数是否为4 ,如果它不是,我需要在它的开头添加多少零'以使它长4个字符。
然后我需要看看字符串第二部分中的字符数是否为2,如果不是,我需要在字符串的那一部分的开头添加一个零。
非常感谢任何帮助,
谢谢。
Hello everyone,
I have a string that has a "/" in the middle of it. Either side of this character there are numbers, but this string can change size at any time, but there is always a "/" in the middle of it.
I need to find the number of characters before the "/" and the number of characters after the "/".
The maximum number of characters before the "/" is 4, and the maximum number of characters after the "/" is 2.
I need to see if the number of characters in the first part of the string is 4, if it isn''t, I need to add however many zero''s to the start of it to make it 4 characters long.
Then I need to see if the number of characters in the second part of the string is 2, if it isn''t, I need to add a zero to the start of that part of the string.
Any help is greatly appreciated,
thanks.
推荐答案
我认为提取这两个部分的正则表达式会做诀窍:
I think a regular expression to extract both parts would do the trick here :
Regex regex = new Regex(@"^(?<LeftPart>[\d]?)/(?<RightPart>[\d]?)
,RegexOptions.Compiled | RegexOptions.IgnoreCase);
匹配m = regex.Match(yourString);
if (m.Success)
{
string left = m.Groups [ LeftPart]。值;
string right = m.Groups [ RightPart跨度>]值。
while (left.Length < 4 )
left.Insert( 0 , 0\" 跨度>);
while (right.Length < 2 )
right.Insert( 0 , 0\" 跨度>);
}
其他
{
// < span class =code-comment>处理错误
}
", RegexOptions.Compiled | RegexOptions.IgnoreCase); Match m = regex.Match(yourString); if (m.Success) { string left = m.Groups["LeftPart"].Value; string right = m.Groups["RightPart"].Value; while (left.Length < 4) left.Insert(0, "0"); while (right.Length < 2) right.Insert(0, "0"); } else { // Handle error }
希望这会有所帮助。
问候。
Hope this helps.
Regards.
这篇关于用“/”分隔字符串。角色,并调整它的任何一面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!