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

时间:2022-11-29 10:55:32

I'm using this for loop to go through my array's individual characters from a string.

我正在使用这个for循环来从字符串中查看我的数组的单个字符。

for(int i=0;i< array;i++){

I need to print the characters one by one using g.drawString. Therefor I need the character at position [i] in the array to be turned into a string. How do I do this?

我需要使用g.drawString逐个打印字符。因此,我需要将数组中位置[i]处的字符转换为字符串。我该怎么做呢?

6 个解决方案

#1


2  

You can use :

您可以使用 :

String.valueOf(yourChar)

So your loop would be:

所以你的循环将是:

for(int i=0;i< array;i++){
 g.drawString(String.valueOf(yourString.charAt(i));
}

#2


1  

It's just simple: "" + array.charAt(i)

这很简单:“”+ array.charAt(i)

#3


0  

Just do this:

这样做:

char[] array;

g.drawString(String.valueOf(array[i]));

#4


0  

This is the best practice way:

这是最佳实践方式:

char[] array;
for (char c : array) {
    String s = String.valueOf(c);
    // do something with s
}

#5


0  

Something like this

像这样的东西

char[] chars = {'a','b','c'};
    String[] strings = new String[chars.length];
    for (int i = 0; i < chars.length; i++) {
        strings[i] = String.valueOf(chars[i]);
    }

#6


0  

Usually, the best way to do this is if your source is a String:

通常,执行此操作的最佳方法是源是String:

str.substring(i, i+1); // If you have a string.

because it avoids unnecessary character buffer copies. Some versions of Java (apparently JDK7u5 and earlier) can then reuse the existing character buffer and this way avoid an extra object creation. (See: this announcement of the change, indicating that this holds for JDK7u5 and before)

因为它避免了不必要的字符缓冲区副本。某些版本的Java(显然是JDK7u5及更早版本)可以重用现有的字符缓冲区,这样可以避免额外的对象创建。 (参见:此更改的公告,表明这适用于JDK7u5及之前)

There are two quite obvious alternatives (this one also works if you have your own char[] as data source):

有两个非常明显的替代方案(如果你有自己的char []作为数据源,这个也有效):

String.valueOf(str.charAt(i)); // If you have a String
String.valueOf(chararray[i]); // If you have a char[]

These will actually create two objects: one String and one char[1].

这些实际上会创建两个对象:一个String和一个char [1]。

And then there is the ugly hackling:

然后是丑陋的ha:声:

"" + str.charAt(i); // If you do do not care about doing things right.

which usually causes the creation of a string buffer, with a single append operation, and the conversion to a string. If you use this code in a hot loop, it may really hurt your applications performance. While the code looks really simple, it supposedly translates to:

这通常会导致创建一个字符串缓冲区,使用单个追加操作,并转换为字符串。如果您在热循环中使用此代码,则可能会严重损害您的应用程序性能。虽然代码看起来非常简单,但据推测它可以转换为:

StringBuilder temp = new StringBuiler("");
temp.append(str.chatAt(i));
temp.toString();

And this overhead is actually quite useless, given that there are two clean solutions available in the API: converting characters to Strings, and constructing substrings.

考虑到API中有两种干净的解决方案:将字符转换为字符串,并构造子字符串,这种开销实际上是无用的。

#1


2  

You can use :

您可以使用 :

String.valueOf(yourChar)

So your loop would be:

所以你的循环将是:

for(int i=0;i< array;i++){
 g.drawString(String.valueOf(yourString.charAt(i));
}

#2


1  

It's just simple: "" + array.charAt(i)

这很简单:“”+ array.charAt(i)

#3


0  

Just do this:

这样做:

char[] array;

g.drawString(String.valueOf(array[i]));

#4


0  

This is the best practice way:

这是最佳实践方式:

char[] array;
for (char c : array) {
    String s = String.valueOf(c);
    // do something with s
}

#5


0  

Something like this

像这样的东西

char[] chars = {'a','b','c'};
    String[] strings = new String[chars.length];
    for (int i = 0; i < chars.length; i++) {
        strings[i] = String.valueOf(chars[i]);
    }

#6


0  

Usually, the best way to do this is if your source is a String:

通常,执行此操作的最佳方法是源是String:

str.substring(i, i+1); // If you have a string.

because it avoids unnecessary character buffer copies. Some versions of Java (apparently JDK7u5 and earlier) can then reuse the existing character buffer and this way avoid an extra object creation. (See: this announcement of the change, indicating that this holds for JDK7u5 and before)

因为它避免了不必要的字符缓冲区副本。某些版本的Java(显然是JDK7u5及更早版本)可以重用现有的字符缓冲区,这样可以避免额外的对象创建。 (参见:此更改的公告,表明这适用于JDK7u5及之前)

There are two quite obvious alternatives (this one also works if you have your own char[] as data source):

有两个非常明显的替代方案(如果你有自己的char []作为数据源,这个也有效):

String.valueOf(str.charAt(i)); // If you have a String
String.valueOf(chararray[i]); // If you have a char[]

These will actually create two objects: one String and one char[1].

这些实际上会创建两个对象:一个String和一个char [1]。

And then there is the ugly hackling:

然后是丑陋的ha:声:

"" + str.charAt(i); // If you do do not care about doing things right.

which usually causes the creation of a string buffer, with a single append operation, and the conversion to a string. If you use this code in a hot loop, it may really hurt your applications performance. While the code looks really simple, it supposedly translates to:

这通常会导致创建一个字符串缓冲区,使用单个追加操作,并转换为字符串。如果您在热循环中使用此代码,则可能会严重损害您的应用程序性能。虽然代码看起来非常简单,但据推测它可以转换为:

StringBuilder temp = new StringBuiler("");
temp.append(str.chatAt(i));
temp.toString();

And this overhead is actually quite useless, given that there are two clean solutions available in the API: converting characters to Strings, and constructing substrings.

考虑到API中有两种干净的解决方案:将字符转换为字符串,并构造子字符串,这种开销实际上是无用的。