0

I want to split a string and keep text within quotes so I can properly parse arguments at a command line. However, Regex and Linq are not supported on COSMOS.

I need some way to split a string like the following:

This is "a very" important string "doing string things"

Into an array with the following contents:

{"This", "is", "a very", "important", "string", "doing string things"}

The closest I could find that could fix my problem is this answer. However, I can't figure out how to convert this to an array because I don't know how to use IEnumerals.

  • I'd suggest using a `List` rather than an array then you'd just add to the list where ever it does a yield and return the list at the end. – juharr Apr 09 '20 at 00:41
  • 1
    If you `.Split('"');`, you already have the parts separated. Now you only need the indexes of the double quotes inside the original string (what Regex.Matches would have already returned as part of the Match). – Jimi Apr 09 '20 at 00:46

1 Answers1

1

I firstly split " then trim and finally with mode split which is not in " " then add them to list string

public string[] toArr(string word){
    List<string> result=new List<string>();
    var split1=word.Split('"');
    for(int i=0 ; i<split1.Length ;i++){
        split1[i]=split1[i].Trim();
    }
    for(int i=0;i<split1.Length;i++){
        if(i%2==0){
            var split2=split1[i].Split(' ');
            foreach(var el in split2){
                result.Add(el);
            }
        }
        else{
            result.Add(split1[i]);
        }

    }
    string[] arr=new string[result.Count];
    for(int i=0;i<result.Count;i++){
        arr[i]=result[i];
    }
    return arr;

}
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54