Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by oliver-jones · Oct 06, 2014 at 04:05 PM · textsplit

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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]);
 
 
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anxo · Oct 06, 2014 at 04:28 PM 0
Share

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.

avatar image
0

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]));
             }
         }
     }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Oct 06, 2014 at 05:23 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges