stream

package
v0.5.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 2 Imported by: 0

README

Stream

Go doc

暂无介绍...

目录导航

列出了该 package 下所有的函数及类型定义,可通过目录导航进行快捷跳转 ❤️

展开 / 折叠目录导航

包级函数定义

函数名称 描述
NewString 创建字符串流
NewStrings 创建字符串切片

类型定义

类型 名称 描述
STRUCT String 字符串流
STRUCT Strings 字符串切片

详情信息

func NewString[S ~string](s S) *String[S]

创建字符串流

查看 / 收起单元测试

func TestNewString(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "hello"}, {name: "case2", in: "world", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in)
			if got.String() != c.want {
				t.Fatalf("NewString(%s) = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func NewStrings[S ~string](s ...S) *Strings[S]

创建字符串切片

查看 / 收起单元测试

func TestNewStrings(t *testing.T) {
	var cases = []struct {
		name string
		in   []string
		want []string
	}{{name: "empty", in: []string{}, want: []string{}}, {name: "one", in: []string{"a"}, want: []string{"a"}}, {name: "two", in: []string{"a", "b"}, want: []string{"a", "b"}}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewStrings(c.in...)
			if got.Len() != len(c.want) {
				t.Errorf("got %v, want %v", got, c.want)
			}
		})
	}
}


String STRUCT

字符串流

type String[S ~string] struct {
	str S
}

func (*String) Elem() S

返回原始元素


func (*String) String() string

返回字符串

查看 / 收起单元测试

func TestString_String(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "hello"}, {name: "case2", in: "world", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).String()
			if got != c.want {
				t.Fatalf("String(%s).String() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Index(i int) *String[S]

返回字符串指定位置的字符,当索引超出范围时将会触发 panic

查看 / 收起单元测试

func TestString_Index(t *testing.T) {
	var cases = []struct {
		name        string
		in          string
		i           int
		want        string
		shouldPanic bool
	}{{name: "case1", in: "hello", i: 0, want: "h", shouldPanic: false}, {name: "case2", in: "world", i: 2, want: "r", shouldPanic: false}, {name: "case3", in: "world", i: 5, want: "", shouldPanic: true}, {name: "case4", in: "world", i: -1, want: "", shouldPanic: true}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			defer func() {
				if r := recover(); (r != nil) != c.shouldPanic {
					t.Fatalf("NewString(%s).Index(%d) should panic", c.in, c.i)
				}
			}()
			got := stream.NewString(c.in).Index(c.i)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Index(%d) = %s; want %s", c.in, c.i, got, c.want)
			}
		})
	}
}


func (*String) Range(start int, end int) *String[S]

返回字符串指定范围的字符

查看 / 收起单元测试

func TestString_Range(t *testing.T) {
	var cases = []struct {
		name        string
		in          string
		start       int
		end         int
		want        string
		shouldPanic bool
	}{{name: "case1", in: "hello", start: 0, end: 2, want: "he", shouldPanic: false}, {name: "case2", in: "world", start: 2, end: 5, want: "rld", shouldPanic: false}, {name: "case3", in: "world", start: 5, end: 6, want: "", shouldPanic: true}, {name: "case4", in: "world", start: -1, end: 6, want: "", shouldPanic: true}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			defer func() {
				if r := recover(); (r != nil) != c.shouldPanic {
					t.Fatalf("NewString(%s).Range(%d, %d) should panic", c.in, c.start, c.end)
				}
			}()
			got := stream.NewString(c.in).Range(c.start, c.end)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Range(%d, %d) = %s; want %s", c.in, c.start, c.end, got, c.want)
			}
		})
	}
}


func (*String) TrimSpace() *String[S]

返回去除字符串首尾空白字符的字符串

查看 / 收起单元测试

func TestString_TrimSpace(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: " hello ", want: "hello"}, {name: "case2", in: " world ", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).TrimSpace()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).TrimSpace() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Trim(cs string) *String[S]

返回去除字符串首尾指定字符的字符串

查看 / 收起单元测试

