Search code examples
c#uwparcgisprojectionesri

Trimming a long text to get what I want using substring


I have a program that was created using c# in UWP and we have a map embedded in it using ESRI.ArcGISRuntime libraries.

I want to show the datum only and I have used substring to extract what I need but it always gives me to much.

This is the line of code I get:

PROJCS[“WGS_84_Web_Mercator_auxillary_Sphere”.DATUM[“GCS WGS 1984”.SPHEROID[“WGS 1984”.6378………..

I want to show only what is after DATUM and before SPHEROID, which is GCS WGS 1984.

This is what I have so far:

_currentProjection = _tileLayer.SpatialReference.WkText.Substring(_tileLayer.SpatialReference.WkText.LastIndexOf("DATUM") + 9, _tileLayer.SpatialReference.WkText.StartsWith.ToString("SPHEROID");

The first part of the substring is correct, but I cannot get the second part correct. Does anyone have an idea on how to do this?

Thank you


Solution

  • Plenty of ways but Regex would probably be my candidate of choice

    var code = @"PROJCS[""WGS_84_Web_Mercator_auxillary_Sphere"".DATUM[""GCS WGS 1984"".SPHEROID[""WGS 1984"".637";
    
    var m = Regex.Match(code, @"DATUM\[""(?<d>[A-Z0-9 ]+)""\.SPHEROID"); 
    var r = m.Groups["d"].Value;
    

    If you want to do it as substring:

    var code = @"PROJCS[""WGS_84_Web_Mercator_auxillary_Sphere"".DATUM[""GCS WGS 1984"".SPHEROID[""WGS 1984"".637";
    
    var f = code.IndexOf("DATUM") + 7;
    var t = code.IndexOf("SPHEROID") - 2;
    var r = code[f..t]; //this is C#9 indexes and ranges; if you don't have it you can upgrade C# or use .Substring(f, t-f)
    

    Or Split:

    var code = @"PROJCS[""WGS_84_Web_Mercator_auxillary_Sphere"".DATUM[""GCS WGS 1984"".SPHEROID[""WGS 1984"".637";
    
    var bits = code.Split('"');
    var i = Array.FindIndex(bits, b => b.Contains("DATUM"));
    var r = bits[i+1];