0

我在尝试转换为 CharSequence [] 时不断收到此错误。

字符串类型的 toArray(CharSequence[]) 方法未定义

这是我的代码

              CharSequence[] cs = abbrev.toArray(new CharSequence[abbrev.length()]);

缩写只是将句子分成第一个字符(Hello World --> HW)

       String[] result = matches.toString().split("\\s+");
        // The string we'll create

        String abbrev = "";

           // Loop over the results from the string splitting
           for (int i = 0; i < result.length; i++){

               // Grab the first character of this entry
               char c = result[i].charAt(0);

               // If its a number, add the whole number
               if (c >= '0' && c <= '9'){
                   abbrev += result[i];
               }

               // If its not a number, just append the character
               else{
                   abbrev += c;
               }
           }

有任何想法吗?

谢谢

4

2 回答 2

0

因为abbrev是一个字符串,所以不能调用toArray它。将其转换为CharSequence第一个。例如,

CharSequence[] abbrevCs = abbrev; // it's converted automatically
CharSequence[] cs = abbrevCs.toArray(new CharSequence[abbrev.length()]);
于 2015-02-24T19:20:25.330 回答
0

您不必将 a 转换String为 aCharSequence因为 aString a CharSequence。您可以将一个String对象分配给一个CharSequence对象,并且转换是隐式的。有关更多详细信息,请参见此处:如何将字符串转换为 CharSequence?

于 2015-02-24T19:21:02.763 回答