func TestString_Trim(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		cs   string
		want string
	}{{name: "case1", in: "hello", cs: "h", want: "ello"}, {name: "case2", in: "world", cs: "d", want: "worl"}, {name: "none", in: "world", cs: "", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Trim(c.cs)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Trim(%s) = %s; want %s", c.in, c.cs, got, c.want)
			}
		})
	}
}


func (*String) TrimPrefix(prefix string) *String[S]

返回去除字符串前缀的字符串

查看 / 收起单元测试

func TestString_TrimPrefix(t *testing.T) {
	var cases = []struct {
		name    string
		in      string
		prefix  string
		want    string
		isEqual bool
	}{{name: "case1", in: "hello", prefix: "h", want: "ello", isEqual: false}, {name: "case2", in: "world", prefix: "w", want: "orld", isEqual: false}, {name: "none", in: "world", prefix: "x", want: "world", isEqual: true}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).TrimPrefix(c.prefix)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).TrimPrefix(%s) = %s; want %s", c.in, c.prefix, got, c.want)
			}
		})
	}
}


func (*String) TrimSuffix(suffix string) *String[S]

返回去除字符串后缀的字符串

查看 / 收起单元测试

func TestString_TrimSuffix(t *testing.T) {
	var cases = []struct {
		name    string
		in      string
		suffix  string
		want    string
		isEqual bool
	}{{name: "case1", in: "hello", suffix: "o", want: "hell", isEqual: false}, {name: "case2", in: "world", suffix: "d", want: "worl", isEqual: false}, {name: "none", in: "world", suffix: "x", want: "world", isEqual: true}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).TrimSuffix(c.suffix)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).TrimSuffix(%s) = %s; want %s", c.in, c.suffix, got, c.want)
			}
		})
	}
}


func (*String) ToUpper() *String[S]

返回字符串的大写形式

查看 / 收起单元测试

func TestString_ToUpper(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "HELLO"}, {name: "case2", in: "world", want: "WORLD"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).ToUpper()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).ToUpper() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) ToLower() *String[S]

返回字符串的小写形式

查看 / 收起单元测试

func TestString_ToLower(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "HELLO", want: "hello"}, {name: "case2", in: "WORLD", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).ToLower()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).ToLower() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Equal(ss S) bool

返回字符串是否相等

查看 / 收起单元测试

func TestString_Equal(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		ss   string
		want bool
	}{{name: "case1", in: "hello", ss: "hello", want: true}, {name: "case2", in: "world", ss: "world", want: true}, {name: "case3", in: "world", ss: "worldx", want: false}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Equal(c.ss)
			if got != c.want {
				t.Fatalf("NewString(%s).Equal(%s) = %t; want %t", c.in, c.ss, got, c.want)
			}
		})
	}
}


func (*String) HasPrefix(prefix S) bool

返回字符串是否包含指定前缀

查看 / 收起单元测试

func TestString_HasPrefix(t *testing.T) {
	var cases = []struct {
		name   string
		in     string
		prefix string
		want   bool
	}{{name: "case1", in: "hello", prefix: "h", want: true}, {name: "case2", in: "world", prefix: "w", want: true}, {name: "case3", in: "world", prefix: "x", want: false}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).HasPrefix(c.prefix)
			if got != c.want {
				t.Fatalf("NewString(%s).HasPrefix(%s) = %t; want %t", c.in, c.prefix, got, c.want)
			}
		})
	}
}


func (*String) HasSuffix(suffix S) bool

返回字符串是否包含指定后缀


func (*String) Len() int

返回字符串长度

查看 / 收起单元测试

func TestString_Len(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want int
	}{{name: "case1", in: "hello", want: 5}, {name: "case2", in: "world", want: 5}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Len()
			if got != c.want {
				t.Fatalf("NewString(%s).Len() = %d; want %d", c.in, got, c.want)
			}
		})
	}
}


func (*String) Contains(sub S) bool

返回字符串是否包含指定子串

查看 / 收起单元测试

