Create ascii numeric String from string

Stempler :

I need to create a numeric String of the ascii values from a given String. My string only contains upercase A-Z chars.

At the moment I have:

String example = "STRING";
List<Integer> list = example.chars()
          .mapToObj(item -> (int) item)
          .collect(Collectors.toList());

Output: [83, 84, 82, 73, 78, 71]

Than I do:

String asciiString = list.stream()
            .map( n -> n.toString() )
            .collect( Collectors.joining( "" ) );

Output: 838482737871

Is there a more effective way to do it? Possibly with only one stream?

Hadi J :

Just use mapToObj and convert it to string.

 String result = example.chars()
            .mapToObj(String::valueOf)
            .collect(Collectors.joining());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116699&siteId=1