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.

有帮助吗?

解决方案

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!

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top