What is the Java command that returns the uppercase characters?

Asked

Viewed 278 times

-5

For example, "Jorge Aragão Silva" ai returns me "J A S"

  • Take a look at this link here

  • Those answers here and here

  • Now if you want to take out just the initials of each word have this here and that here

  • But the spaces are to remove or include ?

  • 1

    I am still in doubt. If it is to return capital characters, then for "JOSÉ DA SILVA", would I have to return all letters? But it seems that you only want the initials of the name, so it should be "J D S"? Or "J S", because usually the "DA" does not count as initials of the name. And you kept the spaces, so it’s not to return only uppercase characters, right? If you can [Edit] the question making more clear what you need, with a few more examples etc. Take the opportunity to get to know the site better and how to ask the questions, reading the [tour] and the page [Ask].

4 answers

4

In the title of the question is to take only the uppercase letters of the words. In the example in the body of the text has spacings, but I am with the @André Filipe.

You can use regular expressions to remove non-uppercase letters:

String nomeCompleto = "Jorge Aragão Silva";
return nomeCompleto.replaceAll("\\P{Lu}", "");

The method replaceAll of the string in Java will replace every part that matches the pattern (first argument) with the substitute. The pattern I passed was \P{Lu}.

Read more about patterns

It consists of the denied property pattern Lu. What does that mean? Basically, if a letter is within the pattern Lu, it will be ignored; otherwise, if Lu, she will marry the standard and therefore be replaced.

The pattern Lu are the uppercase Unicode letters. Therefore, anything that is not uppercase Unicode will be removed.

If I wished a pattern that satisfied the property Lu, I would wear \p{Lu} with p very small. The P uppercase indicates that the property described {between keys} will be denied.

In the substitution, I put two against bars \\ as a matter of how Java treats the counter bar in strings. So if I write \\n, Java will understand that I wanted to type the string \n. Already \n Java interprets it as line breaking. To explicitly tell Java that I want a counter bar \ and a P uppercase, I used \\P.

Sources that helped me:

  1. https://stackoverflow.com/a/20661766/4438007
  2. https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replaceAll-java.lang.String-java.lang.String-
  3. https://stackoverflow.com/a/36312533/4438007
  4. https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
  • Good Jefferson, great answer master!

3

String nomeCompleto = "Jorge Aragão Silva";

StringBuilder iniciaisEmMaiusculo = new StringBuilder();
for (char letra : nomeCompleto.toCharArray()) {
    if (Character.isUpperCase(letra)) {
        iniciaisEmMaiusculo.append(letra);
    }
}
return iniciaisEmMaiusculo.toString(); //Retornará a String "JAS"

Good studies!

  • 1

    what’s that letter char for?

  • In this context the char variable type serves to catch each caractede of the nameCompleto.

  • 2

    @Ruan, that for there is known as foreach. This André Filipe tie is read this way: "for each character letra inside nomeCompleto.toCharArray(), do..."

  • 1

    Exact @Jeffersonquesado, thanks great!

  • 2

    If I’m not mistaken, StringBuilder has the method append, and not add

  • 2

    @hkotsubo truth

Show 1 more comment

2

Using the for after the split you can

Take a look

 String x = "Shojibur rahman";
 String[] myName = x.split(" ");
 String saida = "";
 for (int i = 0; i < myName.length; i++) {
    String s = myName[i].toUpperCase();
    saida += s.charAt(0)+" "; 
 }
 System.out.println(saida);

I hope I’ve helped

Quick test here

1

Simple example

       String nome = "Jorge Aragão Silva".toUpperCase();
       String iniciais = "";

       for (int i = 0; i < nome.length(); i++){
            char caractere = nome.charAt(i);        

            if(i == 0)
                iniciais+=caractere;

            if(caractere == ' ')
                iniciais+=nome.charAt(i+1);
        }   

Browser other questions tagged

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