Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
1
Question by altusi · May 16, 2016 at 07:26 PM · string.split

Split string: keep split chars separators C#

Hello,

I was wondering how to keep the split chars separators when you split a string.

For example:

 UserString = "This is my first sentence. This is my second sentence!";
 
 SentencesList = UserString.Split(new string[] { ".", "!", "?" }, System.StringSplitOptions.None);

My result will be: SentcenceList[0] = "This is my first sentence" SentcenceList[1] = "This is my second sentence"

How I can keep the the split chars separators? (in this context "." and "!")

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Ahndrakhul · May 17, 2016 at 11:10 PM

I'm kind of surprised that this isn't an option with string.split. I'm not very good with regex, but I know that you can do this sort of thing with Regex.Split. You would have to include System.Text.RegularExpressions, and then do something like this:

 string userString = "This is my first sentence. This is my second sentence!";
 string pattern = "(?<=[.!?])";
 string[] sentencesList = Regex.Split(userString, pattern);

In this case, every sentence beyond the first one will have at least one preceding space that you will want to remove. There is probably a way to do this with Regex, but I don't know how. You could also go through the resulting sentenceList array and call trim() on each string.

 for(int i = 0; i < sentenceList.Length; i++)
 {
     sentenceList[i] = sentenceList[i].Trim();
 }

Performance-wise, this is not very good, but if you aren't doing it often, it probably doesn't matter. If you don't want to use Regex, you can use a custom split method. Maybe something like this.

 public  string[] SplitString(string stringToSplit, char[] delimiters, bool includeDelimiter, bool trimEnds)
         {
             int index = -1;
             List<string> stringList = new List<string>();
             StringBuilder sBuilder = new StringBuilder();
 
             while(++index < stringToSplit.Length)
             {
                 sBuilder.Append(stringToSplit[index]);
 
                 foreach(char c in delimiters)
                 {
                     if(stringToSplit[index] == c)
                     {
                         if(!includeDelimiter)
                         {
                             sBuilder.Remove(sBuilder.Length -1, 1);
                         }
                         if(trimEnds)
                         {
                             stringList.Add(sBuilder.ToString().Trim());
                         }
                         else
                         {
                             stringList.Add(sBuilder.ToString());
                         }
                         sBuilder.Clear();
                         break;
                     }
                 }
             }
             return stringList.ToArray();
         }
     }

This isn't very pretty, but it will probably work for what you want to do. Hopefully someone else can respond with a better way of doing this.

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 altusi · May 19, 2016 at 01:18 PM 0
Share

Hello.

Yes, I'm amazed too there is not a direct option in the split. But your example is fitting well with my problem. The space is not a problem in my case. And you explain it perfectly! Thanks to take so much of your time to give me a good solution and an alternative one! I didn't expect so much!

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

54 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 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

How to move player using Coroutine? 1 Answer

How to split a string into multiple floats? 2 Answers

problem comparing Strings using compareTo() as it never evaluates to 0 even when the two strings are equal 0 Answers

How to get rid of empty characters from the string? 1 Answer

imported text contains extra character 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