Android Question how to replace a single char in a string?

wbtcpip

Member
Licensed User
Longtime User
what is the simplest and fastest way to replace a single char in a string?
 

KMatle

Expert
Licensed User
Longtime User
How do you identify the char to change? Example: "Coffee". If you need to replace the e's it will be done with

B4X:
StringName.Replace ("e", "x")

Fact: It replaces all e's, so if wanted it would be ok, if not you need to explain more what you're trying to achieve.

Note: If you type e.g. a stringvariable's name plus "." the IDE shows all the methods available. Works with all objects. Give it a try.
 
Upvote 0

wbtcpip

Member
Licensed User
Longtime User
thank you but i want to replace only a single occurrence of a char not all the occurences

ex: "010575809"

i want to replace only the third char "0" with "-"
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
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
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Just for fun:
B4X:
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)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
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.



 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
in Visual Basic 6 it was
s$="010575809"
Mid$(s$,3,1)="-"
 
Upvote 0

wbtcpip

Member
Licensed User
Longtime User
in Visual Basic 6 it was
s$="010575809"
Mid$(s$,3,1)="-"

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
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
perfect ... i will use it!

Another option:

B4X:
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

BC is the ByteConverter library (1.10)

RBS
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
perfect ... i will use it!
Don't use jStringFunctions or StringFunctions libraries. They are not maintained and all the features are available with the core libraries.


Here the example of mc73 as a function
B4X:
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
B4X:
Log( ReplaceStr( "010575809", 2, "-")) 'Result: 01-575809
 
Upvote 0
Top