func TestString_Contains(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		ss   string
		want bool
	}{{name: "case1", in: "hello", ss: "he", want: true}, {name: "case2", in: "world", ss: "or", want: true}, {name: "case3", in: "world", ss: "x", want: false}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Contains(c.ss)
			if got != c.want {
				t.Fatalf("NewString(%s).Contains(%s) = %t; want %t", c.in, c.ss, got, c.want)
			}
		})
	}
}


func (*String) Count(sub S) int

返回字符串包含指定子串的次数

查看 / 收起单元测试

func TestString_Count(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		ss   string
		want int
	}{{name: "case1", in: "hello", ss: "l", want: 2}, {name: "case2", in: "world", ss: "o", want: 1}, {name: "case3", in: "world", ss: "x", want: 0}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Count(c.ss)
			if got != c.want {
				t.Fatalf("NewString(%s).Count(%s) = %d; want %d", c.in, c.ss, got, c.want)
			}
		})
	}
}


func (*String) Repeat(count int) *String[S]

返回重复 count 次的字符串

查看 / 收起单元测试

func TestString_Repeat(t *testing.T) {
	var cases = []struct {
		name  string
		in    string
		count int
		want  string
	}{{name: "case1", in: "hello", count: 2, want: "hellohello"}, {name: "case2", in: "world", count: 3, want: "worldworldworld"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Repeat(c.count)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Repeat(%d) = %s; want %s", c.in, c.count, got, c.want)
			}
		})
	}
}


func (*String) Replace(old S, new S, n int) *String[S]

返回替换指定子串后的字符串

查看 / 收起单元测试

func TestString_Replace(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		old  string
		new  string
		want string
	}{{name: "case1", in: "hello", old: "l", new: "x", want: "hexxo"}, {name: "case2", in: "world", old: "o", new: "x", want: "wxrld"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Replace(c.old, c.new, -1)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Replace(%s, %s) = %s; want %s", c.in, c.old, c.new, got, c.want)
			}
		})
	}
}


func (*String) ReplaceAll(old S, new S) *String[S]

返回替换所有指定子串后的字符串

查看 / 收起单元测试

func TestString_ReplaceAll(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		old  string
		new  string
		want string
	}{{name: "case1", in: "hello", old: "l", new: "x", want: "hexxo"}, {name: "case2", in: "world", old: "o", new: "x", want: "wxrld"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).ReplaceAll(c.old, c.new)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).ReplaceAll(%s, %s) = %s; want %s", c.in, c.old, c.new, got, c.want)
			}
		})
	}
}


func (*String) Append(ss S) *String[S]

返回追加指定字符串后的字符串

查看 / 收起单元测试

func TestString_Append(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		ss   string
		want string
	}{{name: "case1", in: "hello", ss: " world", want: "hello world"}, {name: "case2", in: "world", ss: " hello", want: "world hello"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Append(c.ss)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Append(%s) = %s; want %s", c.in, c.ss, got, c.want)
			}
		})
	}
}


func (*String) Prepend(ss S) *String[S]

返回追加指定字符串后的字符串,追加的字符串在前

查看 / 收起单元测试

func TestString_Prepend(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		ss   string
		want string
	}{{name: "case1", in: "hello", ss: "world ", want: "world hello"}, {name: "case2", in: "world", ss: "hello ", want: "hello world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Prepend(c.ss)
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Prepend(%s) = %s; want %s", c.in, c.ss, got, c.want)
			}
		})
	}
}


func (*String) Clear() *String[S]

返回清空字符串后的字符串

查看 / 收起单元测试

func TestString_Clear(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: ""}, {name: "case2", in: "world", want: ""}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Clear()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Clear() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Reverse() *String[S]

返回反转字符串后的字符串

查看 / 收起单元测试

func TestString_Reverse(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "olleh"}, {name: "case2", in: "world", want: "dlrow"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Reverse()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Reverse() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Queto() *String[S]

返回带引号的字符串

查看 / 收起单元测试

func TestString_Queto(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "\"hello\""}, {name: "case2", in: "world", want: "\"world\""}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Queto()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Queto() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) QuetoToASCII() *String[S]

返回带引号的字符串

查看 / 收起单元测试

