我有一个包含 Active Directory 调用输出的字节数组。我想对此进行解析并提取帐户到期前的天数。现在我想知道:提取 22-4-2016 11:05:26
的最佳方法是什么(所以 Password Expires
之后的值)?
[]byte(`The request will be processed at a domain controller for domain local.nl.bol.com.
User name bla
Full Name bla bla
Comment
User's comment
Country code (null)
Account active Yes
Account expires Never
Password last set 13-3-2016 11:05:26
Password expires 22-4-2016 11:05:26
Password changeable 13-3-2016 11:05:26
Password required Yes
User may change password Yes
Workstations allowed All
Logon script bla.bat
User profile
Home directory
Last logon 31-3-2016 7:59:29
Logon hours allowed All
The command completed successfully.`)
最佳答案
使用 strings.TrimSpace
、strings.Index
并引用相关的 stackoverflow 答案,我得到了一个有效的解决方案,请在下面找到有效的代码:-
package main
import (
"fmt"
"strings"
)
func CToGoString(c []byte) string {
n := -1
for i, b := range c {
if b == 0 {
break
}
n = i
}
return string(c[:n+1])
}
func main() {
s := []byte(`The request will be processed at a domain controller for domain local.nl.bol.com.
User name bla
Full Name bla bla
Comment
User's comment
Country code (null)
Account active Yes
Account expires Never
Password last set 13-3-2016 11:05:26
Password expires 22-4-2016 11:05:26
Password changeable 13-3-2016 11:05:26
Password required Yes
User may change password Yes
Workstations allowed All
Logon script bla.bat
User profile
Home directory
Last logon 31-3-2016 7:59:29
Logon hours allowed All
The command completed successfully.`)
d := CToGoString(s)
len := len("Password expires")
i := strings.Index(d, "Password expires")
j := strings.Index(d, "Password changeable")
chars := d[i+len:j]
fmt.Println(strings.TrimSpace(chars))
}
已将代码发布到 playground:http://play.golang.org/p/t0Xjd04-pi
关于string - Go,从字节数组中提取天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36516592/