Home > Enterprise >  breaking string into array
breaking string into array

Time:10-10

so im reading this from a terminal:

IN-USE  SSID                        MODE   CHAN  RATE        SIGNAL  BARS  SECURITY
*       example                     Infra  1     130 Mbit/s  71      ▂▄▆_  WPA2
        example2                    Infra  10    130 Mbit/s  65      ▂▄▆_  WPA2

ive tried several methods to breaking it apart to array so that in use will be at[0] and SSID at[1] and so on but what im using is breaking apart the string by number of spaces between 2 chars, ive seen that the minimum distance is 2 spaces but when i will read an SSID that have two spaces between words such as this: im here it will cause problem in the breaking part, ive tried Split("\t") and didnt work any suggestions?

CodePudding user response:

Assuming that the columns are fixed width you can split a single line into an array of strings using this method:

string[] ParseLine(string input) =>
    new[] {
        input[..6].TrimEnd(),
        input[8..34].TrimEnd(),
        input[36..41].TrimEnd(),
        input[43..47].TrimEnd(),
        input[49..59].TrimEnd(),
        input[61..67].TrimEnd(),
        input[69..73].TrimEnd(),
        input[75..].TrimEnd()
    };

To split the entire text you need to split it into lines and then parse each line:

var lines = text.Split('\n');
var result = lines.Select(ParseLine);

The value of result is now a sequence of arrays containing strings. You can enumerate the elements using foreach or convert it to an array using ToArray().

CodePudding user response:

The fixed width columns usually change their width accordingly when some properties became bigger. This solution converts string to c# List that can be used for any reporst or intelligence. So try this

var headerEndIndex = origString.IndexOf("SECURITY")   "SECURITY".Length   2;
var header = origString.Substring(0, headerEndIndex).Replace("\n", "").Replace("\r", ""); ;

var dataStringArray=origString.Substring(headerEndIndex).Split("\n");
    
    
for (var i=0; i< dataStringArray.Length; i  ) 
           dataStringArray[i] = dataStringArray[i].Replace("\n", "").Replace("\r", "");
           
     var ssidIndex= header.IndexOf("SSID");
     var modeIndex= header.IndexOf("MODE");
     var chanIndex= header.IndexOf("CHAN");   
     var rateIndex= header.IndexOf("RATE");
     var signalIndex= header.IndexOf("SIGNAL");
     var barsIndex= header.IndexOf("BARS");   
     var securityIndex= header.IndexOf("SECURITY");   
     
      List<SignalInfo> list= new List<SignalInfo>();
   
      for (var i=0; i< dataStringArray.Length; i  ) 
      {
        var item=new SignalInfo();
        item.InUse=    dataStringArray[i][0] =='*';
        item.Ssid=dataStringArray[i][ssidIndex..modeIndex].TrimEnd();
        item.Mode=dataStringArray[i][modeIndex..chanIndex].TrimEnd();
        item.Chan= Convert.ToInt32(dataStringArray[i][chanIndex..rateIndex]);
        item.Rate= dataStringArray[i][rateIndex..signalIndex].TrimEnd();
        item.Signal= Convert.ToInt32(dataStringArray[i][signalIndex..barsIndex]);
        item.Bars= dataStringArray[i][barsIndex.. securityIndex].TrimEnd();
        item.Security= dataStringArray[i].Substring(securityIndex).TrimEnd();
        list.Add(item);
      };

output in jason format

[
  {
    "InUse": true,
    "Ssid": "example",
    "Mode": "Infra",
    "Chan": 1,
    "Rate": "130 Mbit/s",
    "Signal": 71,
    "Bars": "\u2582\u2584\u2586_",
    "Security": "WPA2"
  },
  {
    "InUse": false,
    "Ssid": "example2",
    "Mode": "Infra",
    "Chan": 10,
    "Rate": "130 Mbit/s",
    "Signal": 65,
    "Bars": "\u2582\u2584\u2586_",
    "Security": "WPA2"
  }
]

class

public class SignalInfo
{
public bool InUse    {get; set;}
public string Ssid {get; set;}
public string Mode { get; set; }
public int Chan { get; set; }
public string Rate { get; set; }
public int Signal { get; set; }
public string Bars { get; set; }
public string Security { get; set; }
}
  • Related