func TestString_QuetoToASCII(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "\"hello\""}, {name: "case2", in: "world", want: "\"world\""}, {name: "case3", in: "你好", want: "\"\\u4f60\\u597d\""}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).QuetoToASCII()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).QuetoToASCII() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) FirstUpper() *String[S]

返回首字母大写的字符串

查看 / 收起单元测试

func TestString_FirstUpper(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "Hello"}, {name: "case2", in: "world", want: "World"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).FirstUpper()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).FirstUpper() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) FirstLower() *String[S]

返回首字母小写的字符串

查看 / 收起单元测试

func TestString_FirstLower(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "Hello", want: "hello"}, {name: "case2", in: "World", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).FirstLower()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).FirstLower() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) SnakeCase() *String[S]

返回蛇形命名的字符串

查看 / 收起单元测试

func TestString_SnakeCase(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "HelloWorld", want: "hello_world"}, {name: "case2", in: "HelloWorldHello", want: "hello_world_hello"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).SnakeCase()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).SnakeCase() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) CamelCase() *String[S]

返回驼峰命名的字符串

查看 / 收起单元测试

func TestString_CamelCase(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello_world", want: "helloWorld"}, {name: "case2", in: "hello_world_hello", want: "helloWorldHello"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).CamelCase()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).CamelCase() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) KebabCase() *String[S]

返回短横线命名的字符串

查看 / 收起单元测试

func TestString_KebabCase(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "HelloWorld", want: "hello-world"}, {name: "case2", in: "HelloWorldHello", want: "hello-world-hello"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).KebabCase()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).KebabCase() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) TitleCase() *String[S]

返回标题命名的字符串

查看 / 收起单元测试

func TestString_TitleCase(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello_world", want: "HelloWorld"}, {name: "case2", in: "hello_world_hello", want: "HelloWorldHello"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).TitleCase()
			if got.String() != c.want {
				t.Fatalf("NewString(%s).TitleCase() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Bytes() []byte

返回字符串的字节数组

查看 / 收起单元测试

func TestString_Bytes(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "hello"}, {name: "case2", in: "world", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Bytes()
			if string(got) != c.want {
				t.Fatalf("NewString(%s).Bytes() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Runes() []rune

返回字符串的字符数组

查看 / 收起单元测试

func TestString_Runes(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "hello"}, {name: "case2", in: "world", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Runes()
			if string(got) != c.want {
				t.Fatalf("NewString(%s).Runes() = %v; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Default(def S) *String[S]

当字符串为空时设置默认值

查看 / 收起单元测试

func TestString_Default(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "", want: "default"}, {name: "case2", in: "world", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Default("default")
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Default() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Handle(f func ( S)) *String[S]

处理字符串

查看 / 收起单元测试

func TestString_Handle(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "hello"}, {name: "case2", in: "world", want: "world"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			var w string
			got := stream.NewString(c.in).Handle(func(s string) {
				w = s
			})
			if w != c.want {
				t.Fatalf("NewString(%s).Handle() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Update(f func ( S) S) *String[S]

更新字符串

查看 / 收起单元测试

func TestString_Update(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		want string
	}{{name: "case1", in: "hello", want: "HELLO"}, {name: "case2", in: "world", want: "WORLD"}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Update(func(s string) string {
				return stream.NewString(s).ToUpper().String()
			})
			if got.String() != c.want {
				t.Fatalf("NewString(%s).Update() = %s; want %s", c.in, got, c.want)
			}
		})
	}
}


func (*String) Split(sep string) *Strings[S]

返回字符串切片

查看 / 收起单元测试

func TestString_Split(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		sep  string
		want []string
	}{{name: "case1", in: "hello world", sep: " ", want: []string{"hello", "world"}}, {name: "case2", in: "hello,world", sep: ",", want: []string{"hello", "world"}}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Split(c.sep)
			for i, v := range got.Elem() {
				if v != c.want[i] {
					t.Fatalf("NewString(%s).Split(%s) = %v; want %v", c.in, c.sep, got, c.want)
				}
			}
		})
	}
}


func (*String) SplitN(sep string, n int) *Strings[S]

返回字符串切片

查看 / 收起单元测试

func TestString_SplitN(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		sep  string
		n    int
		want []string
	}{{name: "case1", in: "hello world", sep: " ", n: 2, want: []string{"hello", "world"}}, {name: "case2", in: "hello,world", sep: ",", n: 2, want: []string{"hello", "world"}}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).SplitN(c.sep, c.n)
			for i, v := range got.Elem() {
				if v != c.want[i] {
					t.Fatalf("NewString(%s).SplitN(%s, %d) = %v; want %v", c.in, c.sep, c.n, got, c.want)
				}
			}
		})
	}
}


func (*String) Batched(size int) *Strings[S]

将字符串按照指定长度分组,最后一组可能小于指定长度

查看 / 收起单元测试

func TestString_Batched(t *testing.T) {
	var cases = []struct {
		name string
		in   string
		size int
		want []string
	}{{name: "case1", in: "hello world", size: 5, want: []string{"hello", " worl", "d"}}, {name: "case2", in: "hello,world", size: 5, want: []string{"hello", ",worl", "d"}}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewString(c.in).Batched(c.size)
			for i, v := range got.Elem() {
				if v != c.want[i] {
					t.Fatalf("NewString(%s).Batched(%d) = %v; want %v", c.in, c.size, got, c.want)
				}
			}
		})
	}
}


Strings STRUCT

字符串切片

type Strings[S ~string] struct {
	s []S
}

func (*Strings) Elem() []S

返回原始元素

查看 / 收起单元测试

func TestStrings_Elem(t *testing.T) {
	var cases = []struct {
		name string
		in   []string
		want []string
	}{{name: "empty", in: []string{}, want: []string{}}, {name: "one", in: []string{"a"}, want: []string{"a"}}, {name: "two", in: []string{"a", "b"}, want: []string{"a", "b"}}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewStrings(c.in...).Elem()
			if len(got) != len(c.want) {
				t.Errorf("got %v, want %v", got, c.want)
			}
		})
	}
}


