将字符串转换为String [] [英] Convert String to String[]

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

问题描述





我是WPF的初学者,我目前正在做这行代码,但是我收到了这个错误。



无法将字符串隐式转换为字符串[]



我想知道如何在数小时后搜索答案在Google。



提前致谢。



Hi,

I am a beginner in WPF and I am currently doing this line of code but I'm getting this error.

"Cannot Implicitly convert string to string[]"

I would like to know how to resolve this after hours searching the answers in Google.

Thanks in advance.

private List<string> GetWords(string text)
        {
            Dictionary<string, int> list = new Dictionary<string, int>();

            string[] words = text.Trim();

            for (int i = 0; i < words.Length; i++)
            {
                string word = words[i].ToString();
                word = word.ToUpper();    //setting the text to uppercase

                if (!list.ContainsKey(word))
                    list.Add(word, 1);

                else
                {
                    int count = list[word];
                    list[word] = count + 1;
                }
            }

            var items = from pair in list   //listing the textblocks according to ascending order
                        orderby pair.Value ascending
                        select pair;
            List<string> result = new List<string>();
            foreach (KeyValuePair<string, int> pair in items)
                result.Add(pair.Key);

            return result;
            
        }

推荐答案

这必须是 string [] words = text.Trim (); 右边的方法返回 string ,而不是 string []



看,你做错了什么。错误消息中可能不清楚的是什么。所有你需要做的,而不是搜索任何东西,看看 System.String 类的文档:

http://msdn.microsoft.com/en-us/library/system.string %28v = vs.110%29.aspx [ ^ ]。



现在,你显然根本不明白你在写什么以及为什么即使没有编译错误。你怎么可能写下一行?

This must be string[] words = text.Trim(); the method on right returns string, not string[].

Look, you are doing something wrong. What could be unclear in the error message. All you needed to do, instead of "searching" for anything, look at the documentation on System.String class:
http://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx[^].

Now, you apparently simply don't understand what you are writing and why even where there are no compilation errors. How could you possibly write the next line?
string word = words[i].ToString();



显然 words [i] 已经是一个字符串,为什么要调用 ToString()为它?



现在,不要问我如何解决它?我不知道你试图用这个修剪做什么。我只能猜测你试图将字符串拆分为字符串数组并希望调用拆分,而不是修剪 ...



我真的不知道如何帮助你。你显然不知道很多东西,但这不是你真正的问题;你可以快速学到很多东西。但是你的问题是你不是试图自己思考而是试图寻求一些帮助。也许这些话可以帮助你...



-SA


Isn't it obvious that words[i] is already a string, why calling ToString() for it?

Now, don't ask me "how to fix it?" I don't know what you tried to do with this Trim. I can only guess that you tried to split the string into array of string and wanted to call Split, not Trim

I don't really know how to help you. You apparently don't know many things, but this is not your real problem; you could learn a lot, fast. But your problem is that you are not trying to think by yourself and instead try to search for some help. Maybe these words can help you…

—SA


替换行

replace line
string[] words =text.Trim();



with


with

string[] words = text.Split(' ');


这篇关于将字符串转换为String []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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