string - Go,从字节数组中提取天数

标签 string go byte

我有一个包含 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.TrimSpacestrings.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/

相关文章:

python - Django def __str__ 列表索引超出范围

python - 使用 __str__ 方法加入 python 对象列表

python - 如何使用 Python(或 Scapy)生成随机 IPv6 地址?

hash - 哈希任意对象的正确方法

java - 在一组 4 个数字中查找出现次数最多的数字的最有效算法

javascript - 如何使用 JavaScript 将字符串中的双引号替换为转义字符双引号?

regex - 如何提取子字符串

go - goroutine调用中 channel 接收运算符的阻塞行为

python - 计算 numpy 数组中变化的位

c# - 注册表中的 Byte[] 仅返回一个字母