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
0
Question by jsatterw · Jun 25, 2015 at 05:57 PM · c#stringparse

How to parse a string between certain characters?

Hello all,

I'm working on a script now that will read some code that a user has typed on a keyboard in-game and parse it so that I can send those codes to start different commands. The codes will be a single uppercase letter followed be a series of numbers. Many different codes can be entered on a single line. The code will look something like this: "G10M8X5Y5", and I'd like to get something like this out of it: "G10","M8","X5","Y5".

What parsing technique will I need to use in order to accomplish this?

Thanks!

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
2
Best Answer

Answer by Landern · Jun 25, 2015 at 06:00 PM

Could be done with Regex and matches, here is a super simple version that should be easy to follow.

             string b = "G393Z47E18724F111";
             StringBuilder sb = new StringBuilder();
             List<string> output = new List<string>();
 
             foreach( char c in b )
             {
                 if( char.IsNumber(c) )
                 {
                     // if it's a number, lets add it to the current code.
                     sb.Append(c);
                     continue;
                 }
 
                 if( char.IsLetter(c) && sb.Length == 0 )
                 {
                     sb.Append(c);
                     continue;
                 }
 
                 if( char.IsLetter(c) && sb.Length > 0 )
                 {
                     // we assume we have hit a new code since the builder has more then 0 characters in it and the next char coming up is a letter.
                     // Lets add our previous code to the output List of string, then clear the stringbuild for the next one and append for the next code.
                     output.Add(sb.ToString());
                     sb.Clear();
                     sb.Append(c);
                     continue;
                 }
             }
 
             // Grab the last one that wasn't captured in the foreach loop:
             output.Add(sb.ToString());


One will need to use System.Text for StringBuilder and System.Collections.Generic for the use of List. Plug this in where ever you want.

output is a List of string where each entry/element is a parsed value as you asked about.

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 jsatterw · Jun 25, 2015 at 06:33 PM 0
Share

This is exactly what I was looking for. Thank you so much!

avatar image
1

Answer by brunopava · Jun 25, 2015 at 06:02 PM

You can use Split.

ex:

 string allCodes = "G10,M8,X5,Y5";
 
 // this splits the string using comma as a parameter.
 // every index on the array will now have a code in it.
 // note that this method requires you to use a single ' instead of "
 string[] codes  = allCodes.Split(',');
 
 // prints on the screen all the codes
 foreach(string current in codes)
 {
    Debug.Log(current);
 }
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 fafase · Jun 25, 2015 at 06:08 PM 2
Share

That would be too simple, notice that the original string does not contain any comas. Regex is a simple but ugly solution. Iteration through the string to find match is a more complex but more readable solution.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

C# Variable 4 Answers

Can i give GetComponent a variabel instead of a scriptname? 1 Answer

Problem recognizing declaration of array of strings 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