简体   繁体   English

Java:将字符转换为字符串

[英]Java: Converting a char to a string

I have just done this in eclipse:我刚刚在 eclipse 中做了这个:

String firstInput = removeSpaces(myIn.readLine());
String first = new String(firstInput.charAt(0));

However, eclipse complains that:但是,eclipse 抱怨说:

The constructor String(char) is undefined构造函数 String(char) 未定义

How do I convert a char to a string then??那么如何将 char 转换为字符串?

Thanks谢谢

EDIT编辑

I tried the substring method but it didn't work for some reason but gandalf's way works for me just fine!我尝试了 substring 方法,但由于某种原因它不起作用,但甘道夫的方法对我来说很好用! Very straightforward!非常直截了当!

Easiest way? 最简单的方法?

String x = 'c'+"";

or of course 当然

String.valueOf('c');

Instead of... 代替...

String first = new String(firstInput.charAt(0));

you could use... 你可以用......

String first = firstInput.substring(0,1);

substring(begin,end) gives you a segment of a string - in this case, 1 character. substring(begin,end)为您提供一个字符串段 - 在本例中为1个字符。

String x = String.valueOf('c');`

这是最直接的方式。

为什么不使用子串?

String first = firstInput.substring(0, 1);
String firstInput = removeSpaces(myIn.readLine());
String first = firstInput.substring(0,1);

This has an advantage that no new storage is allocated. 这具有不分配新存储的优点。

你可以这样做:

String s = Character.toString('k');

String.valueOf() to Convert Char to String String.valueOf() 将 Char 转换为 String

The String class has a static method valueOf() that is designed for this particular use case. String 类具有专为此特定用例设计的静态方法 valueOf()。 Here you can see it in action:在这里你可以看到它的实际效果:

public class CharToString {
public static void main(String[] args) {
    char givenChar = 'c';
    String result = String.valueOf(givenChar);

    //check result value string or char 
    System.out.println(result.equals("c")); 
    System.out.println(result.equals(givenChar));
  }
}

Output::输出::

true
false

Character.toString() to Convert Char to String Character.toString() 将字符转换为字符串

We can also use the built-in method of Character class to convert a character to a String.我们还可以使用 Character 类的内置方法将字符转换为 String。

The below example illustrates this:下面的例子说明了这一点:

public class CharToString {
public static void main(String[] args) {
    char givenChar = 'c';
    String result = Character.toString(givenChar);

    //check result value string or char
    System.out.println(result.equals("c"));
    System.out.println(result.equals(givenChar));
    }
 }

Output::输出::

true
false

String Concatenation to Convert Char to String字符串连接将字符转换为字符串

This method simply concatenates the given character with an empty string to convert it to a String.此方法只是将给定的字符与空字符串连接起来,以将其转换为字符串。

The below example illustrates this:下面的例子说明了这一点:

public class CharToString {
public static void main(String[] args) {
    char myChar = 'c';
    String charToString = myChar + "";

    //check result value string or char
    System.out.println(charToString.equals("c"));
    System.out.println(charToString.equals(myChar));
   }
}

Output::输出::

true
false

However, this is the least efficient method of all since the seemingly simple concatenation operation expands to new StringBuilder().append(x).append("").toString();然而,这是效率最低的方法,因为看似简单的连接操作扩展为 new StringBuilder().append(x).append("").toString(); which is more time consuming than the other methods we discussed.这比我们讨论的其他方法更耗时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM