The line contains words from small letters. It is necessary to replace the first letters of words with large ones.

I decided to do this:

  1. Break the string into characters.
  2. Replace small characters with large ones.
  3. The array of characters turn into a string. char[] b;

I'm stuck on step 3. Can anyone write how to do this? It can be an explanation or a block of code.

  • Do all the words of the line need to be written with a big one? - LEQADA
  • Yes. The fact of the matter is that I was able to make the characters big, but I don’t know how to merge them into 1 line. - Tayker

7 answers 7

You can do it in different ways, of course, but in Java there are methods for working with strings. In an amicable way, you need to do this:

  1. Break the string into spaces by words and write to an array
  2. Go through the array, replacing the first characters with uppercase characters and putting them in one line.

No char arrays are needed. You will need the following String and Character methods:

  1. split (String regex) - divides a string into an array by some rule
  2. charAt (int index) - returns the character at the specified index
  3. toUpperCase (char ch) - converts to upper case
  4. substring (int beginIndex) - returns a substring starting at the specified index

If there are several spaces in the line and they need to be considered when assembling the text back, the algorithm will be as follows:

We pass on the line:

  1. If the character at this position is a space, then remember this event.
  2. Otherwise, if the character on the previous position was a space in Java (a usual space, tab, etc.) or the beginning of a line, then replace it with an uppercase character

    You will need the following StringBuilder and Character methods:

-

  1. length () - returns the length of the string
  2. charAt (int index) - returns the character at the specified position
  3. isWhitespace (char ch) - checks if the character is a Java space
  4. setCharAt (int index, char ch) - replaces the character at the specified position with the specified
  5. toUpperCase (char ch) - converts to upper case
  • one
    substring() is more likely to come in handy. - Axifive
  • Thanks, but just a minute ago I found another solution using toCharArray (), and translated it using Character.toString (b [i]). Of course it was in a loop. - Tayker
  • @IntFloat, correct comment. I add to the answer. Thank. - LEQADA
  • did not understand how to enter into the code - Tayker
  • @Tayker, you can answer your question. Below there is a blue button. - LEQADA

This operation is called capitalize , regardless of language. Not knowing Java at all, I googled Java capitalize and the first (!) Link gave:

 WordUtils.capitalize(string) 

https://stackoverflow.com/questions/1892765/capitalize-first-char-of-each-word-in-a-string-java

Actually, in one line without third-party libraries:

 str = str.replaceAll("((^|\\s+)(\\w))", "$2\\u\\$3"); 

Honestly, I don’t know if \u works in Java, but you can:

 str = str.replaceAll("((^|\\s+)(\\w))", "$2" + "$3".toUpperCase()); 

A regular looks for either the first letter in a string, or a letter that stands behind an arbitrary number of spaces and brings it to upper case, keeping what was before it (the same number of spaces, or nothing). I don’t have Java, perl testing, it works there.

P.S. outer parentheses are probably not needed.


Unfortunately, the proposed method without WordUtils will not work in Java for several reasons. The problem is interesting, so I will give its solution in a couple of other languages.

1) perl. The coolest of all. One line. There is no point even writing a function. perl, IMHO, cooler than all languages ​​works with strings:

 my $s = "simple string - example, Π° Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Русский тСкст"; $s =~ s/(\b\w)/\u$1/g; print "$s\n"; 

2) python. (The solution in Java will be similar, in 2.x Python it works only with English letters, alas)

 def capitalize(s): for g in re.finditer(r"^|\s+\w", s): s = s.replace(g.group(0), g.group(0).upper(), 1) return s if __name__ == "__main__": s = capitalize("my text solo\t\t\t\tstring\nmmm") print s 
  • one
    It is worth mentioning that this solution requires a third-party org.apache.commons:commons-lang3 , which should be added to the project dependencies. - pavlofff
  • @pavlofff, thanks, of course, it was worth mentioning. I am more to the fact that it is worth knowing the standard name of a more or less standard operation, and use Google. - andy.37
 BufferedReader reader = new BufferedReader(new InputStreamReader(System. in )); String s = reader.readLine(); char[] b = s.toCharArray(); if (b[0] != ' ') b[0] = Character.toUpperCase(b[0]); for (int i = 0; i < b.length - 1; i++) { if (b[i] == ' ' && b[i + 1] != ' ') { b[i + 1] = Character.toUpperCase(b[i + 1]); } } String result = ""; for (int i = 0; i < b.length; i++) { result += Character.toString(b[i]); } 
  • And in truth ... did not think about it. All because of (b[i+1]) or how? - Tayker
  • I slightly corrected your code. I hope you do not mind :) The answer can help someone in the future. - LEQADA
  • @LEQADA yes :() that's better. - Tayker

