can someone please help me with how to remove that empty white space?
How am I able to delete that space in line 6?
Strip is the function of String class which helps to remove unwanted space available in the string.
For Example:
str1 = " \n Testing String \n"
puts str1.strip
Output:
Testing String
Related
I want to be able to take in a string with the word 'WUB' placed randomly throughout it, and remove those instances replaced with white space.
Ex. "WUBWUBWUBWEWUBWUBAREWUBWUBWUBTHEWUBCHAMPIONSWUBWUBMYWUBFRIENDS"
turns into...
WE ARE THE CHAMPIONS MY FRIENDS
However the problems stands that I receive that with extra white space for each 'WUB.' How can I take out the extra white space and retain only one single white space?
def song_decoder(song)
song.gsub!(/WUB/, " ")
song.strip!
print song
return song
end
song_decoder("WUBWUBWUBWEWUBWUBAREWUBWUBWUBTHEWUBCHAMPIONSWUBWUBMYWUBFRIENDS")
# above is test case
/WUB/ gets all the "WUB" in the string, so if there are some consecutive ones, you'll have two white spaces, and using strip on the result, just would remove all whitespaces, so wouldn't be what you expect.
You could get any "WUB" as groups and replace them with ' '. As this specific result leaves you just with an initial whitespace (first character), lstrip would deal with that:
str = 'WUBWUBWUBWEWUBWUBAREWUBWUBWUBTHEWUBCHAMPIONSWUBWUBMYWUBFRIENDS'
p str.gsub(/(WUB)+/, ' ').lstrip
# "WE ARE THE CHAMPIONS MY FRIENDS"
I just found the squeeze method!
def song_decoder(song)
song.gsub!(/WUB/, " ")
song.strip!
song.squeeze!(" ")
print song
return song
end
This does exactly what I need. My apologies.
Without regular expression:
"WUBWUBWUBWEWUBWUBAREWUBWUBWUBTHEWUBCHAMPIONSWUBWUBMYWUBFRIENDS".
split('WUB').reject(&:empty?).join(' ')
#⇒ "WE ARE THE CHAMPIONS MY FRIENDS"
As the tittle suggests, I'd like to get some chars and check if the string as any of them. If I suppose, for example, "!" to be forbidden, then string.replace("",word_with_!). How can I check for forbidden chars if forbidden_chars is an array?
forbidden_chars = ["!",",",...]
check ARRAY (it is the string split into an array) for forbidden chars
erase all words with forbidden chars
Could anyone help me please? I just consider searching for the words with the cards and retrieving index as mandatory in the answer please. Thank you very much :)
string = 'I like my coffee hot, with no sugar!'
forbidden_chars = ['!', ',']
forbidden_chars_pattern = forbidden_chars.map(&Regexp.method(:escape)).join('|')
string.gsub /\S*(#{forbidden_chars_pattern})\S*/, ''
# => "I like my coffee with no "
The idea is to match as many non-white space characters as possible \S*, followed by any of the forbidden characters (!|,), followed by as many non-white space characters as possible again.
The reason we need the Regexp.escape is for the cases when a forbidden character has special regex meaning (like .).
string = 'I like my coffee strong, with no cream or sugar!'
verboten = '!,'
string.split.select { |s| s.count(verboten).zero? }.join ' '
#=> "I like my coffee with no cream or"
Note this does not preserve the spacing between "I" and "like" but if there are no extra spaces in string it returns a string that has no extra spaces.
I have a variable title. It can look like:
title = 'One two three'
Is it possible to replace the blanks with underscores?
Sure! What you want is either gsub or gsub! depending on your use case.
title = "One two three".gsub(/\s+/, "_")
will substitute any whitespace character with an underscore in the string and will store the string into title
if you already have title with the string stored then you can do
title.gsub!(/\s+/, "_")
and it will do the same substitution in title.
Yes, you can use the gsub method:
title = 'One two three'.gsub(/ /, '_')
title = 'One two three'.tr(" ", "_")
You can also split the string, automatically removing extra white space with .split and then rejoin the words with .join('_')
So title.split.join('_')
This has the benefit of not putting underscores or hyphens or whatever in the place of any trailing or leading spaces.
The user is able to input text, but the way I ingest the data it often contains unnecessary carriage returns and spaces.
To remove those to make the input look more like a real sentence, I use the following:
string.delete!("\n")
string = string.squeeze(" ").gsub(/([.?!]) */,'\1 ')
But in the case of the following, I get an unintended space in the email:
string = "Hey what is \n\n\n up joeblow#dude.com \n okay"
I get the following:
"Hey what is up joeblow#dude. com okay"
How can I enable an exception for the email part of the string so I get the following:
"Hey what is up joeblow#dude.com okay"
Edited
your method does the following:
string.squeeze(" ") # replaces each squence of " " by one space
gsub(/([.?!] */, '\1 ') # check if there is a space after every char in the between the brackets [.?!]
# and whether it finds one or more or none at all
# it adds another space, this is why the email address
# is splitted
I guess what you really want by this is, if there is no space after punctuation marks, add one space. You can do this instead.
string.gsub(/([.?!])\W/, '\1 ') # if there is a non word char after
# those punctuation chars, just add a space
Then you just need to replace every sequence of space chars with one space. so the last solution will be:
string.gsub(/([.?!])(?=\W)/, '\1 ').gsub(/\s+/, ' ')
# ([.?!]) => this will match the ., ?, or !. and capture it
# (?=\W) => this will match any non word char but will not capture it.
# so /([.?!])(?=\W)/ will find punctuation between parenthesis that
# are followed by a non word char (a space or new line, or even
# puctuation for example).
# '\1 ' => \1 is for the captured group (i.e. string that match the
# group ([.?!]) which is a single char in this case.), so it will add
# a space after the matched group.
If you are okay with getting rid of the squeeze statement then, using Nafaa's answer is the simplest way to do it but I've listed an alternate method in case its helpful:
string = string.split(" ").join(" ")
However, if you want to keep that squeeze statement you can amend Nafaa's method and use it after the squeeze statement:
string.gsub(/\s+/, ' ').gsub('. com', '.com')
or just directly change the string:
string.gsub('. com', '.com')
I'm struggling a bit with replacing some spaces with HTML nbsp; characters.
I'm trying to replace each space with a nbsp; character (not replace all of them with one nbsp;).
Here's what I'm trying at the moment:
"My String: ".gsub(/(?<=:).*\s/, ' ')
=>"My String: "
but this is about as close that I can get (I can kinda see why it's not working, but I'm unable to take it to that next step - if there is one?)...
Any Regex gods out there that can help?
If you are happy with your regexp you could just go:
p "My String: ".gsub(/(?<=:).*\s/){|x| ' '*x.size }
#=> "My String: "
If you instead want to make a new regexp:
# Any single space character that must be followed by 0+ spaces and then end of string.
string.gsub(/\s(?=\s*\Z)/,' ')