'Delete trailing ufeff character from output

I have these two methods:

func getSubName() string {

    // execute wecutil es
    out, err := exec.Command("cmd.exe", "/c", "wecutil", "es").Output()

    //handle any errors
    if err != nil {
        fmt.Printf("%s", err)
    }
    return string(out)

}

and

func getSubInfo(name string) string {
    out, err := exec.Command("cmd.exe", "/c", "wecutil gs ", name).Output()
    if err != nil {
        fmt.Printf("%s", err)
    }

    output := string(out)
    return output
}

getSubName returns a subscription name for Windows Event Forwarding, and getSubInfo grabs that name and returns the full info about the subscription.

My issue is getSubName is returning the correct name but adds a "ufeff" character to the beginning, making the name unrecognizable to Windows.

In this case, the name of the subscription is "test". When I manually pass the value to getSubInfo it returns all the right data, but I get a parameter error when I pass the name given by getSubName. Why? Cause of that "ufeff" character.

I'll be honest, I haven't ran into an issue like this before and I saw that it may have something to do with how the return value is encoded, but I'd like to ask you fine folks first.

Is there a way I can just strip that first character so the "ufeff" character never gets passed?

Thanks!

I included an image showing how the functions are formatting the return value, one that I trimmed with strings.TrimSpace and the other without any trimming.

Notice how the untrimmed value also adds a ton of whitespace to the end (first line): enter image description here



Solution 1:[1]

FIX

Aright, so we need to get rid of the "ufeff" character without using the strings.TrimSpace function. From what I can tell from your question the character is at
the end of the string.

  1. Add this function to your app
func TrimFirst(str string) string {
   for i := range s {
        if i > 0 {
            return s[i:]
        }
    }
    return ""
}
  1. Change your return statement to
return TrimFirst(output)

This should take off the the first character of your string. Which shoud be that "ufeff"

WHY

I suspect you are getting this random "ufeff" character because It is built into windows and is
some kind of formatting character.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mazecreator