为了账号安全,请及时绑定邮箱和手机立即绑定

从字节数组中提取天数

从字节数组中提取天数

Go
繁花如伊 2022-01-04 10:22:40
我有一个包含 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                    blaFull Name                    bla blaCommentUser's commentCountry code                 (null)Account active               YesAccount expires              NeverPassword last set            13-3-2016 11:05:26Password expires             22-4-2016 11:05:26Password changeable          13-3-2016 11:05:26Password required            YesUser may change password     YesWorkstations allowed         AllLogon script                 bla.batUser profileHome directoryLast logon                   31-3-2016 7:59:29Logon hours allowed          AllThe command completed successfully.`)
查看完整描述

2 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

使用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))

}

已将代码发布到操场:http : //play.golang.org/p/t0Xjd04-pi


查看完整回答
反对 回复 2022-01-04
?
慕神8447489

TA贡献1780条经验 获得超1个赞

您可以通过将 []byte 转换为字符串,然后使用 strings 包查找并提取该值,最后使用 time.Parse 对其进行解析以将字符串转换为您可以使用的时间来实现。


package main


import (

    "fmt"

    "strings"

    "time"

    "log"

)


func main() {

    line := data[strings.Index(data, "Password expires"):strings.Index(data, "Password changeable")]

    date := strings.TrimSpace(strings.TrimPrefix(line, "Password expires"))

    fmt.Println(date)

    pDate, err := time.Parse("02-1-2006 03:04:05", date)

    if err != nil {

        log.Fatal(err)

    }

    fmt.Println(pDate)

}


var data = string([]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.`))


查看完整回答
反对 回复 2022-01-04
  • 2 回答
  • 0 关注
  • 140 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号