by Eric A. Johnson (4 Submissions)
Category: String Manipulation
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Tue 2nd May 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)
This is an update to my earlier ShiftString function. I had one digit wrong by one, thus sometimes giving wrong results. This one has been
ByVal PositionToShift As Integer, _
ByVal NumChars As Integer, _
ByVal PlacesToShift As Integer) As String
' Variable declaration
Dim ShiftedString, SubString As String
Dim stringLength As Integer
ShiftedString = OriginalString
stringLength = ShiftedString.Length
' Error-checking section
If (NumChars < 0) Or (NumChars >= stringLength) Then
Return ShiftedString
End If
If PlacesToShift = 0 Then
Return ShiftedString
End If
If PositionToShift < 0 Or PositionToShift >= stringLength Then
Return ShiftedString
End If
If (PositionToShift + NumChars + PlacesToShift > stringLength) Or _
(PositionToShift + PlacesToShift < 0) Then
Return ""
End If
' Get substring to shift
SubString = ShiftedString.Substring(PositionToShift, NumChars)
' Remove substring from original string
ShiftedString = ShiftedString.Remove(PositionToShift, NumChars)
' Insert substring into new position
ShiftedString = ShiftedString.Insert(PositionToShift + PlacesToShift, _
SubString)
Return ShiftedString
End Function
No comments have been posted about This is an update to my earlier ShiftString function. I had one digit wrong by one, thus sometimes . Why not be the first to post a comment about This is an update to my earlier ShiftString function. I had one digit wrong by one, thus sometimes .