gpt4 book ai didi

java - 从对象获取字符串,该对象是一个字符数组(不是对象[])

转载 作者:行者123 更新时间:2023-12-01 18:04:29 25 4
gpt4 key购买 nike

这不是重复的,请阅读问题并理解我有一个对象(不是 char[],不是对象[],但是>一个对象)

public String getStringFromObject(Object o){
// What do I do here to get a String? o is actually a char[]
}

我有一个代表字符数组的 java 对象:

我将一个对象信用传递到一个方法

L.info("Credentials component type is {} and is array -> {}", 
cred.getClass().getComponentType(), cred.getClass().isArray());

这将打印: token 的凭据为:[s, d, f],类型为 [C

如何将其放入字符串中?我不知道如何将对象转换为任何类型的 [] 数组。如果从 Object[] 开始就不会出现问题。

最佳答案

如果您有一个 char[] 并且希望将其转换为 String,只需使用 constructor

public String(char[] value)

来自javadoc :

Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

<小时/>

注意

如果您有一个对象,它是一个char[],这就是您所拥有的:

char[] x = ....;
Object objectIsACharArray = x;
String s = new String((char[]) x);

如果对象不是char[],而是表示或保存字符数组的东西,则需要自定义代码将其转换为字符串.

关于java - 从对象获取字符串,该对象是一个字符数组(不是对象[]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37794732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com