2

I have the following input:

Dim str_format as string = "XXXXX00000"
Dim str as string = "INV"
Dim int as integer = "56"

How can I replace XXXXX with INV and replace 00000 with 56?

For the example above the result should be INVXX00056.

X can only replace with alphabet and 0 can only replace with integer, if str has more than five alphabet. The extra alphabets will be thrown away because str_format only has five X. The same algorithm is true for the integer.

Example 2

Dim str_format as string = "XXX00000"
Dim str as string = "ABCD"
Dim int as integer = 654321

Expected result: ABC54321

Process:

1. ABCD XXX00000 654321
2. ABC DXX000006 54321
3. AB CDX00065 4321
4. A BCD00654 321
5. ABC06543 21
6. ABC65432 1
7. ABC54321
3
  • 3
    Looks like homework to me. Questions asking for homework help must include a summary of the work you've done so far to solve the problem, and a description of the difficulty you are having solving it. Commented Dec 20, 2016 at 9:02
  • What you've done so far? Commented Dec 20, 2016 at 9:27
  • 1
    Do yourself a favor and turn on Option Strict Commented Dec 20, 2016 at 14:21

3 Answers 3

2

As Spidey mentioned... show some code. That said the process you describe is a bit long-winded.

The Letter part of the solution can be done by grabbing the first 3 characters of str using Left(str,3) this will bring in the leftmost 3 character (if there are less it will get what is there). Then check that you have 3 characters using str.Length(). If the length is less than 3 then append the appropriate number of 'X'.

The Numeric part can be done in a similar way. Your int is actually a string in your code above. If it was a real integer you can cast it to string. Use Right(int,5). Again check to see you have 5 digits and if not prepend with appropriate number of 0.

Have a go... if you run into problems post your code and someone is bound to help.

UPDATE

As there have been actual answers posted here is my solution

Function FormatMyString(str As String, num as String) As String

    Dim result As String

    result = Left(str,3).PadRight(3, "X"c).ToUpper() & Right(num,5).PadLeft(5, "0"c)

    Return result

End Function

UPDATE 2

based on Wiktors answer... made an amendment to my solution to cope with different formats

Function FormatMyString(str As String, num as String, alpha as Integer, digits as Integer) As String

    Dim result As String

    result = Left(str, alpha).PadRight(alpha, "X"c).ToUpper() & Right(num, digits).PadLeft(digits, "0"c)

    Return result

End Function

To use... 
FormatMyString("ABCDE", "56",3 5) will return ABC00056
FormatMyString("ABCDE", "123456",4 3) will return ABCD456
FormatMyString("AB", "123456",4 3) will return ABXX456
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a possible solution that just uses basic string methods and PadLeft/PadRight and a specific method to count occurrences of specific chars in the string. It assumes the format string can only contain X and 0 in the known order.

Public Function CountCharacter(ByVal value As String, ByVal ch As Char) As Integer
    Return value.Count(Function(c As Char) c = ch)
End Function

Public Sub run1()
    Dim str_format As String = "XXXXX00000" '"XXX00000"
    Dim str As String = "INV"
    Dim int As Integer = 56 '  ABC54321
    Dim xCnt As Integer = CountCharacter(str_format, "X")
    Dim zCnt As Integer = CountCharacter(str_format, "0")
    Dim result As String
    If xCnt > str.Length Then
        result = str.PadRight(xCnt, "X")
    Else
        result = str.Substring(0, xCnt)
    End If
    If zCnt > int.ToString().Length Then
        result = result & int.ToString().PadLeft(zCnt, "0")
    Else
        result = result & int.ToString().Substring(int.ToString().Length-zCnt
    End If
    Console.WriteLine(result)    
End Sub

Output for your both scenarios is as expected.

Comments

0

Take a look at this sample

Dim str_format As String = str_format.Replace("XXX", "ABC")
Msgbox(str_format )

As we assume that the X is 3 only. I dont want to give you more it is a start and everything will be easy.

If that kind of format is fix I mean the number of X will go or down then you can make a conditional statement based on the length of string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.