Go语言程序检查给定字符串是否为空
在 Go 中,字符串是一系列字符。它是一种不可变的数据类型,这意味着一旦创建了一个字符串,就不能修改它。字符串用双引号 ("") 括起来,可以包含任意组合的字母、数字和符号。它们通常用于存储文本,并且经常用作程序中的输入和输出。
语法
func len(v Type) int
len() 函数用于获取任何参数的长度。它将要查找长度的数据类型变量作为参数,并返回一个整数,即该变量的长度。
func TrimSpace(str string) string
trimSpace() 函数位于 strings 包中,用于删除字符串字符之间的空格。该函数接受所需的字符串作为参数,并在删除单词之间的空格后返回最终字符串。
算法
步骤 1 - 首先,我们需要导入 fmt 包。
步骤 2 - 现在,启动一个名为 isEmpty() 的函数。
步骤 3 - 使用 for 循环遍历字符串。
步骤 4 - 现在,启动 main() 函数。在 main() 中初始化一个字符串变量并为其赋值。
步骤 5 - 通过将字符串变量作为参数传递给函数来调用 isEmpty() 函数。
步骤 6 - 使用 if 条件检查函数返回的值是 true 还是 false。
步骤 7 - 如果函数返回的值为 true,则打印字符串为空,否则打印初始化的变量中存在的字符串。
示例 1
在此示例中,我们将使用 for 循环通过在外部函数中使用 for 循环来检查给定字符串是否为空。我们创建的函数将接受要检查的字符串作为参数,并根据字符串中是否包含字符返回一个布尔变量。
package main
import "fmt"
func isEmpty(str string) bool {
var empty bool = true
for i := 0; i < len(str); i++ {
if str[i] != ' ' {
empty = false
break
}
}
return empty
}
func main() {
var input string
input = "Easier way to succeed is by trying"
fmt.Println("Getting first string")
if isEmpty(input) {
fmt.Println("The string is empty.")
} else {
fmt.Println("The given string is:\n", input)
}
fmt.Println()
fmt.Println("Getting second string")
input = ""
if isEmpty(input) {
fmt.Println("The string is empty.")
} else {
fmt.Println("The given string is:\n", input)
}
}
输出
Getting first string The given string is: Easier way to succeed is by trying Getting second string The string is empty.
示例 2
在此示例中,我们将使用 Go 语言中的内置函数 len() 来查找给定字符串是否为空。
package main
import "fmt"
func main() {
var input string
fmt.Println("Example 1:")
input = "This is a string"
var result int = len(input)
if result == 0 {
fmt.Println("The string is empty.")
} else {
fmt.Println("The given string is:\n", input)
}
fmt.Println()
fmt.Println("Example 2:")
result = 0
input = ""
if result == 0 {
fmt.Println("The given string is empty.")
} else {
fmt.Println("The given string is:\n", input)
}
}
输出
Example 1: The given string is: This is a string Example 2: The given string is empty.
示例 3
在此示例中,我们将使用 strings 包中的 TrimSpace() 函数来检查给定字符串是否为空。
package main
import (
"fmt"
"strings"
)
func main() {
var input, result string
result = ""
input = "Try and try until you get the success"
fmt.Println("Example 1:")
if result == strings.TrimSpace(input) {
fmt.Println("The given string is empty")
} else {
fmt.Println("The given string is:\n", input)
}
fmt.Println()
fmt.Println("Example 2:")
input = ""
if result == strings.TrimSpace(input) {
fmt.Println("The given string is empty")
} else {
fmt.Println("The given string is:\n", input)
}
}
输出
Example 1: The given string is: Try and try until you get the success Example 2: The given string is empty
示例 4
在此示例中,我们将使用 reflect 包中的 ValueOf() 函数来检查给定字符串是否为空。
package main
import (
"fmt"
"reflect"
)
func main() {
var input string
input = "This is some string."
fmt.Println("Example 1:")
if reflect.ValueOf(input).String() == "" {
fmt.Println("The given string is empty")
} else {
fmt.Println("The given string is:\n", input)
}
fmt.Println()
fmt.Println("Example 2:")
input = ""
if reflect.ValueOf(input).String() == "" {
fmt.Println("The given string is empty")
} else {
fmt.Println("The given string is:\n", input)
}
}
输出
Example 1: The given string is: This is some string. Example 2: The given string is empty
结论
我们已经成功编译并执行了一个 Go 语言程序来检查给定字符串是否为空,并附带示例。在这里,我们使用了库函数和 for 循环来实现此结果。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP