0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Goの文字列操作でよく使う関数

Posted at

Goの文字列操作でよく使う関数を記載しています。

文字列の結合

strings.Join([]string{"I'm","love in it"}, " ") // "I'm love in it"

文字列のカウント

strings.Count("Mac Mac Mac Mos", "Mac") // 3

部分文字列の検索

strings.Contains("Macdonald", "Mac") // true

// 指定した文字列のどれかが含まれるか
strings.ContainsAny("Macdonald", "Md") // true

// Prefixで検索
strings.HasPrefix("Macdonald", "Mac") // true

// Suffixで検索
strings.HasSuffix("Macdonald", "donald") // true

文字列の置換

strings.Replace("I love Mac", "Mac", "Mos", 1) // "I love Mos"
strings.Replace("MacMacMac", "Mac", "Mos", 2) // "MosMosMac"
strings.Replace("MacMacMac", "Mac", "Mos", -1) // "MosMosMos"

文字列の分割

strings.Split("Mos,Mac,BergerKing,Lotteria,KFC", ",") // []string{"Mos", "Mac", "BergerKing", "Lotteria", "KFC"}

大文字/小文字変換

strings.ToLower("MACDONALD") // "macdonald"
strings.ToUpper("macdonald") // "MACDONALD"

空白トリム

strings.TrimSpace("  Macdonald . ") // "Macdonald ."
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?