Browse Source

IsHex and StripHex

pull/1842/head
rigel rozanski 8 years ago
parent
commit
295f6c2cc6
1 changed files with 20 additions and 0 deletions
  1. +20
    -0
      common/string.go

+ 20
- 0
common/string.go View File

@ -1,6 +1,7 @@
package common
import (
"encoding/hex"
"fmt"
"strings"
)
@ -22,3 +23,22 @@ func LeftPadString(s string, totalLength int) string {
}
return s
}
// Returns true for non-empty hex-string prefixed with "0x"
func IsHex(s string) bool {
if len(s) > 2 && s[:2] == "0x" {
_, err := hex.DecodeString(s[2:])
if err != nil {
return false
}
return true
}
return false
}
func StripHex(s string) string {
if IsHex(s) {
return s[2:]
}
return s
}

Loading…
Cancel
Save