user1986815
user1986815

Reputation:

VB.NET specific string formatting

I have a string, lets call it "S" and it can be up to 8 digits log, and I want to align it in a string, and later in a text file to the right in 8 blanks (chr(32))

Ex. ( I put underscores in the example to mark the blanks.

S="1234"    should result in "____1234"
S="444444"  should result in "__444444"
S="abc"     should result in "_____abc"

For this I would write the following code

Public Function feld(ByVal S As String, Optional I As Integer = 8) As String
    Dim lenS As Integer = Strings.Len(S)
    Dim vorS As Integer = I - lenS
    Dim rez As String = ""
    For x = 1 To vorS
        rez += Strings.Chr(32)
    Next
    rez += S
    Return rez
End Function

Is there a more elegant way to do this?

Upvotes: 1

Views: 1507

Answers (4)

abcool
abcool

Reputation: 95

(I hope you are using SQL if not just avoid this)

It would be easier later if you do this in SQL creating scalar-function and you need only left align so what I do is (below is just for executes having example, go and create function using below code):

DECLARE @StringValue varchar(8)='123'
DECLARE @totalStringLength As Integer=8
DECLARE @ReplaceChar As char(1)='_'

DECLARE @stringLength As int
DECLARE @NewString As varchar(8)
DECLARE @ResultVal As varchar(8)

SET @stringLength = LEN(ISNULL(@StringValue,@ReplaceChar))
IF @stringLength >= @totalStringLength 
Begin
    SET @ResultVal = @StringValue
End
Else
Begin
SET @NewString = REPLICATE(@ReplaceChar, @totalStringLength - @stringLength) + ISNULL(@StringValue, @ReplaceChar)
    SET @ResultVal = @NewString
End
select @ResultVal

Now, you can create a stored procedure or just call function where you are want the string value to be.

here I assume now you have created function something named as dbo.leftPAD, just use below code on command line

select dbo.leftpad(string.text,8,'_')

if this is not what you are looking than sorry and forgive for my bad English.

Thanks

Upvotes: 1

romulus001
romulus001

Reputation: 318

Other solution :

Public Function AddBlanks(ByVal S As String, Optional L As Integer = 8) As String
    return Microsoft.VisualBasic.Right(StrDup(L, " ") & S, L)
End Function

Upvotes: 1

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

You're making this way too complicated:

Public Function feld(ByVal S As String, Optional I As Integer = 8) As String
    Return S.PadLeft(I)
End Function

One wonders why you even need a new function, unless you want behavior different from the what PadLeft() uses in the case where the string is too long... but if that's the case, we need to know what behavior you want in the question.

Upvotes: 5

Reza Aghaei
Reza Aghaei

Reputation: 125197

You can use String.Format("{0,8}", s) to align input string at right side of the result which will contains 8 characters.

To align the input at left side, use a negative number in format.

For more information see Controlling formatting and Controlling spacing in String.Format method documentations.

Upvotes: 4

Related Questions