如何将char数组转换回字符串 [英] How to convert char array back to string

查看:63
本文介绍了如何将char数组转换回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个大写第一个单词字母的代码。但是,我找不到将char数组转换回String的方法:

There is a code which capitalize first word letter. However I wasn't able to find a method to convert char array back to String:

例如: hello world代码将其转换为 [ H, e, l, l, o,, W, o, r, l, d] 我想将其转换回 Hello World

For example: "hello world" code transforms it to ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"] I want to transform it back to "Hello World"

public class Solution
   {
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String s = reader.readLine();

        char[] chars = s.toCharArray();
        chars[0] = Character.toUpperCase(chars[0]);

        for (int i = 0; i < chars.length; i++){
            if (chars[i] == ' '){
                chars[i + 1] = Character.toUpperCase(chars[i + 1]);
            }
        }
        System.out.println(chars);
    }
}


推荐答案

String str = String.valueOf( chars );

String str = new String( chars );

这篇关于如何将char数组转换回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