How to use the first and last letters of each word in a string in java

Like the capital letters of the first and last letters of each word in a string

I did it this way -

String cap = ""; for (int i = 0; i < sent.length() - 1; i++) { if (sent.charAt(i + 1) == ' ') { cap += Character.toUpperCase(sent.charAt(i)) + " " + Character.toUpperCase(sent.charAt(i + 2)); i += 2; } else cap += sent.charAt(i); } cap += Character.toUpperCase(sent.charAt(sent.length() - 1)); System.out.print (cap); 

This does not work when the first word has more than one character

Please use simple functions since I'm new

+5
source share
5 answers

Using apache commons lang library , this will be very easy to do:

  String testString = "this string is needed to be 1st and 2nd letter-uppercased for each word"; testString = WordUtils.capitalize(testString); testString = StringUtils.reverse(testString); testString = WordUtils.capitalize(testString); testString = StringUtils.reverse(testString); System.out.println(testString); 

ThiS StrinG NEEDS TO BE 1sT AND 2nD Letter-uppercaseD ForR EacH WORD

+4
source

Most likely you will divide the String into a space as a character delimiter, then for each token apply toUpperCase () to the first and last character and create a new String result.

A very simple example:

  String cap = ""; String sent = "hello world. again."; String[] token = sent.split("\\s+|\\.$"); for (String currentToken : token){ String firstChar = String.valueOf(Character.toUpperCase(currentToken.charAt(0))); String between = currentToken.substring(1, currentToken.length()-1); String LastChar = String.valueOf(Character.toUpperCase(currentToken.charAt(currentToken.length()-1))); if (!cap.equals("")){ cap += " "; } cap += firstChar+between+LastChar; } 

Of course, you must approve of using StringBuilder over String when performing many concatenations.

Output Result: HellO World. AgaiN HellO World. AgaiN

+2
source

Your code is missing the first letter of the first word. I would consider this as a special case, i.e.

 cap = ""+Character.toUpperCase(sent.charAt(0)); for (int i = 1; i < sent.length() - 1; i++) { ..... 

Of course, there are much simpler ways to do what you do.

0
source

Basically, you just need to iterate over all the characters and replace them if one of the following conditions is true:

  • this is the first character
  • this is the last character
  • the previous character was a space (or what you want, for example, punctuation - see below)
  • the next character is a space (or whatever you want, such as punctuation - see below)

If you use StringBuilder for performance and memory reasons (don't create a String at every iteration that will be executed += ), it might look like this:

 StringBuilder sb = new StringBuilder( "some words in a list even with longer whitespace in between" ); for( int i = 0; i < sb.length(); i++ ) { if( i == 0 || //rule 1 i == (sb.length() - 1 ) || //rule 2 Character.isWhitespace( sb.charAt( i - 1 ) ) || //rule 3 Character.isWhitespace( sb.charAt( i + 1 ) ) ) { //rule 4 sb.setCharAt( i, Character.toUpperCase( sb.charAt( i ) ) ); } } 

Result: SomE WordS IN A LisT EveN WitH LongeR WhitespacE IN BetweeN

If you want to check other rules (for example, punctuation, etc.), you can create a method that you call for the previous and next character, and which checks the required properties.

0
source
 String stringToSearch = "this string is needed to be first and last letter uppercased for each word"; // First letter upper case using regex Pattern firstLetterPtn = Pattern.compile("(\\b[az]{1})+"); Matcher m = firstLetterPtn.matcher(stringToSearch); StringBuffer sb = new StringBuffer(); while(m.find()){ m.appendReplacement(sb,m.group().toUpperCase()); } m.appendTail(sb); stringToSearch = sb.toString(); sb.setLength(0); // Last letter upper case using regex Pattern LastLetterPtn = Pattern.compile("([az]{1}\\b)+"); m = LastLetterPtn.matcher(stringToSearch); while(m.find()){ m.appendReplacement(sb,m.group().toUpperCase()); } m.appendTail(sb); System.out.println(sb.toString()); output: ThiS StrinG IS NeedeD TO BE FirsT AnD LasT LetteR UppercaseD FoR EacH WorD 
0
source

Source: https://habr.com/ru/post/1264352/


All Articles