Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 拆分后获取不正确的值_C# - Fatal编程技术网

C# 拆分后获取不正确的值

C# 拆分后获取不正确的值,c#,C#,我想用string[]words中存储的ID向某个用户发送消息。我已尝试将其输出到lable\u name.Text。ID显示正确,但下一个方法中的string user\u ID似乎不正确。 我试着为lable\u name.Text=“65896237”更改lable\u name.Text=words[2],一切都很好 我刚刚查看了Debugger,它说,lable\u name.Text=“65896237\r\n” 这是原因吗?我怎样才能得到明确的价值 private void l

我想用
string[]words
中存储的ID向某个用户发送消息。我已尝试将其输出到
lable\u name.Text
。ID显示正确,但下一个方法中的
string user\u ID
似乎不正确。 我试着为
lable\u name.Text=“65896237”
更改
lable\u name.Text=words[2]
,一切都很好

我刚刚查看了Debugger,它说,
lable\u name.Text=“65896237\r\n”
这是原因吗?我怎样才能得到明确的价值

  private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            int count = listBox1.Items.Count -1;
            for (int counter = count; counter >= 0; counter--)
            {
                if (listBox1.GetSelected(counter))
                { 
                     string[] words= listBox1.Items[counter].ToString().Split(' ');
                     lable_name.Text = words[2];
                }
            }
        }

        private void send_Click(object sender, EventArgs e)
        {
            if (radioMessage.Checked==true)
            {
            string user_id = lable_name.Text;
            string message = textBox1.Text;
            string url = "https://api.vk.com/method/messages.send?user_id="+user_id+"&message="+message+"&v=5.31&access_token=...";
            WebClient client = new WebClient();
            string json = client.DownloadString(url);
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            RootObject response = (RootObject)json_serializer.Deserialize(json, typeof(RootObject));
            MessageBox.Show("This is Message");
            }
            else
            {...
我刚刚查看了Debugger,它说,这个标签是'u name.Text=“65896237\r\n”这是原因吗?我怎样才能得到明确的价值

您可以从如下变量中选择空格、制表符、回车符和换行符

string trimmed = lable_name.Text.Trim();

您可以在拆分之前或之后删除
/n
/r

int count = listBox1.Items.Count -1;
for (int counter = count; counter >= 0; counter--)
{
    if (listBox1.GetSelected(counter))
    { 
        string[] words= listBox1.Items[counter].ToString().Replace("\r\n", string.Empty).Split(' ');
        lable_name.Text = words[2];
    }
}

最简单的解决方案可能是一个简单的
lable\u name.Text=words[2].trim()
,但您可能希望了解从何处获取换行符。