Question

I am trying to get an item such as WO123000 to come out as WO-123000. Here is the code, but it isn't working for me.

WorkOrder = String.Format("{0:##-######}", Trim(WorkOrderNum))

This is where WorkOrderNum is WO123000. This error stems from VB6.Format migration issues.

Was it helpful?

Solution

If the length of the string remains constant:

Dim WorkOrderNum = "WO123000"   
Dim testStr As String = String.Format("{0}-{1}", WorkOrderNum.SubString(0, 2), WorkOrderNum.SubString(2, 6))    
Console.WriteLine(testStr)

'outputs WO-123000

Although I'm sure there's a better way of doing this which should include error handling etc this was just a demo!

OTHER TIPS

If you don't need to format the number itself, the following should give you what you want:

WorkOrder = Trim(WorkOrderNum).Insert(2, "-")

If the input varies a lot (as your comment states it does) then you'll want to extract the number first, then format it:

string WorkOrder = "abc123";
int woNum;
if (int.TryParse(System.Text.RegularExpressions.Regex.Match(WorkOrder, @"\d+").Value, out woNum))
{
    return String.Format("WO-{0:00000}", woNum);
}

I'm assuming that WO stands for 'Work Order'. This would suggest to me that the first two letters of every work order will be WO. This means that you can just do the following instead of formatting:

Dim WorkOrderNum As String = "WO123000"
Dim OutputOrderNum As String = WorkOrderNum.Replace("WO", "WO-")

Hope this helped,

Rodit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top