Created
January 12, 2018 03:30
-
-
Save sekky0905/e2061f65b9ab097ec35864f8db33bb95 to your computer and use it in GitHub Desktop.
Goで文字列の置き換えをする(strings packageのReplace) ref: https://qiita.com/Sekky0905/items/f0bed43ad3ab4be13385
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
str := "Go Java JavaScript Typescript CofeeScript" | |
// 普通に表示 | |
fmt.Printf("str : %s\n", str) | |
// 最初の"Java"という文字列だけ置き換え | |
replaced1 := strings.Replace(str, "Java", "Replaced", 1) | |
fmt.Printf("replaced1 : %s\n", replaced1) | |
// 全部のJavaを置き換え | |
replaced2 := strings.Replace(str, "Java", "Replaced", -1) | |
fmt.Printf("replaced2 : %s\n", replaced2) | |
// 全部のScriptを置き換え | |
// Typescriptの"script"は小文字のため、変更されない | |
replaced3 := strings.Replace(str, "Script", "Replaced", -1) | |
fmt.Printf("replaced3 : %s\n", replaced3) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
str : Go Java JavaScript Typescript CofeeScript | |
replaced1 : Go Replaced JavaScript Typescript CofeeScript | |
replaced2 : Go Replaced ReplacedScript Typescript CofeeScript | |
replaced3 : Go Java JavaReplaced Typescript CofeeReplaced |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment