Android Question regex or substring?

fifiddu70

Well-Known Member
Licensed User
Longtime User
hello, i have one spinner with this elemet: 1:Bicchiere di birra - Euro 5.50
i need to insert in other edittext: 1 Bicchiere di birra
without Price.
one help?
 

klaus

Expert
Licensed User
Longtime User
You can use both:
Regex:
B4X:
Dim str = "1:Bicchiere di birra - Euro 5.50" As String
Dim str1() As String
str1 = Regex.Split(" - ", str)
str1(0) = str1(0).Replace(":", " ")

SubString2:
B4X:
Dim str = "1:Bicchiere di birra - Euro 5.50" As String
Dim str2 As String
str2 = str.SubString2(0, str.IndexOf(" - "))
str2  = str2 .Replace(":", " ")

If you need to have also the price separated, Regex is easier.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
To take in account eventual dash separated names like "lemon-mint" I'd search from the end backward.
Try with "1: Fresh lemon-mint soda - Eur 7.60" to see what I mean.

udg
 
Upvote 0

fifiddu70

Well-Known Member
Licensed User
Longtime User
Thanks Friends, i try your code, work fine, but i replace ( " - " ) with ( " Euro " ) because i have elemtns with example: 1: Fresh lemon-mint soda - Eur 7.60 good work Klaus and Good work Udg, very thanks all.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In my code, "1: Fresh lemon-mint soda - Eur 7.60" works OK, because I use the empty characters " - " before the "-" character.
But this would be wrong: "1: Fresh lemon - mint soda - Eur 7.60"
But splitting with " - Euro " is better.
You might use " - Euro " to eliminate "-".
 
Upvote 0
Top