- Home /
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?
Answer by Anxo · Oct 06, 2014 at 04:26 PM
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]);
That is C# by the way, sorry. but you get the concept. the Split, looks for a character not a word, so if you type in Name:, it split it by the character N, Thats why your first string is missing the N in Name and your second string is missing the N in Phone. But you can give it an array of characters as I did to use multiple characters to as separators.
Answer by gjf · Oct 06, 2014 at 04:35 PM
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\nName: 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\nName: 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('\n');
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]));
}
}
}
}
and if you felt like making it even more cryptic, throw in some LINQ!
foreach (var groups in from myLine in inputArray select regEx.$$anonymous$$atch(myLine) into match where match.Success select match.Groups)
{
Debug.Log(string.Format("name='{0}',type='{1}',IP='{2}',$$anonymous$$AC='{3}',Last seen='{4}'", groups[1], groups[2], groups[3], groups[4], groups[5]));
}
don't forget to add using System.Linq;
enjoy ;)
Your answer
Follow this Question
Related Questions
start counting number of line in input field after a specific line and read it 0 Answers
Best way to manage stats in a text file 2 Answers
Loading a text document without spaces 1 Answer
Random word from text file c# 3 Answers
How to save script component variables at runtime from IOS device? 1 Answer