0

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
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". Commented Jan 2, 2019 at 4:48
  • Is "--NAME: " actually a delimiter? To me it looks more like a prefix / label. Commented Jan 2, 2019 at 13:26

6 Answers 6

2

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"

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

(?<=--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> 

Comments

0

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)

Comments

0

If you are getting your example string in same formats everytime,

example.split(' ', 2).last

Comments

0
> value = example.split("--NAME: ")
#=> ["", "John Doe"]
> value.join.strip
#=> "John Doe" 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.