I came to love word arrays, but today I face a challenge:
> a = %w[ faq contact 'about us' legal 'bug reports' ]
=> ["faq", "contact", "'about", "us'", "legal", "'bug", "reports'"]
> a = %w[ faq contact "about us" legal 'bug reports' ]
=> ["faq", "contact", "\"about", "us\"", "legal", "'bug", "reports'"]
How can I have whitespace an element?
To add a space between the characters of a string, call the split() method on the string to get an array of characters, and call the join() method on the array to join the substrings with a space separator, e.g. str. split(''). join(' ') . Copied!
To convert an array to a string with spaces, call the join() method on the array, passing it a string containing a space as a parameter - arr. join(' ') . The join method returns a string with all array elements joined by the provided separator. Copied!
For this C code: for(i=0;i<n;i++) printf("%4d",array[i]);
To split a string by multiple spaces, call the split() method, passing it a regular expression, e.g. str. trim(). split(/\s+/) . The regular expression will split the string on one or more spaces and return an array containing the substrings.
You can escape space characters
a = %w[ faq contact about\ us legal bug\ reports ]
a # => ["faq", "contact", "about us", "legal", "bug reports"]
But I'd still consider using "full" array literals. They are less confusing in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With