but u should make a own function (sub) for it that it is more handy.
B4X:
Sub Test
Dim v As String = "010575809"
Log(v)
Log(v.SubString2(2,3))
If v.SubString2(2,3)="0" Then
v = v.SubString2(0,2) & "-" & v.SubString(3)
End If
Log(v)
End Sub
Private s As String="010575809"
Private posToReplace As Int=2
Private strReplace As String="-"
Private sb As StringBuilder
sb.Initialize
Private afterReplace As String=sb.Append(s).Remove(posToReplace,posToReplace+1).Insert(posToReplace,strReplace).ToString
Log(afterReplace)
@MarkusR and @mc73 : Both of your solution are correct. I think there is another way using Regex one line:
B4X:
Log(Regex.Replace(Pattern, "010575809", ""))
I cannot wrap my head around the pattern where you can replace a character at a given position with another.
Maybe someone knowledgeable with regex which I am not can come up with it effortlessly.
yes Markus i was searching for something easy like in Visual Basic, i was used to do it in 1 line with Mid$, but it seems multiple lines are needed in B4A
Sub ReplaceChar(strString As String, btOldByte As Byte, btNewByte As Byte) As String
Dim i As Long
Dim arrBytes() As Byte
arrBytes = strString.GetBytes("ASCII")
For i = 0 To arrBytes.Length - 1
If arrBytes(i) = btOldByte Then
arrBytes(i) = btNewByte
Return BC.StringFromBytes(arrBytes, "ASCII")
End If
Next
Return strString
End Sub
Sub ReplaceStr( Text As String, Position As Int, ReplaceString As String) As String
'2018-10-08
'https://www.b4x.com/android/forum/threads/how-To-replace-a-single-char-in-a-string.97875/#post-616854
Private sb As StringBuilder
sb.Initialize
Return sb.Append( Text).Remove( Position, Position+1).Insert( Position, ReplaceString).ToString
End Sub