Parsing Strings

I have an array of strings I get from a file each element formated like below

name,place,age
or
string,string,int

how would I turn that into 3 separate values?

String objects have a method called Split. Try to use that.

I.e.

string s = "name,place,age";
string[] pieces = s.Split(','); // separate strings "name" "place" and "age"
1 Like

Thanks for holding my hand :stuck_out_tongue: for some reason when i tried split previously it wasnt showing up

urw

here are some more custom string extensions, might be useful for text parsing.
(place it in a static class, like StringExt or something, and use them on any string object.)

    // similar to String.StartsWith but outputs an optional remainder.
    static public bool StartsWith(this string s, string value, out string remainder) {
      remainder = s;
      if(s.StartsWith(value)) { s.SnipLeft(value.Length, out remainder); return true; }
      return false;
    }

    // similar to String.EndsWith but outputs an optional remainder.
    static public bool EndsWith(this string s, string value, out string remainder) {
      remainder = s;
      if(s.EndsWith(value)) { s.SnipRight(value.Length, out remainder); return true; }
      return false;
    }

    // does what Split does, but allows to ignore the results
    static public bool IsDelimitedBy(this string s, char c, out string[] split) {
      var list = new System.Collections.Generic.List<string>() { s };

      int i = 0;
      while(SplitOnce(list[i], c, out var l, out var r)) {
        list[i] = l;
        list.Add(r);
        i++;
      }

      var result = (list.Count > 1);
      split = result? list.ToArray() : new string[] { s };
      return result;
    }

    // snips the specified amount of chars on the string's left and outputs optional remainder
    static public string SnipLeft(this string s, int length, out string remainder) {
      remainder = s;
      if(length > s.Length) length = s.Length;
      if(length > 0) { remainder = s.Substring(length); return s.Substring(0, length); }
      return string.Empty;
    }

    // snips the specified amount of chars on the string's right and outputs optional remainder
    static public string SnipRight(this string s, int length, out string remainder) {
      remainder = s;
      if(length > s.Length) length = s.Length;
      if(length > 0) { remainder = s.Substring(0, s.Length - length); return s.Substring(s.Length - length); }
      return string.Empty;
    }

    // does what Split does, but only once (left to right), and then outputs the two strings
    static public bool SplitOnce(this string s, char c, out string left, out string right) {
      s.SplitOnce(s.IndexOf(c), out left, out right);
      return right.Length > 0;
    }

    // does what Split does, but only once at a specified index, and then outputs the two strings
    static public void SplitOnce(this string s, int index, out string left, out string right) {
      if(index < 0 || index >= s.Length - 1) {
        left = s;
        right = string.Empty;
      } else {
        left = s.Substring(0, index);
        right = s.Substring(index + 1);
      }
    }

    // combines StartsWith and EndsWith
    static public bool IsWrappedBy(this string s, string w) => s.StartsWith(w) && s.EndsWith(w);

    // combines StartsWith and EndsWith, and outputs a remainder
    static public bool IsWrappedBy(this string s, string w, out string midstr) {
      bool result = s.IsWrappedBy(w);
      midstr = result? s.Substring(w.Length, s.Length - 2 * w.Length) : s;
      return result;
    }

    // allows different StartsWith and EndsWith strings
    static public bool IsWrappedBy(this string s, string left, string right) => s.StartsWith(left) && s.EndsWith(right);

    // allows different StartsWith and EndsWith strings, and outputs a remainder
    static public bool IsWrappedBy(this string s, string left, string right, out string midstr) {
      bool result = s.IsWrappedBy(left, right);
      midstr = result? s.Substring(left.Length, s.Length - (left.Length + right.Length)) : s;
      return result;
    }