title: Is it possible to parse string without GC allocations?
body: <p>I need to parse NMEA data from build-in GPS receiver in Android device. I'm receiving this data few times per second as a string. I'm curious is it possible to do this without garbage collection allocations or parsing strings is one of this moments where I can call <code>GC.Collect()</code> with a clear conscience?</p>
  
  <p>Exactly I need to call <code>string.split()</code> and some other methods like <code>Substring()</code> and result convert to with <code>double.Parse()</code>.</p>
  
  <p>I tried to do this with converting to <code>char[]</code> but in that way GC allocations was even bigger.</p>
  
  <p>GPS NMEA data have many sentences and I need to parse 2-3 of them every second. Below is example code to parse one of this sentences - $GPRMC </p>
  
  <p>Example sentences:</p>
  
  <blockquote>
    <p>$GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
    $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
    $GPGSA,A,3,32,27,03,193,29,23,19,16,21,31,14,,1.18,0.51,1.07*35</p>
  </blockquote>
  
  <pre><code>        // Divide the sentence into words
          string[] Words = sSentence.split(',');
          // Do we have enough values to describe our location?
          if (Words[3] != "" &amp; Words[4] != "" &amp;
              Words[5] != "" &amp; Words[6] != "")
          {
              // example 5230.5900,N
              // 52°30.5900\N
  
              // Yes. Extract latitude and longitude
  
  
              //Latitude decimal
  
              double DegreesLat = double.Parse(Words[3].Substring(0, 2), NmeaCultureInfo);
              string[] tempLat = Words[3].Substring(2).ToString ().Split ('.');
              double MinutesLat = double.Parse (tempLat[0], NmeaCultureInfo);
              string SecLat = "0";
              if (tempLat.Length &gt;= 2) {
                  SecLat = "0."+tempLat[1];
              }
              double SecondsLat = double.Parse (SecLat, NmeaCultureInfo)*60;
  
              double Latitude = (DegreesLat + (MinutesLat / 60) + (SecondsLat/3600));
  
  
              //Longitude decimal
  
              double DegreesLon = double.Parse(Words[5].Substring(0, 3), NmeaCultureInfo);
              string[] tempLon = Words[5].Substring(3).ToString ().Split ('.');
              double MinutesLon = double.Parse (tempLon[0], NmeaCultureInfo);
              string SecLon = "0";
              if (tempLon.Length &gt;= 2) {
              SecLon = "0."+tempLon[1];
              }
              double SecondsLon = double.Parse (SecLon, NmeaCultureInfo)*60;
  
              double Longitude = (DegreesLon + (MinutesLon / 60) + (SecondsLon/3600));
  
              // Notify the calling application of the change
              if (PositionReceived != null)
                  PositionReceived(Latitude, Longitude);
  </code></pre>