but u should make a own function (sub) for it that it is more handy.
B4X:
Sub TestDim 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)
EndIfLog(v)
EndSub
Private s As String="010575809"Private posToReplace As Int=2Private 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 StringDim i As LongDim arrBytes() As Byte
arrBytes = strString.GetBytes("ASCII")
For i = 0To arrBytes.Length - 1If arrBytes(i) = btOldByte Then
arrBytes(i) = btNewByte
Return BC.StringFromBytes(arrBytes, "ASCII")
EndIfNextReturn strString
EndSub
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-616854Private sb As StringBuilder
sb.Initialize
Return sb.Append( Text).Remove( Position, Position+1).Insert( Position, ReplaceString).ToString
EndSub
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.