how to convert an array to a string after split function
I'm trying to convert the return value of the .split()
function into a string.
There is a string of characters that is being parsed from a text file, and I need to change it from an array to a string.
After calling the split function, the result is an array, however, I need it to be a string to perform character operations and functions.
example = "--NAME: John Doe"
print example
value = example.split("--NAME: ")
print value.class
value.to_s.strip
print value
print value.class
ruby
add a comment |
I'm trying to convert the return value of the .split()
function into a string.
There is a string of characters that is being parsed from a text file, and I need to change it from an array to a string.
After calling the split function, the result is an array, however, I need it to be a string to perform character operations and functions.
example = "--NAME: John Doe"
print example
value = example.split("--NAME: ")
print value.class
value.to_s.strip
print value
print value.class
ruby
2
It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g.,split
) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".
– Cary Swoveland
Jan 2 at 4:48
Is"--NAME: "
actually a delimiter? To me it looks more like a prefix / label.
– Stefan
Jan 2 at 13:26
add a comment |
I'm trying to convert the return value of the .split()
function into a string.
There is a string of characters that is being parsed from a text file, and I need to change it from an array to a string.
After calling the split function, the result is an array, however, I need it to be a string to perform character operations and functions.
example = "--NAME: John Doe"
print example
value = example.split("--NAME: ")
print value.class
value.to_s.strip
print value
print value.class
ruby
I'm trying to convert the return value of the .split()
function into a string.
There is a string of characters that is being parsed from a text file, and I need to change it from an array to a string.
After calling the split function, the result is an array, however, I need it to be a string to perform character operations and functions.
example = "--NAME: John Doe"
print example
value = example.split("--NAME: ")
print value.class
value.to_s.strip
print value
print value.class
ruby
ruby
edited Jan 2 at 6:00


Lahiru
1,35342527
1,35342527
asked Jan 2 at 1:35
philly_mcvphilly_mcv
41
41
2
It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g.,split
) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".
– Cary Swoveland
Jan 2 at 4:48
Is"--NAME: "
actually a delimiter? To me it looks more like a prefix / label.
– Stefan
Jan 2 at 13:26
add a comment |
2
It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g.,split
) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".
– Cary Swoveland
Jan 2 at 4:48
Is"--NAME: "
actually a delimiter? To me it looks more like a prefix / label.
– Stefan
Jan 2 at 13:26
2
2
It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g.,
split
) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".– Cary Swoveland
Jan 2 at 4:48
It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g.,
split
) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".– Cary Swoveland
Jan 2 at 4:48
Is
"--NAME: "
actually a delimiter? To me it looks more like a prefix / label.– Stefan
Jan 2 at 13:26
Is
"--NAME: "
actually a delimiter? To me it looks more like a prefix / label.– Stefan
Jan 2 at 13:26
add a comment |
6 Answers
6
active
oldest
votes
you can use 'join' to convert the array to string.
With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.
example = "--NAME: John Doe"
value = example.split("--NAME: ")
puts value
["", "John Doe"]
puts value.reject(&:blank?).join(",")
"John Doe"
add a comment |
(?<=--NAME:)
Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text
Strip!
to remove leading or trailing spaces in place.
irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!
Step by step:
irb(main):026:0> exampel = "--NAME: John Doe"
=> "--NAME: John Doe"
irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
=> "John Doe"
irb(main):028:0> example
=> "John Doe"
irb(main):029:0>
add a comment |
Perhaps split()
isn't the right tool for your needs?
If you just want to remove the prompt you could try gsub()
(https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub()
stands for "global substitution" and in other languages it is often known as replace()
.
example.gsub("--NAME: ", "")
Will replace the prompt with an empty string and give you:
"John Doe"
(a String)
add a comment |
Simply sub-string your string:
example = "--NAME: John Doe"
example[8..-1]
#=> "John Doe"
While 8
indicates the start position and -1
indicates that the substring should be ended with the last character of the example
string since the length of the NAME
is a variable.
add a comment |
If you are getting your example
string in same formats everytime,
example.split(' ', 2).last
add a comment |
> value = example.split("--NAME: ")
#=> ["", "John Doe"]
> value.join.strip
#=> "John Doe"
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%2f54000302%2fhow-to-convert-an-array-to-a-string-after-split-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
you can use 'join' to convert the array to string.
With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.
example = "--NAME: John Doe"
value = example.split("--NAME: ")
puts value
["", "John Doe"]
puts value.reject(&:blank?).join(",")
"John Doe"
add a comment |
you can use 'join' to convert the array to string.
With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.
example = "--NAME: John Doe"
value = example.split("--NAME: ")
puts value
["", "John Doe"]
puts value.reject(&:blank?).join(",")
"John Doe"
add a comment |
you can use 'join' to convert the array to string.
With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.
example = "--NAME: John Doe"
value = example.split("--NAME: ")
puts value
["", "John Doe"]
puts value.reject(&:blank?).join(",")
"John Doe"
you can use 'join' to convert the array to string.
With your example, I used 'join' to get string seperated by comma and used 'reject' to remove empty strings if any.
example = "--NAME: John Doe"
value = example.split("--NAME: ")
puts value
["", "John Doe"]
puts value.reject(&:blank?).join(",")
"John Doe"
answered Jan 2 at 2:44
Saraswathy RenugaSaraswathy Renuga
137110
137110
add a comment |
add a comment |
(?<=--NAME:)
Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text
Strip!
to remove leading or trailing spaces in place.
irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!
Step by step:
irb(main):026:0> exampel = "--NAME: John Doe"
=> "--NAME: John Doe"
irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
=> "John Doe"
irb(main):028:0> example
=> "John Doe"
irb(main):029:0>
add a comment |
(?<=--NAME:)
Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text
Strip!
to remove leading or trailing spaces in place.
irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!
Step by step:
irb(main):026:0> exampel = "--NAME: John Doe"
=> "--NAME: John Doe"
irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
=> "John Doe"
irb(main):028:0> example
=> "John Doe"
irb(main):029:0>
add a comment |
(?<=--NAME:)
Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text
Strip!
to remove leading or trailing spaces in place.
irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!
Step by step:
irb(main):026:0> exampel = "--NAME: John Doe"
=> "--NAME: John Doe"
irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
=> "John Doe"
irb(main):028:0> example
=> "John Doe"
irb(main):029:0>
(?<=--NAME:)
Positive lookbehind assertion: ensures that the preceding characters match --NAME:, but doesn't include those characters in the matched text
Strip!
to remove leading or trailing spaces in place.
irb(main):018:0>"--NAME: John Doe"[/(?<=--NAME:).*$/].strip!
Step by step:
irb(main):026:0> exampel = "--NAME: John Doe"
=> "--NAME: John Doe"
irb(main):027:0> example = exampel[/(?<=--NAME:).*$/].strip!
=> "John Doe"
irb(main):028:0> example
=> "John Doe"
irb(main):029:0>
edited Jan 2 at 4:27
answered Jan 2 at 4:21


zeezee
3,29712630
3,29712630
add a comment |
add a comment |
Perhaps split()
isn't the right tool for your needs?
If you just want to remove the prompt you could try gsub()
(https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub()
stands for "global substitution" and in other languages it is often known as replace()
.
example.gsub("--NAME: ", "")
Will replace the prompt with an empty string and give you:
"John Doe"
(a String)
add a comment |
Perhaps split()
isn't the right tool for your needs?
If you just want to remove the prompt you could try gsub()
(https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub()
stands for "global substitution" and in other languages it is often known as replace()
.
example.gsub("--NAME: ", "")
Will replace the prompt with an empty string and give you:
"John Doe"
(a String)
add a comment |
Perhaps split()
isn't the right tool for your needs?
If you just want to remove the prompt you could try gsub()
(https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub()
stands for "global substitution" and in other languages it is often known as replace()
.
example.gsub("--NAME: ", "")
Will replace the prompt with an empty string and give you:
"John Doe"
(a String)
Perhaps split()
isn't the right tool for your needs?
If you just want to remove the prompt you could try gsub()
(https://ruby-doc.org/core-2.4.1/String.html#method-i-gsub). gsub()
stands for "global substitution" and in other languages it is often known as replace()
.
example.gsub("--NAME: ", "")
Will replace the prompt with an empty string and give you:
"John Doe"
(a String)
answered Jan 2 at 3:45
DavidDavid
1,51031428
1,51031428
add a comment |
add a comment |
Simply sub-string your string:
example = "--NAME: John Doe"
example[8..-1]
#=> "John Doe"
While 8
indicates the start position and -1
indicates that the substring should be ended with the last character of the example
string since the length of the NAME
is a variable.
add a comment |
Simply sub-string your string:
example = "--NAME: John Doe"
example[8..-1]
#=> "John Doe"
While 8
indicates the start position and -1
indicates that the substring should be ended with the last character of the example
string since the length of the NAME
is a variable.
add a comment |
Simply sub-string your string:
example = "--NAME: John Doe"
example[8..-1]
#=> "John Doe"
While 8
indicates the start position and -1
indicates that the substring should be ended with the last character of the example
string since the length of the NAME
is a variable.
Simply sub-string your string:
example = "--NAME: John Doe"
example[8..-1]
#=> "John Doe"
While 8
indicates the start position and -1
indicates that the substring should be ended with the last character of the example
string since the length of the NAME
is a variable.
edited Jan 2 at 4:12
answered Jan 2 at 4:05


LahiruLahiru
1,35342527
1,35342527
add a comment |
add a comment |
If you are getting your example
string in same formats everytime,
example.split(' ', 2).last
add a comment |
If you are getting your example
string in same formats everytime,
example.split(' ', 2).last
add a comment |
If you are getting your example
string in same formats everytime,
example.split(' ', 2).last
If you are getting your example
string in same formats everytime,
example.split(' ', 2).last
answered Jan 2 at 5:39


rayray
3,3561829
3,3561829
add a comment |
add a comment |
> value = example.split("--NAME: ")
#=> ["", "John Doe"]
> value.join.strip
#=> "John Doe"
add a comment |
> value = example.split("--NAME: ")
#=> ["", "John Doe"]
> value.join.strip
#=> "John Doe"
add a comment |
> value = example.split("--NAME: ")
#=> ["", "John Doe"]
> value.join.strip
#=> "John Doe"
> value = example.split("--NAME: ")
#=> ["", "John Doe"]
> value.join.strip
#=> "John Doe"
answered Jan 2 at 6:04


Gagan GamiGagan Gami
8,46911643
8,46911643
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%2f54000302%2fhow-to-convert-an-array-to-a-string-after-split-function%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
2
It appears to me that you have a string (possibly read from a file, but that doesn't matter) and you wish to extract something from the string. If so, what do you want to extract? You need to tell us for your example (by editing your question). If I am correct, you should not assume that particular methods (e.g.,
split
) are needed. Just say what you want to achieve. This may be what's sometimes called an "X-Y Problem".– Cary Swoveland
Jan 2 at 4:48
Is
"--NAME: "
actually a delimiter? To me it looks more like a prefix / label.– Stefan
Jan 2 at 13:26