locked
How can i remove from a string[] empty indexs ? RRS feed

  • Question

  • In the array i have 121 words.

    But in places [0] and the last place i see ""

    I can manualy remove them but if i want to check for "" and remove them automatic from any places they are show in the string[] how can i do it ?

    var words = Regex.Split(textBox1.Text, "[^a-zA-Z]+");

    Im using this line to get all the words from a textFile but in the first index and the last index i see ""

    ( This Regex.Split to get all words is a good way ? )

    Thursday, July 4, 2013 12:43 PM

Answers

  • Why not just use a simple string split like this?

    var words = textBox1.Text.Split(new char[] { ' ', '"', ',' }, StringSplitOptions.RemoveEmptyEntries);
    Add any word separator chars you want to the char[].


    Dan Randolph - My Code Samples List

    • Marked as answer by Chocolade1972 Thursday, July 4, 2013 6:12 PM
    Thursday, July 4, 2013 5:21 PM

All replies

  • Does

    var words = textBox1.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries);

    do what you want?


    Paul Linton

    Thursday, July 4, 2013 12:50 PM
  • An option using linq would be:

     var notEmptyWords = words.Where(s => !string.IsNullOrEmpty(s)).ToList();

    (but I would go with Paul's option of stripping out the empty strings from the start).
    Thursday, July 4, 2013 12:53 PM
  • Does

    var words = textBox1.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries);

    do what you want?


    Paul Linton

    Tried im getting some errors:

    Error 1 Argument 1: cannot convert from 'string' to 'char'

    Error 2 The best overloaded method match for 'string.Split(params char[])' has some invalid arguments

    Error 3 Argument 2: cannot convert from 'System.StringSplitOptions' to 'char'

    All the errors on this line.

    Tried to change from ' ' to " " but it didn't do anything.

    Thursday, July 4, 2013 1:21 PM
  • Hi, You can use either of the two options given below.

    string newstring = oldstring.Replace('"', "");

    or

    string newstring = oldstring.Replace('"', string.Empty);

    • Edited by Diptimayee Thursday, July 4, 2013 1:32 PM proper alignment
    Thursday, July 4, 2013 1:31 PM
  • check this link, you can split the string, store the results to array, and then use the array based on your need. http://stackoverflow.com/questions/635751/getting-rid-of-null-empty-string-values-in-a-c-sharp-array

    string[] _results = ",r,t,y,u,".Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

    regards

    joon


    • Edited by Joon84 Thursday, July 4, 2013 2:07 PM spelling mistake
    Thursday, July 4, 2013 2:01 PM
  • check this link, you can split the string, store the results to array, and then use the array based on your need. http://stackoverflow.com/questions/635751/getting-rid-of-null-empty-string-values-in-a-c-sharp-array

    string[] _results = ",r,t,y,u,".Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

    regards

    joon


    Still im getting the same errors as above.

    I did :

    var words = textBox1.Text.Split(' ', StringSplitOptions.RemoveEmptyEntries);

    Thursday, July 4, 2013 4:41 PM
  • Why not just use a simple string split like this?

    var words = textBox1.Text.Split(new char[] { ' ', '"', ',' }, StringSplitOptions.RemoveEmptyEntries);
    Add any word separator chars you want to the char[].


    Dan Randolph - My Code Samples List

    • Marked as answer by Chocolade1972 Thursday, July 4, 2013 6:12 PM
    Thursday, July 4, 2013 5:21 PM