func (*Strings) Len() int

返回切片长度

查看 / 收起单元测试

func TestStrings_Len(t *testing.T) {
	var cases = []struct {
		name string
		in   []string
		want int
	}{{name: "empty", in: []string{}, want: 0}, {name: "one", in: []string{"a"}, want: 1}, {name: "two", in: []string{"a", "b"}, want: 2}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewStrings(c.in...)
			if got.Len() != c.want {
				t.Errorf("got %v, want %v", got, c.want)
			}
		})
	}
}


func (*Strings) Append(ss ...S) *Strings[S]

添加字符串

查看 / 收起单元测试

func TestStrings_Append(t *testing.T) {
	var cases = []struct {
		name   string
		in     []string
		append []string
		want   []string
	}{{name: "empty", in: []string{}, append: []string{"a"}, want: []string{"a"}}, {name: "one", in: []string{"a"}, append: []string{"b"}, want: []string{"a", "b"}}, {name: "two", in: []string{"a", "b"}, append: []string{"c"}, want: []string{"a", "b", "c"}}}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			got := stream.NewStrings(c.in...).Append(c.append...)
			if got.Len() != len(c.want) {
				t.Errorf("got %v, want %v", got, c.want)
			}
		})
	}
}


func (*Strings) Join(sep S) *String[S]

连接字符串


func (*Strings) Choice(i int) *String[S]

选择字符串


func (*Strings) Choices(i ...int) *Strings[S]

选择多个字符串


func (*Strings) ChoiceInRange(start int, end int) *Strings[S]

选择范围内的字符串


func (*Strings) Remove(i int) *Strings[S]

移除字符串


func (*Strings) Removes(i ...int) *Strings[S]

移除多个字符串


func (*Strings) RemoveInRange(start int, end int) *Strings[S]

移除范围内的字符串


func (*Strings) Clear() *Strings[S]

清空字符串


func (*Strings) First() *String[S]

第一个字符串


func (*Strings) Last() *String[S]

最后一个字符串


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type String added in v0.5.3

type String[S ~string] struct {
	// contains filtered or unexported fields
}

String 字符串流

func NewString added in v0.5.3

func NewString[S ~string](s S) *String[S]

