Listchar.toString does not convert correctly

1

I have a list of char , which I want to save in a string with the ToString method.

The problem is that it saves me anything:

For more information, the method, what it does is to receive the string changed from a TextBox , evaluate if there is a non-integer character inside the string , and if it exists, remove it (finding the character, creating a list with the characters of the string received, removing from the list the character that matches the one found, which is not integer, then returning the list to string and showing it in TextBox ).

Why do you keep the string wrong?

    
asked by Facundo Yuffrida 01.01.2018 в 15:46
source

4 answers

2

The ToString method is a legacy method of the Object class that returns a string representing the object. By default this method returns the full name of the object type.

Some types overwrite this method so that it has a different behavior, but this is not the case with generic lists. Therefore when calling the method ToString the result is the name of the object type.

If you want to convert a list of characters to string you can use the Concat method:

string recortado = string.Concat(recortar);
    
answered by 01.01.2018 / 16:00
source
0

I think the way you approach the problem is not the most efficient. I recommend that you simply validate if the typed character is a number or not, and if it is not, do not add it to perfectosTextbox (in the KeyPress event). For example:

public static void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsNumber(e.KeyChar) && e.KeyChar != (char)Keys.Back)
        e.Handled = true;
}

This would make textBox1 just accept numbers, which in the end is what you're looking for.

    
answered by 01.01.2018 в 22:20
0

You can use regular expressions in the following way to eliminate numeric characters

System.Text.RegularExpressions.Regex.Replace(perfectosTextBox.Text, @"[\d-]", string.Empty, RegexOptions.None);

And to eliminate non-numerics

System.Text.RegularExpressions.Regex.Replace(perfectosTextBox.Text, @"[^0-9]", string.Empty, RegexOptions.None);
    
answered by 02.01.2018 в 15:36
0

trim.Tostring returns the literal of type "System.Collections.Generic.List'1 [System.Char]"

to convert the char to string collection

 recortar =new String(recortar.ToArray())
    
answered by 10.01.2018 в 22:56