跳到主要内容
版本:release

串类型(string, buffer)

string为字符串,buffer解释为缓冲区,他们都是连续存储并且存储内存都是动态分配的。

string

string可以理解为一个连续存放字符的容器,它和buffer的区别是buffer可以存放其它数据结构,而string只能存放字符。 string初始化,以及一些简单的符号运算:

string str = "Hello World";         // 使用双引号,只能包括一行字符串

string str0 = """P // 使用"""P ... """P; 的方式可以定义多行任意格式字符串
Hello World! // 其中P可以为[a-z]和[A-Z],任意字母无差别,但要保证前后一致。
This is my first program,
thank you!
"""P;

string str1 = "Hello", str2 = " World";
string add_str = str1 + str2; // add_str为"Hello World"
printf("%c",add_str[0]); // 输出 H

从设备输入数据

mixed input_string(string prompt = EMPTY_STRING, bool echo = true, int max_size= 4096);  // 原型
// 返回值如果是nil表示EOF,数字表示出错,否则就是string

import gs.lang.io

string str = "";
array lines = []; // 行数
int total_size = 0; // 总长度
while (str = input_string("", false, 1024*1024))
{
lines << [str];
total_size += str.length();
}
  • string语法糖
string str = "hello";
write(str[0..4]); // hello
write(str[0..2]); // hel
write(str[0..0]); // h

// <n 代表从右边向左边数,第n个
write(str[0..<1]); // hello
write(str[1..<3]); // el


string常用的外部函数

下面列出string类型一些常用的外部函数以及用法。

1. int转成string

函数原型:
string int_instance.to_string()
使用方法:
int val = 1024;
string str = val.to_string(); // str = "1024"

2. float转成string

函数原型:
string float_instance.to_string()
使用方法:
float pi = 3.14;
string str = pi.to_string(); //str= "3.14

3. bigint转成string

函数原型:
string bigin_instancet.to_string()
使用方法:
bigint bi = bigint("10000");
string str = bi.to_string(); // str为"10000L"

4. 得到string长度

函数原型:
int string_instance.length()
使用方法:
string str = "Hello World";
int length = str.length(); // length = 11

5. 字母转小写

函数原型:
string string_instance.to_lower()
使用方法:
string str = "Hello World";
str = str.to_lower(); // str = "hello world"

6. 字母转大写

函数原型:
string string_instance.to_upper()
使用方法:
string str = "Hello World";
str = str.to_upper(); // str = "HELLO WORLD"

7. 是否为string

函数原型:
bool is_string(srting str)
使用方法:
string str = "Hello World";
bool flag = is_string(str); // flag = true

8. 比较

函数原型:
int strcmp(string s1, string s2) // s1<s2 => -1;
使用方法:
string s1 = "abc", s2 = "abd";
int val = strcmp(s1, s2); // val = -1

9. 转小写比较

函数原型:
int stricmp(string s1, string s2)
使用方法:
string s1 = "aBc", s2 = "abC";
int val = stricmp(s1, s2); // val = 0

10. 相同位置不同字符个数

函数原型:
int strdiff(string s1, string s2)
使用方法:
string s1 = "31243", s2 = "1234";
int val = strdiff(s1,s2); // val = 4

11. 查找

函数原型:
int strsrch(string s1, int/string s2, bool reverse = false)
使用方法:
string s1 = "hello hello world", s2 = "lo";
int pos = strsrch(s1, s2); // pos = 3

12. 递归过滤前后filter

函数原型:
string string_instance.trim(int option = 3, string filter = " ")
使用方法:
string str = "abab hello world abab";
str = str.trim(1, "ab"); // str = " hello world abab"

13. 分割字符串

函数原型:
array string_instance.explode(string delim = "")
使用方法:
string str = "hello world";
array arr = str.explode(" "); // arr = ["hello", "world"]

14. 提取子串

函数原型:
string string_instance.get_range(int pos. int count = -1)
使用方法:
string str = "hello world";
str = str.get_range(3, 2); // str = "lo"

15. 替代子串

函数原型:
mixed string_instance.replace(string s1, string s2)
使用方法:
string str = "hello hello world";
str = str.replace("ello","i"); // str = "hi hi world"

16. 使用string保存数据

函数原型:
string save_string(mixed val)
使用方法:
array arr = [1, "hello", 3.14];
string str = save_string(arr); // str = "[1,"hello",3.14000]"

17. 获取输出字符流

函数原型:
string sprintf(string str, ...)
使用方法:
string str = sprintf("%O", true); // str = "true"

18. 获取输入字符流

函数原型:
array sscanf(string val, string fmt)
使用方法:
array arr = sscanf("g1b2i3ts", "g%db%*si%cts"); // arr = [3, 1, 51]

19. 是否以s为结尾

