- Home /
Is there a symbol which means 'any character'?
this is kinda hard to explain, but i want my script to see if there is a string which has a certain name (eg. "Weapon") and then see if there is a character next to it (eg. "Weapon0", "Weapon1", etc) i know if you search something in windows file browser, you can type * or something after the word and it will find anything which has that word plus another character or so. If someone can make sense of this and knows how to do this it would be appreciated.
Answer by Statement · Mar 03, 2011 at 10:39 AM
- You can use regular expressions for this.
- .NET has System.Text.RegularExpressions namespace for this task.
- You can also do a simple name.Contains("Weapon") or name.StartsWith("Weapon") (thx @yoyo).
A sample regex to find Weapon and any other characters following it could look like:
string pattern = "^Weapon.*$"
To get you started understanding regex, that regex means, "At the start of the string, there must be the string Weapon, followed by any character (the dot), zero or more times (the asterix), followed by end of string.
This pattern would match
- Weapon (Since it starts with Weapon, and have zero other characters after it)
- Weapon0 (Since it starts with Weapon, and have one other character after it)
- WeaponFoobar (Since it starts with Weapon, and have six other characters after it)
But would not match
- 6Weapon (Since it doesn't start with Weapon)
- Weapo15 (Since it doesn't start with Weapon, but Weapo)
- FooWeaponBar (Since it doesn't start with Weapon)
string.Contains would match 6Weapon and FooWeaponBar since it contains the string "anywhere".
string.StartWith would work just as the regex described above. It would accept any string that start with the string passed in. Kudos to yoyo for pointing this out.
yep, nice one. One $$anonymous$$or addition -- name.StartsWith("Weapon") and name.EndsWith("Weapon") can come in handy too.
Answer by Aldwoni_legacy · Mar 03, 2011 at 10:36 AM
You can count the amount of characters. If all names have Weapon in it you just need to check if there are more then 6 characters or not.
Answer by Meltdown · Mar 03, 2011 at 10:38 AM
If you are scripting in C#, do a google search for Regular Expressions C# While it is a little complex in the beginning to understand, you can do all your checks with them.
No, you can use RegExp in Javascript too Check out.. http://www.javascriptkit.com/javatutors/redev2.shtml
I mean, can't you use the regular expressions in .net/mono?
Your answer
![](https://koobas.hobune.stream/wayback/20220613111125im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
using array functions on a string? 1 Answer
changing GUI Button text with a string array 2 Answers
How to delete the last character of a string 3 Answers