Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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.
Posted 16-Jan-13 2:25am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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);
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.
  Permalink  
v3
Comments
thomasriley at 16-Jan-13 8:23am
   
Few tweaks as for one I'm in VB.NET but thanks phil.o that seemed to have worked!
phil.o at 16-Jan-13 8:24am
   
Sorry, it seems I misread the tag of your question.
If you have problems translating to VB let me know.
Glad to help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 455
1 Sergey Alexandrovich Kryukov 445
2 Maciej Los 290
3 ProgramFOX 275
4 CHill60 225
0 OriginalGriff 550
1 Sergey Alexandrovich Kryukov 470
2 Maciej Los 310
3 ProgramFOX 275
4 Peter Leow 250


Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100