Count the number of characters and insert a new one into a given position, into a string?

Asked

Viewed 612 times

0

I have both strings:

String num_tel1 = "03184872882" and the String num_tel2 = "84872882". They are 11 and 8 characters, respectively. How do I count this amount of characters and, if you have a String with 11 characters (num_tel1), insert the number 9, just after 031 and, if it has 8 characters, insert the number 9, in the first position, that is, before the first 8 (num_tel2)?

2 answers

4


You can use the method Stringbuilder::Insert to insert the number 9 in the position you want according to the size of your String.

We can do it this way:

public static void main(String[] args) { 
  System.out.println(formatString("03184872882"));
  System.out.println(formatString("84872882")); 
}

public static String formatString(String s) {
  StringBuilder sBuilder = new StringBuilder(s);
  sBuilder.insert(s.length() == 11 ? 3 : 0, "9");
  return sBuilder.toString();
}

Output:

031984872882
984872882

Note that I used a ternary operator to insert the number 9 in the correct position according to the size of the string. Basically, the ternary operator does this:

if(s.length() == 11) sBuilder.insert(3, "9"); // Insere o número 9 na quarta posição da `String`, depois do DDD.
else sBuilder.insert(0, "9"); // Insere o número na primeira posição da string.
  • thanks! Just an addendum to the reply, the ternary operator, within the Internet, from stringbuilder, at least in the case of android studio, did not work. But that’s what I wanted.

  • Hmm... I had tested here and it worked normally, how strange! But I’m glad I helped somehow. Thanks! :))

  • I also found it strange, but switching to normal operators worked normally. Anyway, thank you again!

1

I have a function that I always use, which is to validate phone. I will put here what you need for it to work that I will try to give you a light. In case it’s all Static because it’s a Util class I have. I modified it for your case blz:

public static final String TELEFONE_REGEX = "([0-9]{4}[0-9]{3}[1-9]9?(\\d{2})?).*";

public static String getTelefone(String telefone, Context context) {
        Pattern phoneRegex = Pattern.compile(TELEFONE_REGEX);

        telefone = reverseStr(telefone.replaceAll("\\D", ""));

        Matcher m = phoneRegex.matcher(telefone);

        if (m.matches())
            telefone = m.group(1);

        telefone = reverseStr(telefone);

        if (telefone.length() == 8)
            telefone = "9" + telefone;
        if (telefone.length() == 11)
            telefone = telefone.substring(0,3) + "9" + telefone.substring(3, telefone.length()-1);

        return telefone;
    }

public static String reverseStr(String input) {
        String output = "";
        for (int i = input.length() - 1; i >= 0; i--) output += input.charAt(i);
        return output;
    }

This function validates whether the input is a phone itself, and checks inversely.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.