DoIt
DoIt

Reputation: 3448

Split a string at the last occurrence of the separator in golang

I am trying to split a string by the last occurrence of a separator (/) in golang

Example, I have a string "a/b/c/d", after performing the split, I would like an array of string as below

[
"a/b/c",
"a/b"
"a"
]

I tried exploring strings package but couldn't find any function that does this

func main() {
    fmt.Printf("%q\n", strings.Split("a/b/c/d/e", "/"))
}

May I know a way to handle this?

Upvotes: 6

Views: 10273

Answers (3)

Riya John
Riya John

Reputation: 532

To split any string only at the last occurrence, using strings.LastIndex

import (
    "fmt"
    "strings"
)

func main() {
    x := "a_ab_daqe_sd_ew"
    lastInd := strings.LastIndex(x, "_")
    fmt.Println(x[:lastInd]) // o/p: a_ab_daqe_sd
    fmt.Println(x[lastInd+1:]) // o/p: ew
}

Note, strings.LastIndex returns -1 if substring passed(in above example, "_") is not found

Upvotes: 16

maerics
maerics

Reputation: 156652

Here's a simple function that uses filepath.Dir(string) to build a list of all ancestor directories of a given filepath:

func main() {
  fmt.Printf("OK: %#v\n", parentsOf("a/b/c/d"))
  // OK: []string{"a/b/c", "a/b", "a"}
}

func parentsOf(s string) []string {
  dirs := []string{}
  for {
    parent := filepath.Dir(s)
    if parent == "." || parent == "/" {
      break
    }
    dirs = append(dirs, parent)
    s = parent
  }
  return dirs
}

Upvotes: 1

Mr_Pink
Mr_Pink

Reputation: 109443

Since this is for path operations, and it looks like you don't want the trailing path separator, then path.Dir does what you're looking for:

fmt.Println(path.Dir("a/b/c/d/e"))
// a/b/c/d

If this is specifically for filesystem paths, you will want to use the filepath package instead, to properly handle multiple path separators.

Upvotes: 4

Related Questions