7
frogstair
205d

Any way to make this less clunky n shit?

Comments
  • 5
    Either move the inner replace out on a new line or use regex. Also, is there no trim function, isn't that what you're trying to do?
  • 5
    If you know these characters are only going to be at the end, then use Trim.

    If you don't, use ReplaceAll instead of Replace to avoid the -1 args.
  • 3
    You could use a regular expression.
    Or you could write your own replace function that's accepts [2]string where 0 is old and 1 is new.
  • 0
    @taigrr the problem with Trim is that I don't know if it's gonna be \n or \r\n so its either trim one or two characters

    @metamourge thats what i'm probably going for
  • 2
    In addition to other suggestions, also #self-describing-code

    text = removeLineEndings(text);
  • 0
    @shoop I don't know how it works in go but other languages like Rust and JS has trim and does exactly this. It removes trailing spaces and newlines. No need to specify length.

    Edit: looks like strings.TrimSpaces is what you're looking for.
  • 4
    @shoop golang's Trim uses a cutset. The order doesn't matter. If your cutset is "abc", then the string "deadbeefcaaaaaaacb" would become "deadbeef".
  • 0
    @taigrr oh, then it's exactly what I'm looking for, thank you!

    @olback this also works
  • 1
    RegExp
Add Comment