Fix string Length

Jeackal

Member
Joined
Jun 15, 2004
Messages
8
Programming Experience
1-3
hello , i am new here n i need some help
i create a structure in a module then i wan to fix the length of a string how can do it??
coz in the main form i create 2 button to save the data in the structure n load the data.
but i was fail to save n load.
 

Jeackal

Member
Joined
Jun 15, 2004
Messages
8
Programming Experience
1-3
Jeackal said:
hello , i am new here n i need some help
i create a structure in a module then i wan to fix the length of a string how can do it??
coz in the main form i create 2 button to save the data in the structure n load the data.
but i was fail to save n load.
urgent pls
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
here was my quick solution to this question:

Public Function FixStringLen(ByRef MyString As String) As String
Dim MyChar As String
Dim X As Integer = 0
Dim MyLength As Integer = 3
Dim MyNewString As String = ""
If Len(MyString) >= MyLength Then
For X = 1 To MyLength
MyChar = Mid(MyString, X, 1)
MyNewString += MyChar
Next X
Return MyNewString
Else
MyNewString=MyString
For X = len(mystring) To MyLength
MyNewString += " "
Next X
Return MyNewString
End If
End Function
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
and my quicker solution using the new VB.NET code:

VB.NET:
Public Function FixStringLen(ByRef MyString As String, _
  ByVal iLength As Integer) As String
    If MyString.Length >= iLength Then
        Return MyString.Substring(0, iLength)
    Else
        Return MyString.PadRight(iLength)
        'To right align the string:
        'Return MyString.PadLeft(iLength)
    End If
End Function
 

hrothenb

New member
Joined
Oct 9, 2014
Messages
2
Programming Experience
10+
here was my quick solution to this question:

VB.NET:
    Function MakeFixedLength(ByVal str As String, ByVal desiredLength As Integer, ByVal LR As String, ByVal chr As Char) As String
        If str.Length >= desiredLength Then
            Return str.Substring(0, desiredLength)
        Else
            If LR = "L" Then
                Return str.PadLeft(desiredLength, chr).Substring(0, desiredLength)
            Else
                Return str.PadRight(desiredLength, chr).Substring(0, desiredLength)
            End If
        End If
    End Function
 
Top Bottom