hello and welcome to another episode in the GT script fundamental tutorial
你好,欢迎来到本集 GT 脚本基础教程
series in this episode we’ll be taking a quick look at strings so basically a
系列的另一集,我们将快速了解字符串,所以基本上
string is any value contained inside double quotations anything inside double
字符串是双引号内包含的任何值双引号内的任何内容
quotations is called a literal string value however I wanted to go just a
都称为文字字符串值,但是我想要
little deep into how strings are stored in memory address strings behave like an
稍微深入了解一下字符串是如何存储在内存
object in memory address almost similar to an array the only difference is you
can’t call certain functions in a string like you would in a ray but they are
地址中的
similar a string value has a character a single character stored at a specific
类似的字符串值有一个字符存储在特定内存地址的单个字符,
memory address and in memory address they are consecutively stored in memory
并且在内存地址中它们连续存储在内存中,
so basically in our address our capital S is at 20 RTS and 21 and we just keep
所以基本上在我们的地址中,我们的大写S 是 20 RTS 和 21,我们只是
going in order almost like an array and on top of that they do have indexes for
继续按顺序几乎就像一个 数组,除此之外,它们确实有索引,
example the first character in your string value is that index 0 and the
例如字符串值中的第一个字符是索引 0,
last value is at the last index position in this case 5 one thing you may not
最后一个值位于最后一个索引位置 这种情况5你可能
have known is that you can loop your string variable so in this case for
不知道的一件事是你可以循环你的字符串变量所以在这种情况下对于
string value has the word string we can loop it for example for every character
字符串值有单词字符串我们可以循环它例如对于
in our string value print out that character and it will print individually
我们字符串值中的每个字符打印出那个字符然后它会打印 将
each individual character inside our string value on top of that just like
字符串值中的每个单独的字符放在上面,就像数组一样,
arrays because strings behave almost like arrays we can indicate an index
因为字符串的行为几乎像数组一样,我们可以
position in our string value for example if we want the first letter of our
在字符串值中指示索引位置,例如,如果我们想要字符串的第一个字母,
string we just do string value square brackets with the index position in this
我们只需使用字符串值方括号 在这种
case we’re using 0 so the first position in our string which is the capital S
情况下,我们使用 0 的索引位置,所以我们字符串中的第一个位置,即大写的 S
string values come with a lot of methods however I just wanted to go over a few
字符串值有很多方法,但是我只想介绍一些
you may use in your programming journey first is the insert method so you get
您可能在编程过程中首先使用的方法是 insert 方法,所以你得到
your string object followed by the dot notation followed by the insert method
你的字符串对象,后跟点符号,然后是 insert
the first argument is the index position and the second argument is a string
方法,第一个参数是索引位置,第二个参数是一个字符串
value basically what you’re doing is you’re inserting the string value into
值,基本上是 w 你正在做的是将字符串值
the index position and then pushing everything else to the right
插入索引位置,然后将其他所有内容推到右边,
next you have the length method and just like an array it will return back the
接下来你有 length 方法,就像数组一样,它会返回
value and just like an array will return back a literal integer value that
值,就像数组会返回 一个表示字符串长度的文字整数值,
represents the length of your string in this case our string will always be the
在这种情况下,我们的字符串将始终是
last index position plus one so in this case we will get back to six and lastly
最后一个索引位置加 1,因此在这种情况下,我们将返回到 6,最后
you may want to split your strings keep in mind the split method cannot split
您可能想要拆分字符串,请记住 split 方法 不能
your strings by character you do have to declare a delimiter for example if your
按字符拆分字符串,您必须声明一个分隔符,例如,如果您的
string values are separated by commas you can use the comma character as your
字符串值用逗号分隔,您可以使用逗号字符作为
delimiter and you will split your string by that delimiter and return back an
分隔符,您将按该分隔符拆分字符串并返回
array containing your new values however if you want to split a string by its
包含新值的数组 但是,如果您想按字符拆分字符串,
character it’s best to use a for loop now let’s go ahead and take a look at
最好使用 for 循环,现在让我们继续看
some code as you can see here on our first line of code we have a variable
一些代码,正如您在我们的第一行代码中看到的那样,我们有一个 va riable
called string value and it is assigned a literal string value with the word
称为字符串值,它被分配了一个带有单词
string I’ve also created a second variable test basically this line to
string
show you that even when we assign our string value to tests we are not
的字符串值
pointing to the same memory address location I mean I want to show that even
location 我的意思是我想表明,
though we point to the same value if we were to change tests we will not change
即使我们指向相同的值,如果我们要更改测试,我们也不会
the value inside string value unlike an array now in our ready function you can
更改字符串值中的值,这与现在在我们的就绪函数中的数组不同,您
see here in our first line we are looping through our string value for
可以在我们循环的第一行中看到通过字符串值中
character in string value however just note that you can loop through strings
字符的字符串值,但是请注意,您可以遍历字符串
and your for loop and as you can see here we’re using some methods provided
和 for 循环,正如您在此处看到的,我们正在使用
to us in our string object the length method will return back 6 our split
在字符串对象中提供给我们的一些方法,长度方法将返回 6 我们的拆分
method and our delimiter is nothing so that just basically returns everything
方法和我们的分隔符什么都不是,所以基本上只是将所有内容
back into the index position 1 or rather 0 the index position 0 to our string
返回到索引位置 1 或者更确切地说是0 索引位置 0 到我们的字符串
variable so our variable will contain an array of length 1 containing our entire
变量,因此我们的变量将包含一个长度数组 1 包含我们的整个
string value and lastly we are inserting a string into our variable string insert
字符串值,最后我们将一个字符串插入到我们的变量字符串插入中
and basically what we’re saying is insert the new string value new with a
,基本上我们所说的是将新的字符串值
space and insert that into index position 0 so now our
插入一个空格并将其插入索引位置 0,所以现在我们的
in value which is string will now say new string or new space string and over
in 值是字符串 现在会说新字符串或新空格字符串,在
here we print that out to screen so you can see what the new values consist of
这里我们将其打印到屏幕上,这样您就可以看到新值的组成
and what they look like strings are quite complex but pretty straightforward
以及它们看起来像什么 字符串非常复杂,但
once you figure out how they behave in memory and how you’re able to use
一旦您弄清楚它们在内存中的行为方式就会非常简单,并且 您如何能够使用
methods provided to you with the string object in GT script the string object
GT 脚本中的字符串对象提供给您的方法 字符串对象
comes with a lot of options provided to you so feel free to check that out in
附带提供给您的许多选项,因此请随时
the GD script API documentation on the Godot Engine website that’s all I have
在 Godot 引擎网站上的 GD 脚本 API 文档中查看这些内容
for you in this episode thank you for joining me thank you for clicking the
在这一集中我为你准备了感谢您加入我感谢您单击“
like button thank you for subscribing I look forward to seeing you in the next
喜欢”按钮感谢您订阅我期待在下一集中见到您祝您
episode have an amazing day
有美好的一天