はじめに

前回の記事の続きです。
前回の記事はこちら

テキスト処理

前回同様テキスト処理についてやっていきます。
今回はファイルの操作をやっていきましょう。

文字列の操作

文字列操作についてやっていきます。
今回は、実際にやってみた内容だけ記載いたします。

func Contains(s, substr string) bool

1
2
3
test := "applelemonapplemon"
fmt.Println(strings.Contains(test, "apple"))
// true

func Join(a []string, sep string) string

1
2
3
test2 := []string{"apple", "lemon", "melon"}
fmt.Println(strings.Join(test2, ","))
// apple,lemon,melon

func Index(s, sep string) int

1
2
3
test3 := "applelemonapplemon"
fmt.Println(strings.Index(test3, "apple"))
// 0

func Repeat(s string, count int) string

1
2
fmt.Println(strings.Repeat("apple", 3))
// appleappleapple

func Replace(s, old, new string, n int) string

1
2
3
test4 := "applelemonapple"
fmt.Println(strings.Replace(test4, "apple", "", -1))
// lemon

func Split(s, sep string) []string

1
2
3
test5 := "test,test1,test2"
fmt.Println(strings.Split(test5, ","))
// [test test1 test2]

func Trim(s string, cutset string) string

1
2
3
4
5
test6 := " aaatest, spaces "
fmt.Println(test6)
fmt.Println(strings.Trim(test6, " "))
//  aaatest, spaces
//aaatest, spaces

func Fields(s string) []string

1
2
3
test7 := "a b c d e f"
fmt.Println(strings.Fields(test7))
// [a b c d e f]

文字列の変換

Append系

Append シリーズの関数は整数などを文字列に変換した後、現在のバイト列に追加します。

1
2
3
4
5
6
7
str := make([]byte, 0, 100)
str = strconv.AppendInt(str, 4567, 10)
str = strconv.AppendBool(str, false)
str = strconv.AppendQuote(str, "abcdefg")
str = strconv.AppendQuoteRune(str, '单')
fmt.Println(string(str))
// 4567false"abcdefg"'单'

Format系

1
2
3
4
5
6
7
a := strconv.FormatBool(false)
b := strconv.FormatFloat(123.23, 'g', 12, 64)
c := strconv.FormatInt(1234, 10)
d := strconv.FormatUint(12345, 10)
e := strconv.Itoa(1023)
fmt.Println(a, b, c, d, e)
// false 123.23 1234 12345 1023

Parse系

1
2
3
4
5
6
7
a, err := strconv.ParseBool("false")
b, err := strconv.ParseFloat("123.23", 64)
c, err := strconv.ParseInt("1234", 10, 64)
d, err := strconv.ParseUint("12345", 10, 64)
e, err := strconv.Atoi("1023")
fmt.Println(a, b, c, d, e)
// false 123.23 1234 12345 1023

全部変換する系ですね。

最後にここまでで使用したコードのまとめを載せます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main

import (
	"fmt"
	"strconv"
	"strings"
)

func main() {
	test := "applelemonapplemon"
	fmt.Println(strings.Contains(test, "apple"))

	test2 := []string{"apple", "lemon", "melon"}
	fmt.Println(strings.Join(test2, ","))

	test3 := "applelemonapplemon"
	fmt.Println(strings.Index(test3, "apple"))

	fmt.Println(strings.Repeat("apple", 3))

	test4 := "applelemonapple"
	fmt.Println(strings.Replace(test4, "apple", "", -1))

	test5 := "test,test1,test2"
	fmt.Println(strings.Split(test5, ","))

	test6 := " aaatest, spaces "
	fmt.Println(test6)
	fmt.Println(strings.Trim(test6, " "))

	test7 := "a b c d e f"
	fmt.Println(strings.Fields(test7))

	str := make([]byte, 0, 100)
	str = strconv.AppendInt(str, 4567, 10)
	str = strconv.AppendBool(str, false)
	str = strconv.AppendQuote(str, "abcdefg")
	str = strconv.AppendQuoteRune(str, '单')
	fmt.Println(string(str))

	a := strconv.FormatBool(false)
	b := strconv.FormatFloat(123.23, 'g', 12, 64)
	c := strconv.FormatInt(1234, 10)
	d := strconv.FormatUint(12345, 10)
	e := strconv.Itoa(1023)
	fmt.Println(a, b, c, d, e)

	f, _ := strconv.ParseBool("false")
	g, _ := strconv.ParseFloat("123.23", 64)
	h, _ := strconv.ParseInt("1234", 10, 64)
	i, _ := strconv.ParseUint("12345", 10, 64)
	j, _ := strconv.Atoi("1023")
	fmt.Println(f, g, h, i, j)
}

おわりに

次回は「Webサービス」です。