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 oliver-jones · May 18, 2015 at 03:00 PM · arraystringsplit

Split String into Arrays

Hello,

I have a string like this:

00-tweets/Username

I want to somehow split the string, and place different parts of the string into arrays. The format is as follows:

[Index] -tweets/ [Username]

I want the 'Index', which is '00' to be inserted into one array, and the 'Username' to be interested into another, whilst the '-tweets/' is completely ignored/removed. This is essentially what breaks the Index apart from the Username.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · May 18, 2015 at 03:10 PM

If you know that the tweets section cannot contain any forward slashes then you can use a function to get the string before the first hyphen (a.k.a the index) and then use another function to get the string after the forwards slash (a.k.a the username).

The actual functions (which I've taken from my library of helper functions):

 /// <summary>
 /// Returns the substring before the given value.
 /// If the value is not found in the string then the default value is returned.
 /// </summary>
 /// <param name="value">The value to search for</param>
 /// <param name="defaultValue">The value to return if the string was not found</param>
 /// <param name="caseSensitive">If true the search is case sensistive. Optional.</param>
 public static string SubstringBefore(this string str, string value, string defaultValue, bool caseSensitive = false)
 {
     // If the string is not null or empty
     if (!str.IsNullOrEmpty())
     {
         // The index of the value found
         int index = -1;
         if (caseSensitive)
         {
             // Case sensitive so don't do anything extra
             index = str.IndexOf(value);
         }
         else
         {
             // Case insensitive, so lowercase both first
             index = str.ToLowerInvariant().IndexOf(value.ToLowerInvariant());
         }

         // If we found something
         if (index >= 0)
         {
             // Return everything before that index
             return str.Substring(0, index);
         }
     }

     // Default to the default value
     return defaultValue;
 }

 /// <summary>
 /// Returns the substring after the given value.
 /// If the value is not found in the string then the default value is returned.
 /// </summary>
 /// <param name="value">The value to search for</param>
 /// <param name="defaultValue">The value to return if the string was not found</param>
 /// <param name="caseSensitive">If true the search is case sensistive. Optional.</param>
 public static string SubstringAfter(this string str, string value, string defaultValue, bool caseSensitive = false)
 {
     // If the string is not null or empty
     if (!str.IsNullOrEmpty())
     {
         // The index of the value found
         int index = -1;
         if (caseSensitive)
         {
             // Case sensitive so don't do anything extra
             index = str.IndexOf(value);
         }
         else
         {
             // Case insensitive, so lowercase both first
             index = str.ToLowerInvariant().IndexOf(value.ToLowerInvariant());
         }

         // If we found something
         if (index >= 0)
         {
             // Return everything after that index
             return str.Substring(index + value.Length);
         }
     }

     // Default to the default value
     return defaultValue;
 }
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

19 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

Related Questions

how can i split a word to individual letters..?? 2 Answers

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

convert string to list of lists 1 Answer

How to convert a string to int array in Unity C# 1 Answer

Change part of a string [Solved] 3 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