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
2
Question by saberboy117 · Jan 04, 2012 at 06:22 PM · strings

SubStrings

Im working on a game and I want to identify pickups based on their names, is there a substring function so I can get a string of the other string from point a to point b?

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

4 Replies

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

Answer by luizgpa · Jan 04, 2012 at 06:36 PM

Yes, there's a method Substring. (also, Substring with one argument)

Example:

 print("foo bar".Substring(2, 4));

will output:

 o ba


Comment
Add comment · Show 4 · 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 saberboy117 · Jan 04, 2012 at 06:50 PM 0
Share

anything in js?

avatar image luizgpa · Jan 04, 2012 at 07:00 PM 0
Share

The code is the same in C# and UnityScript. It was tested in a JS file on Unity 3.3.

In theory you could use in UnityScript everything that's avaliable in .Net/$$anonymous$$ono.

avatar image flamy · Jan 04, 2012 at 07:10 PM 0
Share

@luiz everything u can use except Plugins ...

avatar image toddisarockstar · Apr 04, 2017 at 05:43 AM 0
Share

I love strings so i thought i would add to this wonderfull answer.

inside the () brackets. checking anything that is longer than the length of the string can produce errors. if you are unsure of the string then you must check the length of the string before a substring statement.

the example given is a unity function. so it indeed works with all sopported languages.

if using the .net version with C#........

the second number in the brackets represents the length of the substring ins$$anonymous$$d of the position!

avatar image
2

Answer by cj_coimbra · Jan 04, 2012 at 06:35 PM

There are lots of useful C# stuff you can do with strings such as:

yourString.Substring(int startIndex, int 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
avatar image
0

Answer by grovalmitch · Jun 27, 2013 at 06:28 AM

         string str = null;
         string retString = null;
         str = "This is substring test";
         retString = str.Substring(8, 9);

retString willreturn word "substring"

C# substring code...

groval

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
avatar image
0

Answer by firemyst · Apr 04, 2017 at 01:46 AM

One of the best blogs I've read on this topic is here: http://cc.davelozinski.com/c-sharp/fastest-way-to-check-if-a-string-occurs-within-a-string

It performs benchmarks using several methods to see what the fastest way is to find a string within a string.

Excellent read if you're into speed and micro-optimizing code.

Comment
Add comment · Show 2 · 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 Commoble · Apr 04, 2017 at 02:58 AM 0
Share

$$anonymous$$ind you, if you're into speed and micro-optimizing your code, you probably wouldn't be using strings to identify an object.

avatar image toddisarockstar Commoble · Apr 04, 2017 at 06:25 AM 0
Share

for some reason, when you get started many people here hate strings. and in some cases that is correct. but not always so.

everyone one knows that reserving a one byte of memory gives you 256 possibilitys. the default 32bit intreger value uses 4 bytes

not to mention floats with all those numbers before the decimal!

any string character always takes only 1 byte:)

so for comparison purposes, or reservation of ram; speed just depends on how long the string is when the CPU is deciding whether one byte is equal to the other.

if an enemy name is "e1","e2","e3". it will find it double the speed of an intreger value.

my last project needed to store / load large amounts of integer values that i knew would be less that 256. but that's the $$anonymous$$imum for a byte array. storing in strings ins$$anonymous$$d of int made my file format i was sending 1/4 of the size..... the moral of my story is dont hate strings.

when assigning variables in code length does not matter. its compiled into something else anyways. but if you are nameing strings for lookup then the amount of characters make a difference to the CPU. and if the cpu is compairing two things, the only thing the CPU cares about is how many bits and bytes!!!!!!!!

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

12 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

Related Questions

Adding variables to inspector strings 1 Answer

C# string replace method 1 Answer

How can i separate a string in 2? 1 Answer

Localization via script works in editor but not in build, why? 1 Answer

Comparing .NET Strings and JavaScript Strings 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