[Golang] Check if a specific character string is included in the character string

Overview

Make a note of how to check if a variable like the one below contains a particular string

package main

func main(){
  str := "abcde"
}

Basically you will use the strings package, but there are two ways

strings.Index

It is a method to check the character number of a specific character string, but if the specific character string is not included, it returns -1, so use that.

package main

import (
  "fmt"
  "strings"
)

func main(){
  str := "abcde"
  fmt.Print(strings.Index(str, "fg"))
  //result:-1
}

strings.Contains

I introduced strings.Index, but I think that I will basically use this because it will be returned as a bool.

package main

import (
  "fmt"
  "strings"
)

func main(){
  str := "abcde"
  fmt.Print(strings.Contains(str, "fg"))
  //Result: false
}

Recommended Posts

[Golang] Check if a specific character string is included in the character string
Check if the string is a number in python
Delete a particular character in Python if it is the last
Check if there is a specific symbol in the executable file and its dependent libraries (simplified version)
Check if it is Unix in the scripting language
Check if it is Unix in the scripting language
[Python] Leave only the elements that start with a specific character string in the array
How to check in Python if one of the elements of a list is in another list
Find out how many each character is in the string.
Is it a character string operation?
Determine if the string is formatable
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
[Blender] Script to check if the selected one is a mesh
Check if the configuration file is read in an easy-to-understand manner
Checks if there is a specific character string for all files under the directory that is Python and outputs the target line
Check if the URL exists in Python
I made a function to check if the webhook is received in Lambda for the time being
If you get a no attribute error in boto3, check the version
How to input a character string in Python and output it as it is or in the opposite direction.
Check if the characters are similar in Python
Extract lines containing a specific "string" in Pandas
Note) Batch conversion of specific symbols contained in a character string with a dictionary
[For beginners] Unexpected behavior if "\" is included when setting the path in Python
I created a script to check if English is entered in the specified position of the JSON file in Python.
Create a python script to check if the link at the specified URL is valid 2
Get the variable name of the variable as a character string.
Check if the expected column exists in Pandas DataFrame
Create a python script to check if the link at the specified URL is valid
Codeforces Educational Codeforces Round 101 A. Regular Bracket Sequence Check the parentheses and? String in DP
[Python] How to expand variables in a character string
How to use any or all to check if it is in a dictionary (Hash)
# Function that returns the character code of a string
Determine if an attribute is defined in the object
Output a character string with line breaks in PyYAML
Check if the LAN cable is disconnected on Linux
python> check NoneType or not> if a == None:> if a is None:
[Golang] Command to check the supported GOOS and GOARCH in a list (Check the supported platforms of the build)
When reading an image with SimpleITK, there is a problem if there is Japanese in the path
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
When converting a string (a-> b) in Python, which is faster, if statement or dictionary type?
How to quickly count the frequency of appearance of characters from a character string in Python?
[Sublime Text 2] Always execute a specific file in the project
I was in trouble because the character string in the PDF was strange
[python] How to check if the Key exists in the dictionary
Find out the apparent width of a string in python
Determine if a string is a time with a python regular expression
[Pandas] If the first row data is in the header in DataFrame
Get the number of specific elements in a python list
How to check if a value exists in an enum
Mac Linux Check the capacity directly under a specific directory
Is this string a decimal?
The image is a slug
[Short note] In matplotlib, nothing is displayed even if I show it → Check the backend setting
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
Output the key list included in S3 Bucket to a file
Python will fail if there is a space after the backslash
Play a sound in Python assuming that the keyboard is a piano keyboard
pandas Fetch the name of a column that contains a specific character
[Introduction to Python] How to split a character string with the split function
Check if the password hash generated by PHP matches in Python
How to check the memory size of a variable in Python