Skip to content

Commit

Permalink
添加04.5.2,04.5.3及其相应代码
Browse files Browse the repository at this point in the history
  • Loading branch information
hantmac committed Jan 20, 2019
1 parent c199d38 commit 42833ea
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 0 deletions.
50 changes: 50 additions & 0 deletions eBook/chapter4/04.5.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# **关于Unicode的包**

Go提供的`unicode`标准库提供了很多便捷的函数,其中`unicode.IsPrint()`能帮助你判断字符串的某一部分是否能够以rune的类型打印出来。接下来将会在代码`unicode.go`中分两部分展示该函数的用法。

第一部分:

> ```go
> package main
>
> import (
> "fmt"
> "unicode"
> )
>
> func main() {
> const sL= "\x99\x00ab\x50\x00\x23\x50\x29\x9c"
> ```
第二部分:
> ```go
> for i := 0; i < len(sL); i++ {
> if unicode.IsPrint(rune(sL[i])) {
> fmt.Printf("%c\n", sL[i])
> } else {
> fmt.Println("Not printable!")
> }
> }
> }
> ```
`unicode.IsPrint()`函数将检查字符串`sL`的每个元素是否是rune类型,如果是的话将返回`true`否则返回false。如果你需要更多操作Unicode字符的方法,可以参考官方`unicode`包的介绍。
执行`unicode.go`将会打印:
> $ go run unicode.go
>
> ```shell
> Not printable!
> Not printable!
> a
> b
> P
> Not printable!
> #
> P
> )
> Not printable!
> ```
144 changes: 144 additions & 0 deletions eBook/chapter4/04.5.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# **关于字符串处理的包**

Go在`strings`包里面提供了很多操作UTF-8字符串的强大工具,大部分的工具将会在`useStrings.go`介绍,与文件IO相关的 操作将会在*第8章*详细介绍。

`useStrings.go`的第一部分代码:

> ```go
> package main
>
> import (
> "fmt"
> s "strings"
> "unicode"
> )
>
> var f = fmt.Printf
> ```
> `s "strings"`的导包方式是非常推荐的,意味着我们给strings包起了一个简短的包名,这样就可以使用`s.FunctionName()`而不必使用`strings.FunctionName()`
当你频繁使用某个包中的函数的时候,也可以将该函数赋值给一个变量,像上面的代码一样,`var f = fmt.Printf`,接下来就可以用`f`代替`fmt.Printf()`。要注意的是不要过度使用该特性,不然你的代码可读性会变得很差。
第二部分代码:
> ```go
> unc main() {
> upper := s.ToUpper("Hello there!")
> f("To Upper: %s\n", upper)
> f("To Lower: %s\n", s.ToLower("Hello THERE"))
>
> f("%s\n", s.Title("tHis wiLL be A title!"))
>
> f("EqualFold: %v\n", s.EqualFold("Mihalis", "MIHAlis"))
> f("EqualFold: %v\n", s.EqualFold("Mihalis", "MIHAli"))
> ```
这段代码提供了很多玩转字符串的例子,`strings.EqualFold()`函数能够判断两个字符串是否相同。
第三部分代码:
> ```go
> f("Prefix: %v\n", s.HasPrefix("Mihalis", "Mi"))
> f("Prefix: %v\n", s.HasPrefix("Mihalis", "mi"))
> f("Suffix: %v\n", s.HasSuffix("Mihalis", "is"))
> f("Suffix: %v\n", s.HasSuffix("Mihalis", "IS"))
>
> f("Index: %v\n", s.Index("Mihalis", "ha"))
> f("Index: %v\n", s.Index("Mihalis", "Ha"))
> f("Count: %v\n", s.Count("Mihalis", "i"))
> f("Count: %v\n", s.Count("Mihalis", "I"))
> f("Repeat: %s\n", s.Repeat("ab", 5))
>
> f("TrimSpace: %s\n", s.TrimSpace(" \tThis is a line. \n"))
> f("TrimLeft: %s", s.TrimLeft(" \tThis is a\t line. \n", "\n\t "))
> f("TrimRight: %s\n", s.TrimRight(" \tThis is a\t line. \n", "\n\t "))
> ```
`strings.Count()`函数能够计算第二个参数(字符串类型),在第一个参数(字符串类型)中出现的次数;`strings.HasPrefix()`函数判断字符串是否是以某字符串开头,如果是的话便返回`true`;同样,`strings.HasSuffix()`判断字符串是否以某字符串结尾。
第四部分代码:
> ```go
> f("Compare: %v\n", s.Compare("Mihalis", "MIHALIS"))
> f("Compare: %v\n", s.Compare("Mihalis", "Mihalis"))
> f("Compare: %v\n", s.Compare("MIHALIS", "MIHalis"))
>
> f("Fields: %v\n", s.Fields("This is a string!"))
> f("Fields: %v\n", s.Fields("Thisis\na\tstring!"))
>
> f("%s\n", s.Split("abcd efg", ""))
> f("%s\n", s.Replace("abcd efg", "", "_", -1))
> f("%s\n", s.Replace("abcd efg", "", "_", 4))
> f("%s\n", s.Replace("abcd efg", "", "_", 2))
> ```
这部分展示了`strings`比较高级的一些函数。`strings.Split()`能够以特定字符分割字符串,并返回一个**字符串切片**。
`strings.Compare()`函数用于比较两个字符串是否相等,相等返回`true`,否则返回1或者-1(strings.Compare(a,b),如果a<b则返回1,a<b返回-1,所谓a>b,意思是出现在字母表中的顺序,译者加)。
`strings.Fields()`按照空格将字符串分割。
> `strings.Split()`非常有用,你迟早会在自己的程序中用到它!
最后一部分代码:
> ```go
> lines := []string{"Line 1", "Line 2", "Line 3"}
> f("Join: %s\n", s.Join(lines, "+++"))
>
> f("SplitAfter: %s\n", s.SplitAfter("123++432++", "++"))
>
> trimFunction := func(c rune) bool {
> return !unicode.IsLetter(c)
> }
> f("TrimFunc: %s\n", s.TrimFunc("123 abc ABC \t .", trimFunction))
> }
> ```
最后一部分代码提供的方法非常好理解,而且功能强大:
`strings.Replace()`函数需要4个参数,第一个是你要处理的字符串,第二个是你准备替换的字符,第三个是你要用该字符去替换,第四个参数是你要替换的数量,如果是`-1`就意味着你会替换所有要替换的字符。
`strings.TrimFunc()`函数可按照自定义函数获取你感兴趣的内容;
`strings.SplitAfter()`函数基于指定分隔符将字符串分割。
执行`useStrings.go`的输出如下:
> $ go run useStrings.go
>
> ```shell
> To Upper: HELLO THERE!
> To Lower: hello there
> THis WiLL Be A Title!
> EqualFold: true
> EqualFold: false
> Prefix: true
> Prefix: false
> Suffix: true
> Suffix: false
> Index: 2
> Index: -1
> Count: 2
> Count: 0
> Repeat: ababababab
> TrimSpace: This is a line.
> TrimLeft: This is a line.
> TrimRight: This is a line.
> Compare: 1
> Compare: 0
> Compare: -1
> Fields: [This is a string!]
> Fields: [Thisis a string!]
> [a b c d e f g]
> _a_b_c_d_ _e_f_g_
> _a_b_c_d efg
> _a_bcd efg
> Join: Line 1+++Line 2+++Line 3
> SplitAfter: [123++ 432++ ]
> TrimFunc: abc ABC
> ```
官方`strings`包中的方法远比`useStrings.go`中所展示的多,如果你正在写的代码是关于文本处理的,那么你应该阅读[]`strings`的官方文档](https://golang.org/pkg/strings)以了解更多。
18 changes: 18 additions & 0 deletions eBook/examples/chapter4/unicode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"
"unicode"
)

func main() {
const sL= "\x99\x00ab\x50\x00\x23\x50\x29\x9c"

for i := 0; i < len(sL); i++ {
if unicode.IsPrint(rune(sL[i])) {
fmt.Printf("%c\n", sL[i])
} else {
fmt.Println("Not printable!")
}
}
}
57 changes: 57 additions & 0 deletions eBook/examples/chapter4/useStrings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
s "strings"
"unicode"
)

var f = fmt.Printf

func main() {
upper := s.ToUpper("Hello there!")
f("To Upper: %s\n", upper)
f("To Lower: %s\n", s.ToLower("Hello THERE"))

f("%s\n", s.Title("tHis wiLL be A title!"))

f("EqualFold: %v\n", s.EqualFold("Mihalis", "MIHAlis"))
f("EqualFold: %v\n", s.EqualFold("Mihalis", "MIHAli"))

f("Prefix: %v\n", s.HasPrefix("Mihalis", "Mi"))
f("Prefix: %v\n", s.HasPrefix("Mihalis", "mi"))
f("Suffix: %v\n", s.HasSuffix("Mihalis", "is"))
f("Suffix: %v\n", s.HasSuffix("Mihalis", "IS"))

f("Index: %v\n", s.Index("Mihalis", "ha"))
f("Index: %v\n", s.Index("Mihalis", "Ha"))
f("Count: %v\n", s.Count("Mihalis", "i"))
f("Count: %v\n", s.Count("Mihalis", "I"))
f("Repeat: %s\n", s.Repeat("ab", 5))

f("TrimSpace: %s\n", s.TrimSpace(" \tThis is a line. \n"))
f("TrimLeft: %s", s.TrimLeft(" \tThis is a\t line. \n", "\n\t "))
f("TrimRight: %s\n", s.TrimRight(" \tThis is a\t line. \n", "\n\t "))

f("Compare: %v\n", s.Compare("Mihalis", "MIHALIS"))
f("Compare: %v\n", s.Compare("Mihalis", "Mihalis"))
f("Compare: %v\n", s.Compare("MIHALIS", "MIHalis"))

f("Fields: %v\n", s.Fields("This is a string!"))
f("Fields: %v\n", s.Fields("Thisis\na\tstring!"))

f("%s\n", s.Split("abcd efg", ""))
f("%s\n", s.Replace("abcd efg", "", "_", -1))
f("%s\n", s.Replace("abcd efg", "", "_", 4))
f("%s\n", s.Replace("abcd efg", "", "_", 2))

lines := []string{"Line 1", "Line 2", "Line 3"}
f("Join: %s\n", s.Join(lines, "+++"))

f("SplitAfter: %s\n", s.SplitAfter("123++432++", "++"))

trimFunction := func(c rune) bool {
return !unicode.IsLetter(c)
}
f("TrimFunc: %s\n", s.TrimFunc("123 abc ABC \t .", trimFunction))
}

0 comments on commit 42833ea

Please sign in to comment.