NewString 创建字符串流

func (*String[S]) Append added in v0.5.3

func (s *String[S]) Append(ss S) *String[S]

Append 返回追加指定字符串后的字符串

func (*String[S]) Batched added in v0.5.3

func (s *String[S]) Batched(size int) *Strings[S]

Batched 将字符串按照指定长度分组,最后一组可能小于指定长度

func (*String[S]) Bytes added in v0.5.3

func (s *String[S]) Bytes() []byte

Bytes 返回字符串的字节数组

func (*String[S]) CamelCase added in v0.5.3

func (s *String[S]) CamelCase() *String[S]

CamelCase 返回驼峰命名的字符串

func (*String[S]) Clear added in v0.5.3

func (s *String[S]) Clear() *String[S]

Clear 返回清空字符串后的字符串

func (*String[S]) Contains added in v0.5.3

func (s *String[S]) Contains(sub S) bool

Contains 返回字符串是否包含指定子串

func (*String[S]) Count added in v0.5.3

func (s *String[S]) Count(sub S) int

Count 返回字符串包含指定子串的次数

func (*String[S]) Default added in v0.5.3

func (s *String[S]) Default(def S) *String[S]

Default 当字符串为空时设置默认值

func (*String[S]) Elem added in v0.5.3

func (s *String[S]) Elem() S

Elem 返回原始元素

func (*String[S]) Equal added in v0.5.3

func (s *String[S]) Equal(ss S) bool

Equal 返回字符串是否相等

func (*String[S]) FirstLower added in v0.5.3

func (s *String[S]) FirstLower() *String[S]

FirstLower 返回首字母小写的字符串

func (*String[S]) FirstUpper added in v0.5.3

func (s *String[S]) FirstUpper() *String[S]

FirstUpper 返回首字母大写的字符串

func (*String[S]) Handle added in v0.5.3

func (s *String[S]) Handle(f func(S)) *String[S]

Handle 处理字符串

func (*String[S]) HasPrefix added in v0.5.3

func (s *String[S]) HasPrefix(prefix S) bool

HasPrefix 返回字符串是否包含指定前缀

func (*String[S]) HasSuffix added in v0.5.3

func (s *String[S]) HasSuffix(suffix S) bool

HasSuffix 返回字符串是否包含指定后缀

func (*String[S]) Index added in v0.5.3

func (s *String[S]) Index(i int) *String[S]

Index 返回字符串指定位置的字符,当索引超出范围时将会触发 panic

func (*String[S]) KebabCase added in v0.5.3

func (s *String[S]) KebabCase() *String[S]

KebabCase 返回短横线命名的字符串

func (*String[S]) Len added in v0.5.3

func (s *String[S]) Len() int

Len 返回字符串长度

func (*String[S]) Prepend added in v0.5.3

func (s *String[S]) Prepend(ss S) *String[S]

Prepend 返回追加指定字符串后的字符串,追加的字符串在前

func (*String[S]) Queto added in v0.5.3

func (s *String[S]) Queto() *String[S]

Queto 返回带引号的字符串

func (*String[S]) QuetoToASCII added in v0.5.3

func (s *String[S]) QuetoToASCII() *String[S]

QuetoToASCII 返回带引号的字符串

func (*String[S]) Range added in v0.5.3

func (s *String[S]) Range(start, end int) *String[S]

Range 返回字符串指定范围的字符

func (*String[S]) Repeat added in v0.5.3

func (s *String[S]) Repeat(count int) *String[S]

Repeat 返回重复 count 次的字符串

func (*String[S]) Replace added in v0.5.3

func (s *String[S]) Replace(old, new S, n int) *String[S]

Replace 返回替换指定子串后的字符串

func (*String[S]) ReplaceAll added in v0.5.3

func (s *String[S]) ReplaceAll(old, new S) *String[S]

ReplaceAll 返回替换所有指定子串后的字符串

func (*String[S]) Reverse added in v0.5.3

func (s *String[S]) Reverse() *String[S]

Reverse 返回反转字符串后的字符串

func (*String[S]) Runes added in v0.5.3

func (s *String[S]) Runes() []rune

