-3

My string looks like this,

"<tlc:919876543212@abc.google.com>; tag=kkytuybsjdf <nil>"

I want to fetch the value-> 919876543212 from the string ,how to slice the string starting from next to the ':' and before the '@'

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
mark
  • 1,045
  • 2
  • 13
  • 23
  • 3
    OK, what have you tried? What problems do you have? Show us what you tried. – icza Oct 08 '20 at 12:09
  • You have 95% covered already: to extract that substring you need to find the first character, `:`, then the second, `@`, and with the knowledge of their positions in the string (indices), slicing the string is a no-brainer. So I invite you to think about how would you find a particular character in a string. – kostix Oct 08 '20 at 12:12
  • 1
    If you have trouble with actually slicing a string, then read [this](https://blog.golang.org/slices-intro), [this](https://blog.golang.org/slices), and [this](https://blog.golang.org/strings). And actually you might need to start right with [the Tour](https://tour.golang.org) and then move on to ["Effective Go"](https://golang.org/doc/effective_go.html). – kostix Oct 08 '20 at 12:18
  • First you need to know this [How to find a character index in Golang?](https://stackoverflow.com/questions/20827332/how-to-find-a-character-index-in-golang) then you need to know this [Extracting substrings in Go](https://stackoverflow.com/questions/12311033/extracting-substrings-in-go) – Eklavya Oct 08 '20 at 12:18
  • After getting answer read here before ask a new question https://stackoverflow.com/help/how-to-ask – Eklavya Oct 08 '20 at 12:24
  • Yes , As I was new to programming I was written a loop which will check for the match, as it was not efficient I haven't added in the post, but as what you told I researched bit more and got Idea about the strings and got the efficient way to solve this, I will be updating my post with the solution I came up with :) – mark Oct 08 '20 at 12:53
  • guys let me know if any improvement is there , Thanks – mark Oct 08 '20 at 12:56
  • You have a working solution. Why do you think it is not efficient? – icza Oct 08 '20 at 13:08
  • I'd improve it the following way: after searching for `:`, it may be useful to test whether the index is `-1` and exit right away if it is: it means the character was not found. You might also play with the following idea: since `@` must occur _past_ `:`, you might search for the `@` in the slice of the source string which starts right after `:`. This requires simple math on the indices—since the found index, if any, will be off its real place by the index of `:`. I'm not sure I'd wrote the code this way, but it may be a useful exercise. – kostix Oct 08 '20 at 13:19
  • Also, in the present form of the code, I'd make sure the index of `@` is greater than the index of `:`, otherwise the `a[n+1:m]` expression would be invalid. – kostix Oct 08 '20 at 13:20

1 Answers1

1

OP's answer, which was incorrectly edited into the question:

Solution:

a := "<tlc:919876543212@abc.google.com>; tag=kkytuybsjdf <nil>"
n := strings.Index(a, ":")
m := strings.Index(a, "@")
if n > -1 && m > -1 {
    number := a[n+1 : m]
    fmt.Println(number)
} else {
    fmt.Println("Index not found")
}
icza
  • 389,944
  • 63
  • 907
  • 827
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189