String.charAt(n)?

I’d like to be able to take a string and find a specific character in that string (say, the fifth letter). In Javascript and Actionscript, I’m used to having access to string manipulation tools like charAt, but Unity doesn’t seem to support these, and the rare bit of help I can find on the subject refers me to something I’ve never heard of called .NET.

Any advice? Does Unity have something I can use? The only workaround I see is to create an array with each letter of the word as a seperate item in the array, but that’s going to be quite tedious if it’s the only solution.

Incidentally, this may be why I’ve never yet seen a Unity word game.

I know in C#, you just use the string like an array:

String str = "Hello";
char c = str[1];   // Will store 'e' in c

.NET actually has tons of string manipulation features. Check out the MSDN C# docs. I have used them to make at least 3 word games with Unity (working on a 4th right now).

.NET is the name of the Microsoft framework that includes a library of classes that do all kinds of useful things. Unity uses a 3rd-party implementation of the .NET framework, so just about anything you find documented in Microsoft’s MSDN docs, you can use in Unity. It’s actually extremely powerful.

you are in the best place i think because none of the other game engines have this amount of string manipulation functions.
with .NET you have regular expressions and string and string builder classes and many many many more just take a .NET book or look at string processing in .NET programming guide on MSDN’s web site

i actually wrote this reply to tell you be careful about strings and changing them.
each time that you change an string a new instance of it will be created and the last one will become a garbage and will be collected soon after the garbage collector tries to collect garbages of this function after this function’s work is done

maybe seems complicated but just google this term “strings in C#” or “.NET memory management and GC” for information about garbage collector and strings

jessy liberty’s book called “programming C# 3” has a good chapter about strings. find this book in a library and read the chapter to learn anything needed

i am writing a small tutorial for unity users that they don’t know much about .NET but i am in early stages of writing.

ahh - you gave some great info - because i wondered exactly why the hell the simple line (see below) in Update() causes heap growing until GC cleans up…

Place only this line in Update() and you’ll see the danger with C# string functions - just ridiculous…
(no big deal in general - but on the iphone it causes my app to produce framedrops when i use string.Format() to produce my Score Display…

string.Format("ABC); :evil:

her’s my original post…
http://forum.unity3d.com/viewtopic.php?t=33929

Okaaaay… so, I need to learn the .NET programming language to access this stuff? Or do I need to learn C#? Or can I still access this stuff in a Javascript file? I still don’t understand how to implement any of this stuff.

C# and JavaScript are languages, .NET is a class library that you call from a language. Think of .NET as a library of routines/classes you can use that are already written for you. .NET is not a language in itself. If you see the MSDN docs, you’ll see code examples for using various .NET classes from different languages (like C#, Visual Basic, J#, etc).

If you’re going to be making heavy use of .NET classes, I’d recommend using C# as you can easily translate the MSDN documentation examples to your own code (the MSDN docs don’t have JavaScript samples). But it’s up to you.

It’s not a programming language, it’s an API.

Look in the MSDN docs…JScript is pretty similar to Javascript in Unity.

There are Unity word games, such as Wordular.

Just to note, they are .NET string functions, not C# string functions. Important because the functions are the same in all languages, not just one.

–Eric

hey guys .NET is not just a set of libraries
.NET is a set of services for software development.
it has a virtual machine. a runtime called CLR (common language runtime), a collection of class libraries and some other services specific to windows platform and what we use here (Mono) is a free and open source implementation of .NET framework that it’s advantage is that it works on different platforms. Microsoft’s CLR runs on windows and windows mobile and XBOX 360 and zune and some other devices like Micro devices that they support .NET micro framework and Microsoft surface but Mono supports linux, max and other unix based oporating systems.
the Mono version that unity uses is old and don’t support somefeatures of the languages and some classes of the latest .NET framework but it’s the most powerful scripting language that a multiplatform engine can have, it’s the best choice. it’s very better than lua/python/…
just not for languages but for class libraries

Yes, we know, but I was giving a very simplified description since the OP is obviously unfamiliar with all of this. I figure its best not to overload people with information they don’t know what to do with yet. :slight_smile:

there is a good rule about this.
information should be simple and also complete. when you say .NET is a class library i would think it’s a class library like C++ libraries or DLLs.
a complex description could be something like.

.NET has a managed enviroument with garbage collection and code security levels. it has a part called CLI that tells the .NET to have same datatypes is all platforms
.NET libraries are implemented as COM objects and interfaces in DLLs and .NET has an abstract machine that can execute it’s bytecode called CIL. the code you write in a .NET enviroument compiles to CIL code by a language specific compiler and then can be run by .NET’s CLR on a supported platform.

just kidding :smile: :smile: :smile:

I agree with Brady… I appreciate the help Ashkan, but I have no idea what you are talking about.

As it turns out, all I really needed to know is that I can use .NET functions regarding strings just as easily as using Unity scripts. After reading through the .NET documentation on strings, I made the following code, which gives me the basic framework of what I needed to do:

var myString = "TRAMPOLINE";

function Start () {
	for (i=0; i<myString.length; i++) {
		print (myString.Substring(i, 1));
	}
}

//returns each letter individually

It sounded a lot more complicated on Microsoft’s site, of course…

sorry but i wrote the last post just for kidding
after all if you have any question or problem with .NET just PM me or write that as a post so others can use that too

Hi, I am using Unity javascript to access .NET System, System.IO, and System.Xml:

import System;
import System.IO;
import System.Xml;

I’m reading an xml file using:
var xdoc = new System.Xml.XmlTextReader(“./path_to_file”);

The elements and attributes are read fine, and they are assigned to an Array and Hashtable. The Hashtable handles each xml element’s key=value pairs. Multiple hashtables are loaded into the array. So, I can access each element of the xml file by the array row and then the hashtable key = element pair.

So, when I’m reading in the values for any given xml element, it is treated as text. I’ve looked up the System.String.Split() method, and it seems to ask for delimiter characters in the argument list.

When I try this:

someString.Split(“,”);

I get an error:

MissingMethodException: Method not found: ‘System.String.Split’.

When I remove the argument like this:

someString.Split();

It runs fine, and “Split” s the string with a SPACE…

What am I doing wrong? Or are the some Unity-Javascript-specific nuances with .NET? If so, is there a book or a reference manual that outlines these?

Thank you - Maximum respect
guitarguy91403

I’m not 100% sure, but I think the problem is you need to use single quotes since you are specifying a character rather than a string. So it would be:

someString.Split(‘,’);

Alternatively, you could do:

char[ ] delimiters = new char[ ] {‘,’ , ‘;’};

Where ‘,’ and ‘;’ are characters you want to use as delimiters. This allows you to use multiple delimiters in a single Split() call.

PS: The sample code above is in C#, I’m not 100% of the equivalent JS code, but you should be able to do the same in JS.

Try this:

fields = someString.Split(","[0]);

Yes, that will work too since the result is a character (rather than a string). Just something to remember: double-quotes denotes a string, single-quotes denotes a character.

Brady and dpn,

thank you - that worked!

Besides the MSDN documentation, do you recommend a book with examples to use .NET?

Thanks again

Maximum respect
GuitarGuy91403