Rails Strip Method Not Working Correctly - ruby-on-rails

So just in case someone stumbles across the same issue, I have a string:
a = " I am a string with whitespace at the start and end "
Interestingly when trying to a.strip, my string didn't change to:
a = "I am a string with whitespace at the start and end"

The issue here was, that I had or some other kind of space which prevented strip from doing what it does best.
My solution, first replace all my spaces with spaces:
a.gsub("\u00A0", " ") (I tried a.gsub(" ", " ") at first, but no luck)
tada!
Now I got my expected a.strip result :)
(Maybe there is a clearer way to do this, if so let me know)

Related

how to replace " \" " in string in lua

I try to replace the group 2 character \" in my JSON string but not sure why it didnt work.
Here is what i have
raw_json_text_edited = string.gsub( raw_json_text, [[\"]], [[]])
I also use
raw_json_text_edited = string.gsub( raw_json_text, '\"', '')
Both way doesnt work since it somehow only remove the " part of string rather than both \"
A bit background about the problem if you have any other suggestion.
I have a long JSON string with
..."phone":"{\"p1\":\"13068527218\",\"p2\":\"13062225064\",\"p3\":\"14445554444\"}","email":"{\"e1\":\"test#test.com\",\"e2\":\"test2#test.com\",\"e3\":\"sss#ww.com\"}",....
If I remove \" part in this JSON string, everything work.
UPDATE:
WORKING CODE ONLY FOR REPLACING:
raw_json_text_edited = string.gsub( raw_json_text, [[\\"]], [[]])
However, I just discover a problem why my JSON didnt work is that for JSON string,
..."phone":"{\"p1\":\"13068527218\",\"p2\":\"13062225064\",\"p3\":\"14445554444\"}","email":"{\"e1\":\"test#test.com\",\"e2\":\"test2#test.com\",\"e3\":\"sss#ww.com\"}"
If i only replace \" then it would be a sub string in "{}", eg: "{"p1":"1213131"}". This is wrong JSON format since table cannot be string.
This problem is something else so I will put this as solved
raw_json_text_edited = string.gsub( raw_json_text, [[\"]], [[]])

#HttpContext.Current.User.Identity.Name not showing backslash

Super Simple. Only issues I find are people getting null. Which I obvi fixed. But where is the backslash???!!
params.me = '#HttpContext.Current.User.Identity.Name';
This returns
"domainUserName" <- Browser
"domain\\UserName" <- Debugging
What I expect is
"domain\UserName" <- Browser
Any ideas?
Based on your comments you are using the following code to show the user name:
alert('#HttpContext.Current.User.Identity.Name');
#HttpContext.Current.User.Identity.Nameis a string that can contain "\" backslash character. This character is considered as a escape character in javascript as it is in C# as well.
You need to escape the "\" character in the string before passing it to Javascript like that:
alert('#HttpContext.Current.User.Identity.Name.Replace("\\", "\\\\")')

Xcode is throwing me an error in swift

I'm following a tutorial(http://youtube.com/watch?v=xvvsG9Cl4HA 19 min 20sec) and to make his code look neat he puts some on a ew line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \
(myPlacement.country) \
(myPlacement.postalCode)"
}
. But when I try I get an error
unterminated string literal
and
consecutive statements on a line must be seperated by a ';'
but the guy in the tutorial has done it the exact same way. What's going on?
I'm using the latest swift and and latest xcode 7.2 any help would be apreciated
if I write everything on the same line like this
if let myPlacement = myPlacements?.first
{
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
}
it works fine though
if I write everything on the same line like this
Well, there's your answer. You are not permitted to break up a string literal into multiple lines as you are doing in your first example. There are languages that permit this, but Swift is not one of them. This is not legal:
let s = "hello
there"
There is no magic line-continuation character which, placed at the end of the first line, would make that legal.
If the window is narrower than the line, the editor may wrap the line, for display purposes; but you cannot put actual line breaks inside a string literal.
You can work around this by combining (concatenating) multiple string literals, if you think that makes for greater legibility. This, for example, is legal:
let myAddress = "\(myPlacement.locality) " +
"\(myPlacement.country) " +
"\(myPlacement.postalCode)"
I look your video tutorial carefully. You have a misunderstanding here.
You must pay attention to the video, the code in this picture is not break lines because he add a return here, it is because his screen is too narrow.
So, the real code is
let myAddress = "\(myPlacement.locality) \(myPlacement.country) \(myPlacement.postalCode)"
Please watch it carefully.
And you may need know first, \ in \(myPlacement.locality) is a escape character, it means to get the value of myPlacement.locality and put in the string.

ruby regex with quotes

I'm trying to pass more than one regex parameter for parts of a string that needs to be replaced. Here's the string:
str = "stands in hall "Let's go get to first period everyone" Students continue moving to seats."
Here is the expected string:
str = "stands in hall "Let's go get to first period everyone" Students continue moving to seats."
This is what I tried:
str.gsub(/'|"/, "'" => "\'", """ => "\"")
This is what I got:
"stands in hall \"Let's go get to first period everyone\" Students continue moving to seats."
How do I get the quotes in while sending in two regex parameters using gsub?
This is an HTML unescaping problem.
require 'cgi'
CGI.unescape_html(str)
This gives you the correct answer.
From my comments on this question:
Your updated version is correct. The only reason the slashes are in your final line of code is that it's an escape sequence so that you don't mistakenly think the first slash is used to terminate the string. Try assigning your output and printing it:
str1 = str.gsub(/'|"/, "'" => "\'", """ => "\"")
puts str1
and you'll see that the slashes are gone when str1 is printed using puts.
The difference is that autoevaluating variables within irb (which is what I assume you're doing to execute this sample code) automatically calls the inspect method, which for string variables shows the string in its entirety.
Because I did not understand unescaping characters I found an alternative solution that might be the "rails-way"
Can you use <%= raw 'some_html' %>
My final solution ended up being this instead of messy regex and requiring CGI
<%= raw evidence_score.description %>
Unescaping HTML string in Rails

Cleaning a URL with PHP

I've been looking though google and stackflow for an answer for this and testing a few finds but I still can't get this working.
All of these end my link at a space. For example www.website.com/movies/movie
Where I'm trying to get it to read www.website.com/movies/movie with spaces here.mp4
$namehref = "movie/" . $dirArray[$index]. " download";
$DoStream = "Watch";
$DoDownload = "Download";
However this code does not remove the spaces???
$name = $dirArray[$index];
$movienameonly = substr($name, 0, -4);
example www.website.com/movies/movie with spaces here
So my questions are - Why does the first section of code remove the spaces and how do I correct it. In addition to spaces I also hit errors with 's as well.
example They're here.mp4
To remove the spaces completely:
preg_replace("/\s/", "", $your_url);
To replace the spaces with %20 (best way):
preg_replace("/\s/", "%20", $your_url);
To replace spaces with + like url_encode($url) does:
preg_replace("/\s/", "+", $your_url);
To replace ' with %27:
preg_replace("/\s/", "%20", $your_url);
You get errors because spaces can't be inputted in the browser and converts the spaces to %20 and the apostrophe to %27
I found it:
$DoStream = "Watch";
Should have been
$DoStream = "<a href='$the_dir'>Watch</a>";

Resources