3
I’m taking the IMEI of the phone and I have to divide it into groups of 2 characters for me to convert them to hexa later.
This string comes without separator, the numbers come together, for example 36529874 and I wanted to separate them into groups of two numbers. So I take the String divide the first two and convert into Hexa, divide the two seconds and convert into Hexa, divide the third two and convert into Hexa. After converting these pairs into hexa I want to use only a few to show on the screen. How can I do this? I’m a beginner, I looked for a solution but I couldn’t find.
Too much waste using regex for something so simple.
– Luis Henrique
What else could I use @Luishenrique ?
– kaamis
What would be the parameters
("[0-9]{2}");
of the linePattern p = Pattern.compile("[0-9]{2}");
? @Fernando– kaamis
Substring solves your problem.
– Luis Henrique
I saw here @Luishenrique is more practical. If you can leave an example would be cool but thank you!
– kaamis
@kaamis ,edited as new information regarding '[0-9]{2}'
– Fernando Leal
@Luishenrique, I do not consider waste using Regex, since Regex is one of the fastest and most efficient forms of string manipulation, and it is still much more self-documenting (for those who know regular expressions) than any algorithm with
substring
and somefor's
do something similar. But please post your version withsubstring
, so that we may have another option.– Fernando Leal
Too bad I saw the comment only a year later. Fast and efficient is not. It’s just practice. We exchange efficiency and speed precisely for the practicality of solving in fewer lines.
– Bacco