Java转换:char [] array - >串 [英] Java Converting: char[] array --> String

查看:115
本文介绍了Java转换:char [] array - >串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符数组转换为字符串?


我有这个代码

  Console c = System.console(); 
if(c == null){
System.err.println(No console。);
System.exit(1);
}
char [] password = c.readPassword(输入您的密码:);

我需要将其转换为String,才能验证

  if(stringPassword ==Password){
System.out.println(Valid);
}

任何人都可以帮我吗?





$ b b

  char [] password = c.readPassword(输入您的密码:); 
String stringPassword = new String(password);

比较时,不要使用 == ,使用`.equals():

  if(stringPassword.equals(Password)){


How do you convert a character array to a String?
I have this code

Console c = System.console();
if (c == null) {
    System.err.println("No console.");
    System.exit(1);
}
char [] password = c.readPassword("Enter your password: ");

I need to convert that to a String so I can verify

if(stringPassword == "Password"){
    System.out.println("Valid");
}

Can anyone help me with this?

解决方案

Use the String(char[]) constructor.

char [] password = c.readPassword("Enter your password: ");
String stringPassword = new String(password);

And when you compare, don't use ==, use `.equals():

if(stringPassword.equals("Password")){

这篇关于Java转换:char [] array - >串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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