Splitting Text

Hello,

I have a text document that contains a load of ‘IP Scanned Results’, which basically looks like this:

Name: DEVICE_1, Device Type: PHONE_1, IP: 192.168.1.54, MAC: 00:00:00:00:00:00, Last Seen: 06-10-2014 14:23:12
Name: DEVICE_2, Device Type: PHONE_2, IP: 192.168.1.54, MAC: 00:00:00:00:00:00, Last Seen: 06-10-2014 14:23:12
Name: DEVICE_3, Device Type: PHONE_3, IP: 192.168.1.54, MAC: 00:00:00:00:00:00, Last Seen: 06-10-2014 14:23:12

This text file is dynamic (the text in Caps are the dynamic keywords). How can I separate them out into arrays such as:

var Name : String[];
var Type : String[];
var IP : String[];
var MAC : String[];
var LastSeen : String[];

I was messing around with the ‘Split’, like this:

Split("Name:"[0]);

But all that did was:

Element 0 : 
Element 1 : ame: DEVICE_2, Device Type: PHO
Element 2 : E_2, IP: 192.168.1.54, MAC: 00:00:00:00:00:00, Last Seen: 06-10-2014 14:23:12

Which makes no sense. Could someone point me in the right direction?

You where in the right direction, you are just not using split correctly. Check out these docs, it looks for a character to seperate the strings by. So you would do something like this

string unformattedString = Name: DEVICE_1, Device Type: PHONE_1, IP: 192.168.1.54, MAC: 00:00:00:00:00:00, Last Seen: 06-10-2014 14:23:12;

string[] splitString = unformattedString.Split(new Char[] {':',',',':'});

List<string> Names = new List<string>();
Names.Add(splitString[1]);

List<string> Type = new List<string>();
Type.Add(splitString[3]);

you’ll be better off looking at regular expressions. they look like voodoo to the uninitiated but they’re actually quite powerful when used to do this sort of thing…

EDIT: like this! it’s far from perfect but shows you the sort of thing that you should do…

using UnityEngine;
using System.Text.RegularExpressions;

public class RegExTest : MonoBehaviour
{
    private const string InputText = "Name: DEVICE_1, Device Type: PHONE_1, IP: 192.168.1.54, MAC: 00:00:00:00:00:00, Last Seen: 06-10-2014 14:23:12

Name: DEVICE_2, Device Type: PHONE_2, IP: 192.168.1.54, MAC: 00:00:00:00:00:00, Last Seen: 06-10-2014 14:23:12
Name: DEVICE_3, Device Type: PHONE_3, IP: 192.168.1.54, MAC: 00:00:00:00:00:00, Last Seen: 06-10-2014 14:23:12";
private const string RegExInput = @“Name: (\w*), Device Type: (\w*), IP: ([0-9.]), MAC: ([0-9a-fA-F:]), Last Seen: ([0-9-: ]*)”;

    private void Awake()
    {
        var regEx = new Regex(RegExInput, RegexOptions.IgnoreCase);
        var inputArray = InputText.Split('

');

        foreach (var myLine in inputArray)
        {
            var match = regEx.Match(myLine);
            if (match.Success)
            {
                var groups = match.Groups;
                Debug.Log(string.Format("name='{0}',type='{1}',IP='{2}',MAC='{3}',Last seen='{4}'", groups[1], groups[2], groups[3], groups[4], groups[5]));
            }
        }
    }
}