Skip to content

strconv

公式ドキュメント:strconv package - strconv - Go Packages

strconv パッケージは基本データ型の文字列表現との間の変換を実装します。

インポート

go
import (
  "strconv"
)

以下では例を使用して使用方法を示します。

文字列から整数への変換

go
func Atoi(s string) (int, error)
  • s - 変換する文字列
go
func TestAoti(t *testing.T) {
  ints, err := strconv.Atoi("456789")
  fmt.Println(ints, err)
}
=== RUN   TestAoti
456789 <nil>
--- PASS: TestAoti (0.00s)
PASS

整数から文字列への変換

go
func Itoa(i int) string
  • i - 変換する整数
go
func TestIota(t *testing.T) {
   str := strconv.Itoa(114)
   fmt.Println(str)
}
=== RUN   TestIota
114
--- PASS: TestIota (0.00s)
PASS

文字列からブール値への変換

go
func ParseBool(str string) (bool, error)
  • s - 変換する文字列

変換可能な文字列は以下の通りです。

"1", "t", "T", "true", "TRUE", "True" // true
"0", "f", "F", "false", "FALSE", "False" // false
go
func TestAtob(t *testing.T) {
   parseBool, err := strconv.ParseBool("1")
   fmt.Println(parseBool, err)

   b, err := strconv.ParseBool("true")
   fmt.Println(b, err)

   b2, err := strconv.ParseBool("FALSE")
   fmt.Println(b2, err)
}
=== RUN   TestAotb
true <nil>
true <nil>
false <nil>
--- PASS: TestAotb (0.00s)
PASS

ブール値から文字列への変換

go
func FormatBool(b bool) string
  • b - ブール値
go
func TestBota(t *testing.T) {
   fmt.Println(strconv.FormatBool(true))
   fmt.Println(strconv.FormatBool(false))
}
=== RUN   TestBota
true
false
--- PASS: TestBota (0.00s)
PASS

Go 文字列への変換

両方とも文字列を引用符付きの Go 文字列に変換します。違いは、後者が非 ASCII 文字を \u でエスケープすることです。

go
func TestQuote(t *testing.T) {
  fmt.Println(strconv.Quote("hello 世界"))
  fmt.Println(strconv.QuoteToASCII("hello 世界"))
}
=== RUN   TestQuote
"hello 世界"
"hello \u4e16\u754c"
--- PASS: TestQuote (0.00s)
PASS

文字列から浮動小数点数への変換

go
func ParseFloat(s string, bitSize int) (float64, error)
  • s - 変換する文字列
  • bitSize - ビットサイズ
go
func TestParseFloat(t *testing.T) {
   float, err := strconv.ParseFloat("1.145114", 64)
   fmt.Println(float, err)

   float, err = strconv.ParseFloat("2.3333333333333333333", 64)
   fmt.Println(float, err)
}
=== RUN   TestFloat
1.145114 <nil>
2.3333333333333335 <nil>
--- PASS: TestFloat (0.00s)
PASS

浮動小数点数から文字列への変換

文字列を変換する際、公式は異なるスタイルを出力できるようにいくつかのフォーマット方法を提供しています。

// 'b' (-ddddp±ddd、2 進指数)、
// 'e' (-d.dddde±dd、小文字 e の 10 進指数)、
// 'E' (-d.ddddE±dd、大文字 E の 10 進指数)、
// 'f' (-ddd.dddd、指数なし)、// 特別な要件がない場合は通常これを使用
// 'g' (大きな指数には'e'形式、小さな指数には'f'形式を使用)、
// 'G' (大きな指数には'E'形式、小さな指数には'f'形式を使用)、
// 'x' (-0xd.ddddp±ddd、16 進分数と 2 進指数)、
// 'X' (-0Xd.ddddP±ddd、16 進分数と 2 進指数)。

変換関数

go
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
  • f - 変換する浮動小数点数
  • fmt - フォーマットタイプ
  • prec - 精度。g/G の場合を除き、最大有効桁数を示します。それ以外の場合は小数点以下何桁までを保持するかを示します。
  • bitSize - ビットサイズ

通常は f を使用して小数形式で変換することが最も多いです。

go
func TestFormatFloat(t *testing.T) {
   f := 1315643.14159261234567891011
   fmt.Println(strconv.FormatFloat(f, 'f', 6, 64))
   fmt.Println(strconv.FormatFloat(f, 'b', 6, 64))
   fmt.Println(strconv.FormatFloat(f, 'e', 6, 64))
   fmt.Println(strconv.FormatFloat(f, 'x', 6, 64))
   fmt.Println(strconv.FormatFloat(f, 'g', 6, 64))
   fmt.Println(strconv.FormatFloat(1.111, 'g', 6, 64))
}
=== RUN   TestFormatFloat
1315643.141593
5650644266346967p-32
1.315643e+06
0x1.4133b2p+20
1.31564e+06
1.111
--- PASS: TestFormatFloat (0.00s)
PASS

文字列から複素数への変換

go
func ParseComplex(s string, bitSize int) (complex128, error)
  • s - 変換する文字列
  • bitSize - ビットサイズ
go
func TestParseComplex(t *testing.T) {
   fmt.Println(strconv.ParseComplex("1+2i", 128))
   fmt.Println(strconv.ParseComplex("1+2j", 128))
}
=== RUN   TestParseComplex
(1+2i) <nil>
(0+0i) strconv.ParseComplex: parsing "1+2j": invalid syntax
--- PASS: TestParseComplex (0.00s)
PASS

複素数から文字列への変換

go
func FormatComplex(c complex128, fmt byte, prec, bitSize int) string

c - 複素数

fmt - フォーマットタイプ。浮動小数点数のフォーマットタイプを参照

prec - 浮動小数点数の精度を参照

bitSize - ビットサイズ

go
func TestFormatComplex(t *testing.T) {
   fmt.Println(strconv.FormatComplex(complex(1.1, 12), 'f', 2, 128))
   fmt.Println(strconv.FormatComplex(complex(5.6, 2.8), 'b', 2, 128))
   fmt.Println(strconv.FormatComplex(complex(18.88999, 89.7), 'g', 2, 128))
}
=== RUN   TestFormatComplex
(1.10+12.00i)
(6305039478318694p-50+6305039478318694p-51i)
(19+90i)
--- PASS: TestFormatComplex (0.00s)
PASS

文字列へのデータ追加

Java などの他の言語では、"1"+1 の結果は "11" になります。Java は自動的に型変換を実行しますが、Go 言語ではこのような操作は許可されていません。データ型が異なるためです。そのため、strconv の Append 関数を使用する必要があります。具体的なパラメータは上記のデータ変換関数と同じです。

go
func TestAppend(t *testing.T) {
   bytes := []byte("ここにいくつかのデータがあります:")
   bytes = strconv.AppendInt(bytes, 10, 10)
   bytes = strconv.AppendFloat(bytes, 1.2222, 'f', 2, 64)
   bytes = strconv.AppendBool(bytes, false)
   fmt.Println(string(bytes))
}
=== RUN   TestAppend
ここにいくつかのデータがあります:101.22false
--- PASS: TestAppend (0.00s)
PASS

Golang学习网由www.golangdev.cn整理维护