convert to capital letter using java 8 stream

java stream convert to lower case
convert list of string to lowercase java
java 8 capitalize first letter
java stream filter ignore case
java stream string to uppercase
java list of string to uppercase
convert string to uppercase in scala
java 8 stream filter json array

I have a List of strings like this "Taxi or bus driver". I need to convert first letter of each word to capital letter except the word "or" . Is there any easy way to achieve this using Java stream. I have tried with Pattern.compile.splitasstream technique, I could not concat all splitted tokens back to form the original string any help will be appreciated.If any body needs I can post my code here.

Here is my code:

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ConvertToCapitalUsingStreams {
    // collection holds all the words that are not to be capitalized
    private static final List<String> EXCLUSION_LIST = Arrays.asList(new String[]{"or"});

    public String convertToInitCase(final String data) {
        String[] words = data.split("\\s+");
        List<String> initUpperWords = Arrays.stream(words).map(word -> {
            //first make it lowercase
            return word.toLowerCase();
        }).map(word -> {
            //if word present in EXCLUSION_LIST return the words as is
            if (EXCLUSION_LIST.contains(word)) {
                return word;
            }

            //if the word not present in EXCLUSION_LIST, Change the case of
            //first letter of the word and return
            return Character.toUpperCase(word.charAt(0)) + word.substring(1);
        }).collect(Collectors.toList());

        // convert back the list of words into a single string
        String finalWord = String.join(" ", initUpperWords);

       return finalWord;
    }

    public static void main(String[] a) {
        System.out.println(new ConvertToCapitalUsingStreams().convertToInitCase("Taxi or bus driver"));

    }
}

Note: You may also want to look at this SO post about using apache commons-text library to do this job.

Java Stream How to, import java.util.Arrays; import java.util.List; //www . jav a 2 s . c o m public class Main { public static void main(final String[] args) { List<String> friends = Arrays. Using streams to manipulate a String. Unfortunately such scenario is badly supported by Java 8 Stream API. How do I make the first letter of a string

convert to capital letter using java 8 stream, convert list of string to lowercase java java 8 stream convert list to list uppercase stream convert stream to object java string to map java 8 java program to  Convert elements of a collection to upper case Java 7 vs Java 8 import static java.util.stream.Collectors.toList; import java.util.ArrayList; import java.util.List; public class Test { /** * Convert elements in a Collection to upper case using Java 7 * @param inList * @return */ public static List<String> convertToU

Split your string as words then convert first character to uppercase, then joining it to form original String:

String input = "Taxi or bus driver";
String output = Stream.of(input.split(" "))
                .map(w -> {
                     if (w.equals("or") || w.length() == 0) {
                         return w;
                     }
                     return w.substring(1) + Character.toUpperCase(w.charAt(0));
                })
                .collect(Collectors.joining(" "));

java-8-exercises/ToUpperCase.java at master · vfarcic/java-8 , Contribute to vfarcic/java-8-exercises development by creating an account on GitHub. /com/technologyconversations/java8exercises/streams/ToUpperCase.​java / Jump to .map(String::toUpperCase) // Convert each element to upper case. Java Stream How to - Transform List string to upper case Stream Collector; Stream Convert; Stream File Folder; Stream Reduce; Stream Sort; Stream Sum; Java

You may do it like so,

final List<String> firstLetterCapitalized = driverOptions.stream().map(line -> Stream.of(line.split(" "))
        .map(token -> token.equals("or") ? token : Character.toUpperCase(token.charAt(0)) + token.substring(1))
        .collect(Collectors.joining(" "))).collect(Collectors.toList());

Capitalize the first letter of each word in a string using Java (Example), Capitalize the first letter of each word in a string using Java capitalize the first character of each word of a string is by using Java 8 Stream API: String str = "​welcome to java"; // uppercase first letter of each word String output  Convert a List to a Comma-Separated String in Java 8 DZone 's Guide to This quick tutorial shows you how to use streams in Java to convert the contents of a list to a comma-separated string in Java 8.

Convert String to Title Case in Java, Java examples to convert a string to title case using Apache commons WordUtils class, String.split() method and Java 8 stream of character array. my target is to replace all small letters in args[0] to capital letters How to do that? There is a method on the String class called toUpperCase. You use it as . args[0].toUpperCase The second question – how to remove spaces in args[0] ? There is another method in String called trim. You use it as . args[0].trim()

How to filter String stream and map to lower case in Java? Perform , Perform sort as well. Java 8Object Oriented ProgrammingProgramming. Let's say the following is String array,  Java - String toUpperCase() Method. Description. This method has two variants. The first variant converts all of the characters in this String to upper case using the rules of the given Locale. This is equivalent to calling toUpperCase(Locale.getDefault()). The second variant takes locale as an argument to be used while converting into upper case.

Java 8 Method References Examples, Method references are probably most useful when used with the Stream API to "b", "c"); List<String> upperCase = letters.stream() .map(String::toUpperCase)  Welcome to the Convert Case Text Generator Tool. A very handy online text tool where you can change between lower case and upper case letters, where you can capitalize, uncapitalize, convert to mix case and transform your text.

Comments
  • This solution fitting for my requirement
  • oh damn, was about to hit submit with a pattern close to that :|
  • Hi #Holger the first part of the solution is not working as expected, it is not converting to upper case.
  • Or, with java9's Matcher.replaceAll, FIRST_WORD_CHAR_BUT_NOT_OR.matcher(phrase).replaceAll(mr->mr.group().toUpperCase())
  • @PhilipPuthenvila there was a typo in the regex, it looked for upper case letters instead of lowercase. I've fixed it. Thanks.
  • And your reason is?
  • your doing an unnecessary call to Stream#of that internally will call Arrays#stream
  • @Eugene I would prefer Arrays.stream for semantic reasons. String.split returns an array and Arrays.stream is the right idiom to stream over an array. In contrast, Stream.of(…) is a varargs method which happens to be able to accept arrays due to the way, varargs have been implemented (and for compatibility with pre-Java 5 code).