x


Get first certain numbers from a string

Hi guys! I'm sorry if the question is a bit unclear since I don't know how should I put it.

Basically it's should be something simple, I just want to get the first "x" numbers from a string and turn them into a new string. For example, I have a string "unity", I want to get the first 2 characters from the string so the new string should be "un". Thank you in advance =)

more ▼

asked Mar 12 '12 at 10:17 AM

stevenp gravatar image

stevenp
108 10 14 15

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Use string.Substring(). You use it like this:

var oldString : String = "unity";
var newString : String = "";

newString = oldString.Substring(x, y);

x being the character index you wish to begin your SubString at, and y being the length of characters you want the Substring to be. So in your case it would be:

newString = oldString.Substring(0, 2);

and this would return newString = "un".

Hope that helps! Klep

more ▼

answered Mar 12 '12 at 10:27 AM

Kleptomaniac gravatar image

Kleptomaniac
2.5k 6 12 21

Thanks a lot for a swift response! It works, I figured it should be simple but the Unity documentation about String only consist of "string" and "Length" so I couldn't find it xD

Mar 12 '12 at 10:41 AM stevenp

Haha, yes pretty simple. Sometimes the docs won't have everything you need, so in that case, try looking in the msdn .NET libraries, because they'll most certainly have whatever you need in there. :)

Glad I could help! Klep

Mar 12 '12 at 10:44 AM Kleptomaniac

The Unity documentation only covers Unity; for all the Mono stuff you look in the .net or Mono docs (the Mono docs are a little more accurate but less complete).

Mar 12 '12 at 10:48 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3568
x430

asked: Mar 12 '12 at 10:17 AM

Seen: 823 times

Last Updated: Mar 12 '12 at 10:48 AM