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 /
avatar image
1
Question by Marc_Keil · Mar 18, 2018 at 12:23 AM · stringregex

Get the upper Chars out of a string

Hey guys,

i need to get the upper characters out of a string. My method:

test = getUpperChars("ExampleText")

     public string getUpperChars(string str)
     {
         var result = String.Concat(Regex
             .Matches(str, "[A-Z]")
             .OfType<Match>()
             .Select(Match => Match.Value.ToString()));
         Debug.Log(result);
         return result;
     }

I do not understand why this message gets back instead of a string with the content ("ET"):

System.Linq.Enumerable+c__Iterator10`2[System.Text.RegularExpressions.Match,System.String] UnityEngine.Debug:Log(Object)

Can anyone tell me what I am doing wrong? Thanks for the answers!

Comment
Add comment · Show 1
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 ShadyProductions · Mar 18, 2018 at 12:45 AM 0
Share

Works fine for me in console

1 Reply

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

Answer by Bunny83 · Mar 18, 2018 at 12:51 AM

Well, depending on your Unity version and your used scripting backend Untiy will usually use Mono. The Mono version that Unity uses doesn't have this Concat overload. It only has Concat versions that take one or multiple "object" parameters. Of course Concat would simply call ToString on each parameter you pass in. Since the only thing you pass in is an IEnumerable instance (which is created from the Select method), that ToString will just return the type name of that IEnumerable which is what you see here.


You either need to use one of the Aggregate extensions to create a string or convert the IEnumerable into an Array and pass that to Concat


edit
Though all that usage of regular expressions, enumerables and string conversion will generate quite a bit of garbage. Since you really just want upper case letters something like that might be better:

     public static void GetUpperCaseLetters(string aText, System.Text.StringBuilder aOutput)
     {
         for(int i = 0; i < aText.Length; i++)
         {
             char c = aText[i];
             if (c > 'A' && c < 'Z')
                 aOutput.Append(c);
         }
     }
     public static string GetUpperCaseLetters(string aText)
     {
         var sb = new System.Text.StringBuilder(aText.Length);
         GetUpperCaseLetters(aText, sb);
         return sb.ToString();
     }

I splitted the method into two seperate methods as depending on the usecase you may want to reuse the string builder instance.

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 Marc_Keil · Mar 18, 2018 at 01:13 AM 0
Share

Thank you Bunny83, thats great!

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

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

LINQ Find String Duplicates in List 1 Answer

Replace chars in String with a Dictionary and avoid order-sensitive problems 0 Answers

Creating a card game. Best ways to interpret the various cards? 1 Answer

Replace() with string and Regex not replacing numbers/integers 2 Answers

How to replace a word inside a string with different white space combos? 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