ID:181950
 
lol sorry to ask but is there a copytext for java like how its used in DM?

How do I turn DM's:
if(copytext(name,1,2)==" ")

Into Javascript
The substr() function is what you're after.
In response to Nadrew
Yeah, found thaout by searching javascript string functions xD. The difference from DM and javascript is you do 0,1 for the 1st letter and in DM you use 1,2 lol. I hope I don't get confused in future projects.
In response to Ssj4justdale
That's one thing you'll find a lot, DM uses 1-based list indexing and most other languages use 0-based. Strings internally are actually character arrays, each letter in the string belongs to its own array element. Python makes awesome usage of this fact by letting you slice your strings up without using functions.

// Python
mystring = "hello world!"
print mystring[0:5] // hello
print mystring[-5] // orld!


I believe that's the syntax, I haven't done a whole lot in Python lately and I'm a bit rusty.
In response to Nadrew
Yeah, I understand. I stopped using javascript for a while so I am just reviewing some stuff and seeing what I remember.
In response to Nadrew
I believe you are right with those. I've accidentally ran over those tricks a few times. Kinda weird when your output is suddenly "worl" when you know you put that darn d at the end. But when you need to slice up some text, or start at a certain point, it's really handy. I haven't tried any other array functions(index() for example) on strings, but that could be interesting too.
In response to Nadrew
Nadrew wrote:
Python makes awesome usage of this fact by letting you slice your strings up without using functions.

> // Python
> mystring = "hello world!"
> print mystring[0:5] // hello
> print mystring[-5] // orld!
>



actually, the [n...] *is* the function. :-)
In response to digitalmouse
No it's not, it's an array reference method.
In response to Nadrew
It's calling a function in the background. [:] is just syntax sugar.
In response to PirateHead
and it can't be an 'array reference' since the parameters in your example are 'start point' and 'number of characters'.

it's a straightforward string manipulation function. the interpreter see the string, then sees the brackets and parameters and knows that a specific 'function'-ality needs to be applied.

a rather neat usage in my opinion.
In response to digitalmouse
Function or reference, it doesn't really matter. It's extremely useful and gets rid of so much extra text-processing. I wonder if it would be possible for BYOND to encompass it into the next version, doesn't seem SOOO hard... or is it?
In response to Haywire
Haywire wrote:
It's extremely useful and gets rid of so much extra text-processing.

Huh? If it's indeed how it looks, it's basically just a different syntax for DM's copytext(String,N,N2), that takes slightly different arguments. How is it so more extremely useful and gets rid of a lot of extra processing?
In response to Kaioken
You can access the variable directly without having to use other functions to process the text? It's as simple as it sounds.

Would you rather do:

var/string = "Hello World!e"
world << world << copytext(string,1,12)


OR

var/string = "Hello World!e"
world << string[1:12]


We do a lot of crap to cut strings up, etc, etc. That all becomes a lot easier.
In response to Haywire
Python takes different args than in your code, but nevermind.

Haywire wrote:
You can access the variable directly without having to use other functions to process the text? It's as simple as it sounds.

Accessing the var directly would just give you its current, full value. :\ This syntax is just a shortcut way for convenience for calling the function that cuts the string. It doesn't save any "other functions to process the text", they're still called, just implicitly. The fact you don't see them in the code itself doesn't mean they aren't called. For a DM example, when you set client.mob into a different mob, Login() and Logout() are implicitly called, but they're nowhere in your code line that just sets the var! zomg!
In response to Kaioken
Yeah, you're right. Although either way it doesn't matter much to the client or "DreamSeeker" - the virtual world running it. It's obviously for the convenience of programming it. I'd rather use the Python method than calling copytext(). Maybe it's just me but that's how I would prefer it and there are probably countless other people who'd like to too.
In response to Haywire
I love how this discussion of python has nothing to do with the original post. Anyway, the beauty of python is that it is very easy to program in. Python is slow for the sake of being slow, but it will always be slow compared to compiled code. The trade off is that you can typically develop in python faster.

At my work, I code in C++. However, I often write up some python scripts to test functionality because I can write them up quickly and I'm only going to run them a few times, so the speed in which they run isn't a big deal.

Point being, the way python handles strings makes perfect sense to someone who's programmed for a while. It's easy to read and understand without using more 'words' than necessary (as opposed to Java which is easy to read, but uses many more 'words').
In response to Stupot
You've summarised my point in a much more understandable manner.

I'm not really good with words so thanks.