Little Fox
Little Fox

Reputation: 1252

Getting incorrect value after Split

I want to send a message to some user with an ID stored in string[] words. I have tried to output it to lable_name.Text. ID displays correct, but it looks like string user_id in the next method doesn't see it correct. I have tried to change lable_name.Text = words[2] for lable_name.Text = "65896237" and all was good.

I just looked into Debuger - and it says, that lable_name.Text="65896237\r\n" is it the reason? and how can I get clear value?

  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
            {...

Upvotes: 1

Views: 67

Answers (2)

dlavila
dlavila

Reputation: 1212

you can remove the /n and /r before or after the split

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];
    }
}

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

I just looked into Debuger - and it says, that lable_name.Text="65896237\r\n" is it the reason? and how can I get clear value?

You can trim white space such as space, tab, carriage return and newline from a variable like

string trimmed = lable_name.Text.Trim();

Upvotes: 2

Related Questions