1

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?

Reza Aghaei
126k18 gold badges237 silver badges445 bronze badges
asked Nov 1, 2016 at 22:47
2
  • 1
    Seems to be a job for String.PadLeft (Or PadRight) Commented Nov 1, 2016 at 22:52
  • 1
    S.PadLeft(8, "_"c)) Commented Nov 1, 2016 at 23:01

4 Answers 4

5

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.

answered Nov 1, 2016 at 23:00
Sign up to request clarification or add additional context in comments.

Comments

4

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.

answered Nov 1, 2016 at 22:55

1 Comment

This way, it's enough to pass negative or positive length to simply align left or right.
1

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
answered Nov 2, 2016 at 14:02

Comments

1

(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

marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
answered Nov 2, 2016 at 14:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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