函数原型:
bool string_instance.ends_with(string s1)
使用方法:
string str = "hello world";
bool flag = str.ends_with("ld"); // flag = true

20. 是否以s为开始

函数原型:
bool string_instance.starts_with(string s1)
使用方法:
string str = "hello world";
bool flag = str.starts_with("hel"); // flag = true

21. 字母是否都是大写

函数原型:
bool string_instance.isupper()
使用方法:
string str = "HELLO WORLD";
bool flag = str.isupper(); // flag = true

22. 字母是否都是小写

函数原型:
bool string_instance.islower()
使用方法:
string str = "hello world";
bool flag = str.islower(); // flag = true

23. 获取子串的索引(顺序)

函数原型:
int string_instance.index_of(string str)
使用方法:
string str = "hello world";
int index = str.index_of("o"); // index = 4

24. 获取子串的索引(逆序)

函数原型:
int string_instance.rindex_of(string str)
使用方法:
string str = "hello world";
int index = str.rindex_of("o"); // index = 7

表格第12项参数:option:1,2,3分别表示left,right,both size;filter表示过滤因子

buffer

buffer解释为缓存,可以把它理解成一个连续存储的缓存容器,这个容器不仅可以放int,float这些基本数据类型, 还可以存放string,array,map,buffer等等任意mixed数据类型。 我对buffer的理解是这样的: 比如有一个长度n=10的string数组string str[10] ;

string add_string()
{
string result = "";
for(int i = 0;i < n;i ++)
result += str[i];
return result;
}

当执行这个程序的时候,for循环会不断地构造一个新的string对象然后赋给result,然而我们只需要最后一个result的结果, 中间过程中的9个string对象都是浪费内存。 如果我们使用buffer的话,每次修改操作只会在原来的对象中操作,不会生成新的对象,这样也就起到了节省内存空间的效果了。

buffer初始化,以及一些简单的符号运算:

buffer buf = (buffer)"Hello World"; // 初始化buf
buffer buf1 = (buffer)"Hello", buf2 = (buffer)" World";
buffer add_buf = buf1 + buf2; // add_buf为"Hello World"
write(add_buf[0]); // 输出 72 ('H'的ASCII)

buffer常用的外部函数

下面列出buffer类型一些常用的外部函数以及用法。

1. 分配空间

函数原型:
buffer buffer.allocate(int size)
使用方法:
buffer buf = buffer.allocate(6); // buf.length() = 6

2. 清除

函数原型:
void buffer_instance.clear()
使用方法:
buffer buf = buffer.allocate(6);
buf.clear(); // buf.length() = 0

3. 删除

函数原型:
void buffer_instance.delete_at(int pos, int num = 1)
使用方法:
buffer buf = buffer.allocate(0);
buf << "hello world";
buf.delete_at(1,4); // buf = "h world"

4. 查找

函数原型:
int buffer_instance.find(mixed val)
使用方法:
buffer buf = (buffer)"hello world";
int index = buf.find("wo"); // index = 6

5. 取值

函数原型:
mixed buffer_instance.get(int pos)
使用方法:
buffer buf = (buffer)"hello world";
mixed val = buf.get(1); // val = 101 ('e'的ASCII)

6. 取子buffer

函数原型:
buffer buffer_instance.get_range(int pos, int count = -1)
使用方法:
buffer buf = (buffer)"hello world";
buffer child = buf.get_range(6,3); // child = "wor"

7. 插入

函数原型:
void buffer_instance.insert(int pos, mixed val)
使用方法:
buffer buf = buffer.allocate(0);
buf << "hello world";
buf.insert(6,"hi "); // buf = "hello hi world"

8. 往后插入

函数原型:
void buffer_instance.push_back(mixed val)
使用方法:
buffer buf = buffer.allocate(0);
buf << "hello world";
buf.push_back("!"); // buf = "hello world!"

9. 赋值

函数原型:
void buffer_instance.set(int pos, mixed val)
使用方法:
buffer buf = buffer.allocate(0);
buf << 10; buf.set(0, 1); // buf = 1

10. 转成十六进制串

函数原型:
string buffer_instance.to_hex()
使用方法:
buffer buf = (buffer)"abcd";
string str = buf.to_hex(); // str = "61626364"

11. 保存

函数原型:
buffer save_buffer(mixed val, int option_flag = 0)
使用方法:
buffer buf = save_buffer("hello world"); // buf = "hello world"

12. 是否为buffer

函数原型:
bool is_buffer(mixed val)
使用方法:
buffer buf = (buffer)"hello world";
bool flag = is_buffer(buf); // flag = true

13. 复制

函数原型:
void buffer.copy(buffer dst, int pos, buffer src, int start = 0, int copy_len = 2^63 - 1)
使用方法:
buffer buf = (buffer)"hello world";
buffer buf1 = buffer.allocate(11);
buffer.copy(buf1, 0, buf);// buf1 = "hello world"