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 toddisarockstar · Mar 06, 2015 at 05:13 PM · strings

Javascript Strings. check for number of strings in strings?

i was wondering how to see how many times a string contains a set of characers. though there might be easy way to do it with javascript. here is what i came up with. it works but it seems silly to say all this.

 var checkarray:String[];
 
  var words:String;
  words="boring fun boring fun boring boring boring";
  
  checkarray = words.Split("fun"[0]);
  
  print("my user had fun " +checkarray.Length-1+" many times");
  

Also wondering about the String.Contains function. is there any way to get the character position when using String.Contains? or the characters following the spot without splitting?

Javascript here.

Comment
Add comment · Show 2
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 meat5000 ♦ · Mar 06, 2015 at 05:37 PM 0
Share

In Split the Character in the inverted commas is the separator for the split to occur.

If I am not mistaken your output will be

 checkarray[0] = "boring "
 checkarray[1] = " boring "
 checkarray[2] = " boring boring boring".

If it works, roll with it, however I will say that the actual content of the array becomes completely arbitrary after you've taken the count, so you may as well nullify it/clear it.

Remember that working with strings is always slow.

avatar image toddisarockstar · Mar 10, 2015 at 05:17 AM 0
Share

oh, i noticed the word "slow" in your comment. arrgggg. the strings im parcing in my current project actually create over a thousand arrays.

my game has a seperate level editor that saves to the players hard drive in txt format. it also sends level strings to other online players thought the internet. it works fine for a one time start up. i was debating on weather to use string info during gameplay or pack info into arrays. these sort of questions dont get much responce here. ive been only scripting about a year now so i got a lot to learn. any opinions would be helpfull.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tigertrussell · Mar 06, 2015 at 05:57 PM

Are you familiar with Regular Expressions?

You could create a Regular Expression which would match the word "boring," then you use Javascript's RegEx functionality to count the number of matches within your string.

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 toddisarockstar · Mar 07, 2015 at 02:00 AM 0
Share

Thanks Tiger, I will surely check into it! im doing a lot with strings in my current project and trying to learn everything i can about them!

avatar image Eric5h5 · Mar 07, 2015 at 03:23 AM 0
Share

Unity "Javascript" isn't web Javascript so that won't work at all. Unity uses $$anonymous$$ono, hence the Regex class: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx

avatar image tigertrussell · Mar 15, 2015 at 02:43 AM 0
Share

That's why I code in C# ;)

(edit: not really)

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

22 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

Related Questions

Get Component With Variable Help 2 Answers

NullRerenceException On comparisons 0 Answers

Using Buttons to write numbers 3 Answers

If String.Contains an item from List ? 1 Answer

Finding phrases within strings 2 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