Search code examples
c#cosmos

How can I split a string but keep text within quotes together without Regex or Linq


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.


Solution

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