go對字元的迭代
阿新 • • 發佈:2021-12-22
package main
import (
"fmt"
"strings"
)
func main() {
ip:="192.168.0.10"
i:=0
str:="osdf"
for _,c:=range str{
i++
if c == 'l'{
break
}
}
fmt.Println(i)
ips:=strings.Split(ip,".")
for _,v:=range ips{
if v == ""{
return
}
if len(v) >1 &&v[0]==0 {
return}
}
}
func IsIpv4(ip string) bool { | |
//matched, _ := regexp.MatchString("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}", ip) | |
arr := strings.Split(ip, ".") | |
if len(arr) != 4 { | |
return false | |
} | |
for _, v := range arr { | |
if v == "" { | |
return false | |
} | |
if len(v) > 1 && v[0] == '0' { | |
return false | |
} | |
num := 0 | |
for _, c := range v { | |
if c >= '0' && c <= '9' { | |
num = num*10 + int(c-'0') | |
} else { | |
return false | |
} | |
} | |
if num > 255 { | |
return false | |
} | |
} | |
return true | |
} |