Extracting substrings in Go
I'm trying to read an entire line from the console (including whitespace), then process it. Using bufio.ReadString, the newline character is read together with the input, so I came up with the following code to trim the newline character:
input,_:=src.ReadString('n')
inputFmt:=input[0:len(input)-2]+"" //Need to manually add end of string
Is there a more idiomatic way to do this? That is, is there already a library that takes care of the ending null byte when extracting substrings for you?
(Yes, I know there is already a way to read a line without the newline character in go readline -> string but I'm looking more for elegant string manipulation.)
go substring
add a comment |
I'm trying to read an entire line from the console (including whitespace), then process it. Using bufio.ReadString, the newline character is read together with the input, so I came up with the following code to trim the newline character:
input,_:=src.ReadString('n')
inputFmt:=input[0:len(input)-2]+"" //Need to manually add end of string
Is there a more idiomatic way to do this? That is, is there already a library that takes care of the ending null byte when extracting substrings for you?
(Yes, I know there is already a way to read a line without the newline character in go readline -> string but I'm looking more for elegant string manipulation.)
go substring
add a comment |
I'm trying to read an entire line from the console (including whitespace), then process it. Using bufio.ReadString, the newline character is read together with the input, so I came up with the following code to trim the newline character:
input,_:=src.ReadString('n')
inputFmt:=input[0:len(input)-2]+"" //Need to manually add end of string
Is there a more idiomatic way to do this? That is, is there already a library that takes care of the ending null byte when extracting substrings for you?
(Yes, I know there is already a way to read a line without the newline character in go readline -> string but I'm looking more for elegant string manipulation.)
go substring
I'm trying to read an entire line from the console (including whitespace), then process it. Using bufio.ReadString, the newline character is read together with the input, so I came up with the following code to trim the newline character:
input,_:=src.ReadString('n')
inputFmt:=input[0:len(input)-2]+"" //Need to manually add end of string
Is there a more idiomatic way to do this? That is, is there already a library that takes care of the ending null byte when extracting substrings for you?
(Yes, I know there is already a way to read a line without the newline character in go readline -> string but I'm looking more for elegant string manipulation.)
go substring
go substring
edited Nov 13 '18 at 15:55
lospejos
1,43421426
1,43421426
asked Sep 7 '12 at 2:43
mark2222mark2222
482146
482146
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
It looks like you're confused by the working of slices and the string storage format, which is different from what you have in C.
- any slice in Go stores the length (in bytes), so you don't have to care about the cost of the
len
operation : there is no need to count - Go strings aren't null terminated, so you don't have to remove a null byte, and you don't have to add
1
after slicing by adding an empty string.
To remove the last char (if it's a one byte char), simply do
inputFmt:=input[:len(input)-1]
7
You don't even need the 0 (or the :),s = s[:len(s)-1]
will do.
– uriel
Sep 7 '12 at 15:06
1
Thanks a lot for clarifying; it appears that there were two whitespace characters at the end of the string returned from the ReadString function, so I mistook one for a null byte. Sorry for the confusion with C strings; I was using fmt together with bufio resulting in funny stuff appearing in the console, so I thought it could be the dirty null byte. Just a final clarification - what could that extra whitespace from ReadString be?
– mark2222
Sep 8 '12 at 14:32
Ok I'll answer my own question - it's r then n :P The funny console output was because I outputted r without n.
– mark2222
Sep 8 '12 at 14:37
5
Please note that this method will not work with Unicode strings! groups.google.com/forum/#!msg/golang-nuts/ZeYei0IWrLg/…
– Melllvar
Aug 24 '13 at 0:02
@Melllvar That's why I precised "if it's a one byte char". If you want to remove a char taking more than one byte (that's not OP's case), you have to adapt.
– Denys Séguret
Aug 24 '13 at 7:16
|
show 4 more comments
Go strings are not null terminated, and to remove the last char of a string you can simply do:
s = s[:len(s)-1]
4
This is incorrect and will cause bugs. This strips the last byte off the string, which may render it invalid UTF-8 (or other multibyte encoding).
– dr. Sybren
Oct 24 '17 at 10:53
2
See play.golang.org/p/K3HBBtj4Oi for an example of how this breaks.
– dr. Sybren
Oct 24 '17 at 11:04
add a comment |
To avoid a panic on a zero length input, wrap the truncate operation in an if
input, _ := src.ReadString('n')
var inputFmt string
if len(input) > 0 {
inputFmt = input[:len(input)-1]
}
// Do something with inputFmt
add a comment |
To get substring
find position of "sp"
cut string with array-logical
https://play.golang.org/p/0Redd_qiZM
add a comment |
This is the simple one to perform substring in Go
package main
import "fmt"
var p = fmt.Println
func main() {
value := "address;bar"
// Take substring from index 2 to length of string
substring := value[2:len(value)]
p(substring)
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12311033%2fextracting-substrings-in-go%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like you're confused by the working of slices and the string storage format, which is different from what you have in C.
- any slice in Go stores the length (in bytes), so you don't have to care about the cost of the
len
operation : there is no need to count - Go strings aren't null terminated, so you don't have to remove a null byte, and you don't have to add
1
after slicing by adding an empty string.
To remove the last char (if it's a one byte char), simply do
inputFmt:=input[:len(input)-1]
7
You don't even need the 0 (or the :),s = s[:len(s)-1]
will do.
– uriel
Sep 7 '12 at 15:06
1
Thanks a lot for clarifying; it appears that there were two whitespace characters at the end of the string returned from the ReadString function, so I mistook one for a null byte. Sorry for the confusion with C strings; I was using fmt together with bufio resulting in funny stuff appearing in the console, so I thought it could be the dirty null byte. Just a final clarification - what could that extra whitespace from ReadString be?
– mark2222
Sep 8 '12 at 14:32
Ok I'll answer my own question - it's r then n :P The funny console output was because I outputted r without n.
– mark2222
Sep 8 '12 at 14:37
5
Please note that this method will not work with Unicode strings! groups.google.com/forum/#!msg/golang-nuts/ZeYei0IWrLg/…
– Melllvar
Aug 24 '13 at 0:02
@Melllvar That's why I precised "if it's a one byte char". If you want to remove a char taking more than one byte (that's not OP's case), you have to adapt.
– Denys Séguret
Aug 24 '13 at 7:16
|
show 4 more comments
It looks like you're confused by the working of slices and the string storage format, which is different from what you have in C.
- any slice in Go stores the length (in bytes), so you don't have to care about the cost of the
len
operation : there is no need to count - Go strings aren't null terminated, so you don't have to remove a null byte, and you don't have to add
1
after slicing by adding an empty string.
To remove the last char (if it's a one byte char), simply do
inputFmt:=input[:len(input)-1]
7
You don't even need the 0 (or the :),s = s[:len(s)-1]
will do.
– uriel
Sep 7 '12 at 15:06
1
Thanks a lot for clarifying; it appears that there were two whitespace characters at the end of the string returned from the ReadString function, so I mistook one for a null byte. Sorry for the confusion with C strings; I was using fmt together with bufio resulting in funny stuff appearing in the console, so I thought it could be the dirty null byte. Just a final clarification - what could that extra whitespace from ReadString be?
– mark2222
Sep 8 '12 at 14:32
Ok I'll answer my own question - it's r then n :P The funny console output was because I outputted r without n.
– mark2222
Sep 8 '12 at 14:37
5
Please note that this method will not work with Unicode strings! groups.google.com/forum/#!msg/golang-nuts/ZeYei0IWrLg/…
– Melllvar
Aug 24 '13 at 0:02
@Melllvar That's why I precised "if it's a one byte char". If you want to remove a char taking more than one byte (that's not OP's case), you have to adapt.
– Denys Séguret
Aug 24 '13 at 7:16
|
show 4 more comments
It looks like you're confused by the working of slices and the string storage format, which is different from what you have in C.
- any slice in Go stores the length (in bytes), so you don't have to care about the cost of the
len
operation : there is no need to count - Go strings aren't null terminated, so you don't have to remove a null byte, and you don't have to add
1
after slicing by adding an empty string.
To remove the last char (if it's a one byte char), simply do
inputFmt:=input[:len(input)-1]
It looks like you're confused by the working of slices and the string storage format, which is different from what you have in C.
- any slice in Go stores the length (in bytes), so you don't have to care about the cost of the
len
operation : there is no need to count - Go strings aren't null terminated, so you don't have to remove a null byte, and you don't have to add
1
after slicing by adding an empty string.
To remove the last char (if it's a one byte char), simply do
inputFmt:=input[:len(input)-1]
edited yesterday
answered Sep 7 '12 at 7:39
Denys SéguretDenys Séguret
276k54583599
276k54583599
7
You don't even need the 0 (or the :),s = s[:len(s)-1]
will do.
– uriel
Sep 7 '12 at 15:06
1
Thanks a lot for clarifying; it appears that there were two whitespace characters at the end of the string returned from the ReadString function, so I mistook one for a null byte. Sorry for the confusion with C strings; I was using fmt together with bufio resulting in funny stuff appearing in the console, so I thought it could be the dirty null byte. Just a final clarification - what could that extra whitespace from ReadString be?
– mark2222
Sep 8 '12 at 14:32
Ok I'll answer my own question - it's r then n :P The funny console output was because I outputted r without n.
– mark2222
Sep 8 '12 at 14:37
5
Please note that this method will not work with Unicode strings! groups.google.com/forum/#!msg/golang-nuts/ZeYei0IWrLg/…
– Melllvar
Aug 24 '13 at 0:02
@Melllvar That's why I precised "if it's a one byte char". If you want to remove a char taking more than one byte (that's not OP's case), you have to adapt.
– Denys Séguret
Aug 24 '13 at 7:16
|
show 4 more comments
7
You don't even need the 0 (or the :),s = s[:len(s)-1]
will do.
– uriel
Sep 7 '12 at 15:06
1
Thanks a lot for clarifying; it appears that there were two whitespace characters at the end of the string returned from the ReadString function, so I mistook one for a null byte. Sorry for the confusion with C strings; I was using fmt together with bufio resulting in funny stuff appearing in the console, so I thought it could be the dirty null byte. Just a final clarification - what could that extra whitespace from ReadString be?
– mark2222
Sep 8 '12 at 14:32
Ok I'll answer my own question - it's r then n :P The funny console output was because I outputted r without n.
– mark2222
Sep 8 '12 at 14:37
5
Please note that this method will not work with Unicode strings! groups.google.com/forum/#!msg/golang-nuts/ZeYei0IWrLg/…
– Melllvar
Aug 24 '13 at 0:02
@Melllvar That's why I precised "if it's a one byte char". If you want to remove a char taking more than one byte (that's not OP's case), you have to adapt.
– Denys Séguret
Aug 24 '13 at 7:16
7
7
You don't even need the 0 (or the :),
s = s[:len(s)-1]
will do.– uriel
Sep 7 '12 at 15:06
You don't even need the 0 (or the :),
s = s[:len(s)-1]
will do.– uriel
Sep 7 '12 at 15:06
1
1
Thanks a lot for clarifying; it appears that there were two whitespace characters at the end of the string returned from the ReadString function, so I mistook one for a null byte. Sorry for the confusion with C strings; I was using fmt together with bufio resulting in funny stuff appearing in the console, so I thought it could be the dirty null byte. Just a final clarification - what could that extra whitespace from ReadString be?
– mark2222
Sep 8 '12 at 14:32
Thanks a lot for clarifying; it appears that there were two whitespace characters at the end of the string returned from the ReadString function, so I mistook one for a null byte. Sorry for the confusion with C strings; I was using fmt together with bufio resulting in funny stuff appearing in the console, so I thought it could be the dirty null byte. Just a final clarification - what could that extra whitespace from ReadString be?
– mark2222
Sep 8 '12 at 14:32
Ok I'll answer my own question - it's r then n :P The funny console output was because I outputted r without n.
– mark2222
Sep 8 '12 at 14:37
Ok I'll answer my own question - it's r then n :P The funny console output was because I outputted r without n.
– mark2222
Sep 8 '12 at 14:37
5
5
Please note that this method will not work with Unicode strings! groups.google.com/forum/#!msg/golang-nuts/ZeYei0IWrLg/…
– Melllvar
Aug 24 '13 at 0:02
Please note that this method will not work with Unicode strings! groups.google.com/forum/#!msg/golang-nuts/ZeYei0IWrLg/…
– Melllvar
Aug 24 '13 at 0:02
@Melllvar That's why I precised "if it's a one byte char". If you want to remove a char taking more than one byte (that's not OP's case), you have to adapt.
– Denys Séguret
Aug 24 '13 at 7:16
@Melllvar That's why I precised "if it's a one byte char". If you want to remove a char taking more than one byte (that's not OP's case), you have to adapt.
– Denys Séguret
Aug 24 '13 at 7:16
|
show 4 more comments
Go strings are not null terminated, and to remove the last char of a string you can simply do:
s = s[:len(s)-1]
4
This is incorrect and will cause bugs. This strips the last byte off the string, which may render it invalid UTF-8 (or other multibyte encoding).
– dr. Sybren
Oct 24 '17 at 10:53
2
See play.golang.org/p/K3HBBtj4Oi for an example of how this breaks.
– dr. Sybren
Oct 24 '17 at 11:04
add a comment |
Go strings are not null terminated, and to remove the last char of a string you can simply do:
s = s[:len(s)-1]
4
This is incorrect and will cause bugs. This strips the last byte off the string, which may render it invalid UTF-8 (or other multibyte encoding).
– dr. Sybren
Oct 24 '17 at 10:53
2
See play.golang.org/p/K3HBBtj4Oi for an example of how this breaks.
– dr. Sybren
Oct 24 '17 at 11:04
add a comment |
Go strings are not null terminated, and to remove the last char of a string you can simply do:
s = s[:len(s)-1]
Go strings are not null terminated, and to remove the last char of a string you can simply do:
s = s[:len(s)-1]
answered Sep 7 '12 at 15:10
urieluriel
1,197814
1,197814
4
This is incorrect and will cause bugs. This strips the last byte off the string, which may render it invalid UTF-8 (or other multibyte encoding).
– dr. Sybren
Oct 24 '17 at 10:53
2
See play.golang.org/p/K3HBBtj4Oi for an example of how this breaks.
– dr. Sybren
Oct 24 '17 at 11:04
add a comment |
4
This is incorrect and will cause bugs. This strips the last byte off the string, which may render it invalid UTF-8 (or other multibyte encoding).
– dr. Sybren
Oct 24 '17 at 10:53
2
See play.golang.org/p/K3HBBtj4Oi for an example of how this breaks.
– dr. Sybren
Oct 24 '17 at 11:04
4
4
This is incorrect and will cause bugs. This strips the last byte off the string, which may render it invalid UTF-8 (or other multibyte encoding).
– dr. Sybren
Oct 24 '17 at 10:53
This is incorrect and will cause bugs. This strips the last byte off the string, which may render it invalid UTF-8 (or other multibyte encoding).
– dr. Sybren
Oct 24 '17 at 10:53
2
2
See play.golang.org/p/K3HBBtj4Oi for an example of how this breaks.
– dr. Sybren
Oct 24 '17 at 11:04
See play.golang.org/p/K3HBBtj4Oi for an example of how this breaks.
– dr. Sybren
Oct 24 '17 at 11:04
add a comment |
To avoid a panic on a zero length input, wrap the truncate operation in an if
input, _ := src.ReadString('n')
var inputFmt string
if len(input) > 0 {
inputFmt = input[:len(input)-1]
}
// Do something with inputFmt
add a comment |
To avoid a panic on a zero length input, wrap the truncate operation in an if
input, _ := src.ReadString('n')
var inputFmt string
if len(input) > 0 {
inputFmt = input[:len(input)-1]
}
// Do something with inputFmt
add a comment |
To avoid a panic on a zero length input, wrap the truncate operation in an if
input, _ := src.ReadString('n')
var inputFmt string
if len(input) > 0 {
inputFmt = input[:len(input)-1]
}
// Do something with inputFmt
To avoid a panic on a zero length input, wrap the truncate operation in an if
input, _ := src.ReadString('n')
var inputFmt string
if len(input) > 0 {
inputFmt = input[:len(input)-1]
}
// Do something with inputFmt
answered Feb 5 '15 at 18:37
RohanthewizRohanthewiz
53946
53946
add a comment |
add a comment |
To get substring
find position of "sp"
cut string with array-logical
https://play.golang.org/p/0Redd_qiZM
add a comment |
To get substring
find position of "sp"
cut string with array-logical
https://play.golang.org/p/0Redd_qiZM
add a comment |
To get substring
find position of "sp"
cut string with array-logical
https://play.golang.org/p/0Redd_qiZM
To get substring
find position of "sp"
cut string with array-logical
https://play.golang.org/p/0Redd_qiZM
answered Nov 3 '15 at 8:51
TeeTrackerTeeTracker
4,15732634
4,15732634
add a comment |
add a comment |
This is the simple one to perform substring in Go
package main
import "fmt"
var p = fmt.Println
func main() {
value := "address;bar"
// Take substring from index 2 to length of string
substring := value[2:len(value)]
p(substring)
}
add a comment |
This is the simple one to perform substring in Go
package main
import "fmt"
var p = fmt.Println
func main() {
value := "address;bar"
// Take substring from index 2 to length of string
substring := value[2:len(value)]
p(substring)
}
add a comment |
This is the simple one to perform substring in Go
package main
import "fmt"
var p = fmt.Println
func main() {
value := "address;bar"
// Take substring from index 2 to length of string
substring := value[2:len(value)]
p(substring)
}
This is the simple one to perform substring in Go
package main
import "fmt"
var p = fmt.Println
func main() {
value := "address;bar"
// Take substring from index 2 to length of string
substring := value[2:len(value)]
p(substring)
}
answered May 23 '18 at 7:37
Faris RayhanFaris Rayhan
1,53811212
1,53811212
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12311033%2fextracting-substrings-in-go%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown