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 Krshojestic · Sep 03, 2012 at 01:02 PM · stringsplit

Skip empty strings after split

How to split string with a separator, without counting two consecutive separators as two words?

"ZB-AA-IY".Split("-"[0]).Length = 3, which is OK
"ZB--A-IY".Split("-"[0]).Length = 4, which is NOT OK
(I want consecutive separators("--", "---", "----" etc) to be considered as one separator, not as multiple separators with empty strings between them)

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Cripple · Sep 03, 2012 at 02:36 PM

Yep, it is part of System

Just change the line of code with this :

char[] separator = { '-' }; int i = "ZB--A-IY".Split(separator, System.StringSplitOptions.RemoveEmptyEntries).Length;

You can also add "using System;" instead.

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 Thewhiteaura · Jun 05, 2015 at 03:20 AM 0
Share

thank you very much! I forgot the StringSplitOptions was part of System. ^^,

avatar image
1

Answer by aldonaletto · Sep 03, 2012 at 01:11 PM

I've not tested this, but according to the Split .NET doc, you can use the option StringSplitOptions.RemoveEmptyEntries to solve this:

 var strings = "Some---String".Split("-"[0], StringSplitOptions.RemoveEmptyEntries);


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 entity476 · Feb 11, 2015 at 03:16 PM 0
Share

it doesn't seem to work for Unityscript though...

avatar image
1

Answer by Cripple · Sep 03, 2012 at 01:28 PM

It is easy, just add a split option as parameter of the split.

"ZB--A-IY".Split("-"[0], StringSplitOptions.RemoveEmptyEntries);

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

How to split alphanumeric string into array of strings based from their type : alpha or numeric ? 1 Answer

disperse string by letter 1 Answer

Splitting String only on a "space". Using my method --- FIXED 2 Answers

if statement not returning true 1 Answer

GameObject.name to array:String[] 0 Answers


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