- Home /
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
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
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.
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!
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)
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"
groval
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.
$$anonymous$$ind you, if you're into speed and micro-optimizing your code, you probably wouldn't be using strings to identify an object.
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!!!!!!!!