I have a string like below and am trying to remove the last character from that string. can someone please help on this
what if I have a lengthy string and I want to only remove the last character of my string.
Example: "city": "Winston Salem","state": "NC","zip": "27127","country": " "}}
and I want to only remove the last '}'.
Use a String method like replace:
String newString = oldString.replace("}}", "}")
if that´s case or another one; you can use anywhere of methods of String API only if casts to String
Related
I'm trying to export email from Outlook(2010) into a CSV file, but there are emails with a comma in the subject line.
Is there a way to deal with this? I can't find an option to change the delimiter to something else.
Thanks
You can use the VBA Replace function.
Eg, this takes myString and replaces the comma with a dash.
Dim myString As String
myString = "Test subject, with comma"
myString = Replace(myString, ",", "-")
myString becomes "Test subject- with comma"
If the value contains a comma, enclose the value in quotes. If you have a quote, replace it with a double quote. E.g a value like
Weird, encoded "subject"
becomes
"Weird, encoded ""subject"""
I have a long String and want to delete the part of the String that comes after a word and I'm looking for the gsub! command that does that. I would appreciate it if you could provide it.
For reference:
I know that the command to delete the part of the String (the String is called contents) that comes before the word "body" is:
contents.gsub!(/.*?(?=body)/im, "")
Thanks.
This code:
"this has a word in it".gsub! /(word).*/, $1
Will change the string to "this has a word"
The "word" in brackets is the first argument returned by the regex, and $1 returns that argument.
See the Ruby docs for gsub
Going by your regex, that requires the / in body to be escaped, I'm assuming you mean every after
contents = "Stuff before </body> stuff after"
contents.gsub(/(?<=\/body>).+/, "")
=> "Stuff before </body>"
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.
I need to parse a string in ruby which contain vars of ids and names like this {2,Shahar}.
The string is like this:
text = "Hello {1,Micheal}, my name is {2,Shahar}, nice to meet you!"
when I am trying to parse it, the regexp skips the first } and I get something like this:
text.gsub(/\{(.*),(.*)\}/, "\\2(\\1)")
=> "Hello Shahar(1,Micheal}, my name is {2), nice to meet you!"
while the required resault should be:
=> "Hello Michael(1), my name is Shahar(2), nice to meet you!"
I would be thankful to anyone who can help.
Thanks
Shahar
The greedy .* matches too much. It means "any string, maximum possible length". So the first (.*) matches 1,Micheal}, my name is {2, then the comma matches the comma, and the second (.*) matches Shahar (and the final \} matches the closing braces.
Better be more specific. For example, you could restrict the match to allow only characters except braces to ensure that a match will never extend beyond the scope of a {...} section:
text.gsub(/\{([^{}]*),([^{}]*)\}/, "\\2(\\1)")
Or you could do this:
text.gsub(/\{([^,]*),([^}]*)\}/, "\\2(\\1)")
where the first part may be any string that doesn't contain a comma, the second part may be any string that doesn't contain a }.
I have the following code which is supposed to be removing a particular email address from a string if it exists. The problem is i get the error "invalid range "y-d" in string transliteration (ArgumentError)" which I assume is because it's treating my input as a regex. I will need to do this delete by a variable in the actual code, not a string literal but this is a simplified version of the problem.
So how do I properly perform this operation?
myvar = "test1#my-domain.com test2#my-domain.com"
myvar = myvar.delete("test1#my-domain.com")
Try
myvar = "test1#my-domain.com test2#my-domain.com"
myvar = myvar.gsub("test1#my-domain.com", '').strip
String#delete(str) does not delete the literal string str but builds a set out of individual characters of str and deletes all occurrences of these characters. try this:
"sets".delete("test")
=> ""
"sets".delete("est")
=> ""
The hyphen has a special meaning, it defines a range of characters. String#delete("a-d") will delete all occurrences of a,b,c and d characters. Range boundary characters should be given in ascending order: you should write "a-d" but not "d-a".
In your original example, ruby tries to build a character range from y-d substring and fails.
Use String#gsub method instead.
You can do it like this
myvar = "test1#my-domain.com test2#my-domain.com"
remove = "test1#my-domain.com"
myvar.gsub!(remove, "")