Runes 返回字符串的字符数组

func (*String[S]) SnakeCase added in v0.5.3

func (s *String[S]) SnakeCase() *String[S]

SnakeCase 返回蛇形命名的字符串

func (*String[S]) Split added in v0.5.3

func (s *String[S]) Split(sep string) *Strings[S]

Split 返回字符串切片

func (*String[S]) SplitN added in v0.5.3

func (s *String[S]) SplitN(sep string, n int) *Strings[S]

SplitN 返回字符串切片

func (*String[S]) String added in v0.5.3

func (s *String[S]) String() string

String 返回字符串

func (*String[S]) TitleCase added in v0.5.3

func (s *String[S]) TitleCase() *String[S]

TitleCase 返回标题命名的字符串

func (*String[S]) ToLower added in v0.5.3

func (s *String[S]) ToLower() *String[S]

ToLower 返回字符串的小写形式

func (*String[S]) ToUpper added in v0.5.3

func (s *String[S]) ToUpper() *String[S]

ToUpper 返回字符串的大写形式

func (*String[S]) Trim added in v0.5.3

func (s *String[S]) Trim(cs string) *String[S]

Trim 返回去除字符串首尾指定字符的字符串

func (*String[S]) TrimPrefix added in v0.5.3

func (s *String[S]) TrimPrefix(prefix string) *String[S]

TrimPrefix 返回去除字符串前缀的字符串

func (*String[S]) TrimSpace added in v0.5.3

func (s *String[S]) TrimSpace() *String[S]

TrimSpace 返回去除字符串首尾空白字符的字符串

func (*String[S]) TrimSuffix added in v0.5.3

func (s *String[S]) TrimSuffix(suffix string) *String[S]

TrimSuffix 返回去除字符串后缀的字符串

func (*String[S]) Update added in v0.5.3

func (s *String[S]) Update(f func(S) S) *String[S]

Update 更新字符串

type Strings added in v0.5.3

type Strings[S ~string] struct {
	// contains filtered or unexported fields
}

Strings 字符串切片

func NewStrings added in v0.5.3

func NewStrings[S ~string](s ...S) *Strings[S]

NewStrings 创建字符串切片

func (*Strings[S]) Append added in v0.5.3

func (s *Strings[S]) Append(ss ...S) *Strings[S]

Append 添加字符串

func (*Strings[S]) Choice added in v0.5.4

func (s *Strings[S]) Choice(i int) *String[S]

Choice 选择字符串

func (*Strings[S]) ChoiceInRange added in v0.5.4

func (s *Strings[S]) ChoiceInRange(start, end int) *Strings[S]

ChoiceInRange 选择范围内的字符串

func (*Strings[S]) Choices added in v0.5.4

func (s *Strings[S]) Choices(i ...int) *Strings[S]

Choices 选择多个字符串

func (*Strings[S]) Clear added in v0.5.3

func (s *Strings[S]) Clear() *Strings[S]

Clear 清空字符串

func (*Strings[S]) Elem added in v0.5.4

func (s *Strings[S]) Elem() []S

Elem 返回原始元素

func (*Strings[S]) First added in v0.5.3

func (s *Strings[S]) First() *String[S]

First 第一个字符串

func (*Strings[S]) Join added in v0.5.4

func (s *Strings[S]) Join(sep S) *String[S]

Join 连接字符串

func (*Strings[S]) Last added in v0.5.3

func (s *Strings[S]) Last() *String[S]

Last 最后一个字符串

func (*Strings[S]) Len added in v0.5.3

func (s *Strings[S]) Len() int

Len 返回切片长度

func (*Strings[S]) Remove added in v0.5.4

func (s *Strings[S]) Remove(i int) *Strings[S]

Remove 移除字符串

func (*Strings[S]) RemoveInRange added in v0.5.4

func (s *Strings[S]) RemoveInRange(start, end int) *Strings[S]

RemoveInRange 移除范围内的字符串

func (*Strings[S]) Removes added in v0.5.4

func (s *Strings[S]) Removes(i ...int) *Strings[S]

Removes 移除多个字符串

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL