Android Question PadNZeros

Carlo2015

Member
Licensed User
Longtime User
PHP:
function PadNZeros($digit, $padnum)
{
    return sprintf("%0".$padnum."d", $digit);
}



Can someone please help me with this in b4a?

Also do we have an equivalent function for ucfirst (uppercase first character in php)?

Thanks in advance


Carlo
 

Carlo2015

Member
Licensed User
Longtime User
To answer my ucfirst question

i came up with this. Hope this will help someone who is looking for same function

B4X:
Sub ucfirst(str As String) As String
    str = str.ToLowerCase
  
    Try
        Dim Length = str.Length As Int
       
        Dim firstchar = str.SubString2(0,1).ToUpperCase As String
      
        Return firstchar & str.SubString2(1, Length - 1)  
    Catch
        Return str
    End Try
End Sub



And I modified NJDude's function to fit my need (Thanks NJDude)

B4X:
Sub PadNZeros(num As String, Length As Int) As String
    Dim Padding As String
    Length = Abs(Length) 
    For I = 0 To Length
        Padding = Padding & "0"
    Next
    num = Padding & num
    num = num.SubString2(num.Length - Length, num.Length)
  
    Return num
End Sub
 
Upvote 0
Top