If you answer the question directly, you need to use the appropriate constructor :

 String str = new String(b); 

    if you need to make each first character in a line of text from small letters to large letters, I suggest this option with an explanation: (can someone come in handy)

      String str1 = "hot, java"; System.out.println( "рабочая строка : " + str1 ); String[] array = str1.split( " " ); // заполняСм массив Ρ‚ΠΈΠΏΠ° string словами System.out.println( "послС сплита ΠΏΠ΅Ρ€Π²ΠΎΠ΅ ΠΏΠΎΠ»Π΅ массива : " + array[0] ); System.out.println( "послС сплита Π²Ρ‚ΠΎΡ€ΠΎΠ΅ ΠΏΠΎΠ»Π΅ массива : " + array[1] ); System.out.println( "количСство ΠΏΠΎΠ»Π΅ΠΉ массива Ρ‚ΠΈΠΏΠ° string : " + array.length ); System.out.println( "ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ символа Π½ΡƒΠ»Π΅Π²ΠΎΠ³ΠΎ поля : " + array[0].charAt( 0 ) ); System.out.println( "ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ символа ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ поля : " + array[1].charAt( 0 ) ); for (int i = 0; i < array.length; i++) { char x = array[i].charAt( 0 ); // Ρ‚ΠΎΠ³Π΄Π° присваиваСм Π΅Π³ΠΎ ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ Ρ… Ρ‚ΠΈΠΏΠ° char x = Character.toUpperCase( x ); // мСняСм Π½Π° Π²Π΅Ρ€Ρ…Π½ΠΈΠΉ рСгистр символ Π² ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΉ Ρ… array[i] = array[i].replace( array[i].charAt( 0 ), x ); // замСняСм символ Π² Π½ΡƒΠ»Π΅Π²ΠΎΠΉ ΠΏΠΎΠ·ΠΈΡ†ΠΈΠΈ Π½ΡƒΠΆΠ½ΠΎΠ³ΠΎ поля Π½Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Ρ… Ρ‚ΠΈΠΏΠ° char } } System.out.println( "послС Ρ†ΠΈΠΊΠ»Π° ΠΏΠ΅Ρ€Π²ΠΎΠ΅ ΠΏΠΎΠ»Π΅ массива :" + array[0] ); System.out.println( "послС Ρ†ΠΈΠΊΠ»Π° Π²Ρ‚ΠΎΡ€ΠΎΠ΅ ΠΏΠΎΠ»Π΅ массива :" + array[1] ); System.out.println( "Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ Π½ΡƒΠΆΠ½ΠΎ Π±ΠΈΠ»Π΄Π΅Ρ€ΠΎΠΌ Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ ΠΈΠ· массива строки Π² ΠΎΠ΄Π½Ρƒ строку" ); String finalstring = String.join( " ", array ); // раздСляСм ΠΏΡ€ΠΎΠ±Π΅Π»Π°ΠΌΠΈ ΠΏΡ€ΠΈ ΠΈΠΌΠΏΠΎΡ€Ρ‚Π΅ Π² строку, Ρ‚.ΠΊ. сплит Ρ€Π°Π·Π±ΠΈΠ» ΠΏΠΎ ΠΏΡ€ΠΎΠ±Π΅Π»Π°ΠΌ System.out.println( "ΠΊΠΎΠ½Π΅Ρ‡Π½Ρ‹ΠΉ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ :" + finalstring ); 

      Standard operation

       String s = b.toString(); 

      Also converted back

       char[] b = s.toCharArray(); 
      • b.toString() and was used in the code - Tayker

      As for me this is the easiest way.

      Here is the code:

       char[] c = {'a','f','b'}; String s = new String(с);