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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by JChiu · Feb 21, 2014 at 07:03 AM · c#stringcount

How to count the number of strings in a string class?

How do you count the number of strings used in the same string class? Said for example I have two scripts.

 in script one//
 
 public void something(string someWord) {
     //the number of string that belongs to someWord. in this case is 3.
     //How do I do it in C#? 
 } 

 And in script two//
 
 void addstuff() {
    script one.something("word a");
    script one.something("word b");
    script one.something("word c");
 }

P.S. (in case you still don't understand me) I have looked for a solution but so far is only dealing with how many letters in a string. However, I want the number of words (or string)that has been created by the script or a another script calling it.

Comment
Add comment · Show 3
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 flamy · Feb 21, 2014 at 07:05 AM 0
Share

by number of strings do you mean by number of words seperated by space or number of letters in the string? or is it completly different.

avatar image Nick4 · Feb 21, 2014 at 07:12 AM 0
Share

I'm not a c# expert but as far as I know there's no such method to count words in ".net". It shouldn't be too hard to build one. Just get the string variable you want and check each time it detects a space in that string variable. That number + 1 should theorically represent the quantity of words.

avatar image getyour411 · Feb 23, 2014 at 02:33 AM 0
Share

while it's overkill for this use case, there is RegEx (regular expression lib)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GenuiTix · Feb 21, 2014 at 07:58 AM

If you are going to store and use those strings later, you should use list to do that. For using list directly you have to declare preprocessor instruction

 using System.Collection.Generic;

at the beginning of your code. Nærby simillar declariations. Later in your code you have to declare list in body of yours class. Like that:

 List <string> stringsStored;

Your function is rædy now to cøllect string list:

 public void something(string someWord) {
     stringsStored.Add(someWord);
     // Below is your code
 } 

Than the strings amount will be accessible through

 stringsStored.Count

from all other functions in this class. Ås your function is public it will be accessible from other modules as well (until your class is public).

Or

Something simpler. You can use counter defined outside 'something' function bødy and increment it every time the function has been invoked.

 int counter = 0;
 public void something(string someWord) {
     counter++;
     //Other stuff
 } 
Comment
Add comment · Show 3 · 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 RudyTheDev · Feb 21, 2014 at 11:31 AM 0
Share

I think what OP probably wants is to use a List.

avatar image GenuiTix · Feb 21, 2014 at 08:21 PM 0
Share

I thought I included complete solution for the list. I'll update my post then.

avatar image JChiu · Feb 23, 2014 at 02:30 AM 0
Share

Thanks, I have used you second solution scene that how the assessment is (partially) written up.

avatar image
1

Answer by whydoidoit · Feb 21, 2014 at 08:47 AM

Presuming you have a string:

      var numberOfWords = someString.Split( new [] { ' ', '\t' }).Length;
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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# String Array Has Missing or Incorrect Keycode Strings 2 Answers

Can I create a list with an int/float and a string? C# 2 Answers

How do I change the fount size of GUI 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