Working with strings

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Status
Not open for further replies.

Martini002

Member
Feb 17, 2017
36
0
0
Hello guys,

How can I extract a sub string from string?

code_language.skript:
192.16.0.1

I want to extract the first two octets

code_language.skript:
192.16

It is possible using Skript?

This is my first time with this plugin and look very interesting.

Thanks in advance
 
If you know the string is going to be exactly like that, you can just do "first 6 characters of %text%" to get those first six characters. e.g. In this example command:

code_language.skript:
command /substring <text>:
    trigger:
        send "%first 6 characters of arg-1%" to the command sender

If you then ran "/substring 192.16.0.1" it would send you back the 192.16 as you said.

If you want to do it more dynamically, where you get everything before the 2nd break point, you could split the string at the periods, then recombine the first two parts, which would look like this:

code_language.skript:
command /substring <text>:
    trigger:
        set {_sections::*} to arg-1 split at "."
        send "%{_sections::1}%.%{_sections::2}%" to the command sender

Then doing the same command (/substring 192.16.0.1) would send you the 192.16 as before, but it'd work with other IPs too, e.g. doing "/substring 255.136.1.0" would return "255.136" even though it's more than six characters. What it does is separate the input wherever there's a period, dividing the 192.16.0.1 into 192, 16, 0, and 1, and stores them in the list you provide, which in my case I called {_sections::*}. Then I just grab the first two elements of the list (with {_sections::1} and {_sections::2}) and send them back to the user with the period re-added between them.

Hope that helps!
 
  • Like
Reactions: PetyXbron
Sort of, they're referred to as lists, but they're more like traditional lists fused with hash maps/dictionaries, in terms of other programming languages. You can add elements to a Skript list, set it to a specific set of values (like I did in that split), and loop over a list. As you saw, you can refer to specific elements in the list by adding a specific key after the :: list identifier, which is very much like using the square brackets array notation in another language.

You'll notice that Skript list indices are 1 based instead of 0 based, so {_sections::1} is equivalent to sections[0] if you were to convert it to another language. However, the reason I say they're also like hash maps is because you can manually set the key that goes after the :: identifier. As you can see, the default keys that are used are just numbers that count up from 1, but you can set them yourself, which can be very useful. For example, I have this code on my server:

code_language.skript:
on join:
    set {name::%player's uuid%} to player's name

Which then lets me reference the {name::*} list in any other script, but pass in the UUID of any player who joins my server to get back their name, without me having to use an addon to parse their UUID as a player to get their name.

Some other example code just to give you an idea:

code_language.skript:
on join:
    set {players::%player's uuid%} to player's name

command /listplayers [<text="">]:
    trigger:
        if text-argument is "":
            send "Listing all players who have joined:" to the command sender
            send "%{players::*}%"
        else:
            send "Listing all players whose name contains '%text-argument%':" to the command sender
            loop {players::*}:
                "%loop-value%" contains "%text-argument%"
                add loop-value to {_matching::*}
            send "%{_matching::*}%" to the command sender

command /listuuids:
    trigger:
        send "Listing UUID of all players who have joined:" to the command sender
        loop {players::*}:
            add "%loop-index%" to {_uuids::*}
        send "%{_uuids::*}%" to the command sender

As you can see, you can reference the "key" (the index) of the current thing you're looping in a list with the "loop-index" expression. You can think of looping a list like foreaching over a dictionary or hashmap, where loop-index is the current set's key while loop-value is the current set's value.
 
Last edited by a moderator:
Status
Not open for further replies.