why should i add two backslash to this string? [closed] - ios

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
am working on a weather app and this question jump in my mind
original string
<span class="phrase">
string with backslash
"<span class=\"phrase\">"

You have double quotes to open and close the string. If you have a double quote halfway, the parser will think that the string ends there. Adding the backslash ('escaping' the quote) tells the parser that the quote should be interpreted as a literal quote within the string rather than a string terminator.
For more information see Swift: strings and characters (scroll down to 'Special Characters in String Literals').

The backslash is used as an escape character - a " in a string literal means 'end of the string' in Swift, so you need another character - the backslash - to tell Swift 'interpret this " literally - I want it to be part of the string.'.
Of course, in HTML you can specify the value of an attribute with a single quote as well, so
"<span class=\"phrase\">"
and
"<span class='phrase'>"
will have the same effect.

Related

iOS String: remove prefix and suffix by CharacterSet [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
For a given String in Swift I need to remove prefix and suffix characters that belong to a predefined character set.
I can use components(separatedBy:) with the character set and get rid of the empty strings at the beginning and end of the components array. Just wondering if there's a better approach?
Thanks!
You can use .trimmingCharacters(in: CharacterSet) on the string. From the 'help' text in Xcode on this function: "Returns a new string made by removing from both ends of the String characters contained in a given character set."
Here is a unit test example using a custom character set. Notice it only removes characters from the start and end, not the middle (the comma stays):
import XCTest
class TrimCharacters: XCTestCase {
func testExample() throws {
let string = "abcbaHello, World!ccbbaa"
let charactersToTrim = CharacterSet(charactersIn: "abc,")
XCTAssertEqual(string.trimmingCharacters(in: charactersToTrim), "Hello, World!")
}
}
There are also predefined character sets that can be useful. You can also invert a character set. See CharacterSet

What is difference between escape sequence in Single quotes and Double quotes in Ruby [duplicate]

This question already has answers here:
Double vs single quotes
(7 answers)
Closed 6 years ago.
I have the following string and want to replace \n with < br>
string = "Subject: [Information] \n Hi there, \n...\n\n\n\\ud83d\\ude00\n--\n"
string.gsub('\n','<br>') #don't work
Than I tried
string.gsub("\n",'<br>')
It works, can explain this to me. String in double quotes are different from string in single quotes or it has to do with escape sequence.
Thanks
Single quotes preserve characters like \ as-is. Double quotes interpolate them, where \n means newline-character.
There's others, many of these coming from things like C.

Explain like I'm LITERALLY five...what does it mean to "format" a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
When you make a string...what does it mean to "format" that string?
CHALLENGE: Explain this to me like I'm an absolute idiot. Like, take it to a condescending level. Be mean about it. I'm talking how you would explain a lemonade stand to a very, very stupid child.
The most common use of the phrase refers to the replacing of variable placeholders within a string with the correct string representations of the variables' contents.
Consider:
temp_reading = 25.67528
puts "It is currently %0.1f degrees" % [temp_reading]
-> It is currently 25.7 degrees
String formatting is what turns the template into the string you see in the output.
As pointed out by Phil Taprogge, typically formatting a string refers to changing the representation of data for presentation reasons.
Phil's Example
long_number = 1.11111111111111111111111111111111111111111111111111111111111
puts "%0.1f" % long_number
=> 1.1
puts "%d" % long_number
=> 1
There is tons of documentation on string formatting and typically it can carry over to different languages since this come from the C programming language printf.
Formatting a string could refer, however, to any and all transformations to a string for presentation.
str = "hello world"
str.downcase
=> "hello world"
str.upcase
=> "HELLO WORLD"
str.capitalize
=> "Hello world"
str.titlieze
=> "Hello World"
str.parameterize
=> "hello-world"
https://blog.udemy.com/ruby-sprintf/
http://www.cplusplus.com/reference/cstdio/printf/

Molecular weight calculator in Lua [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
As my first real program in Lua, I'd like to create a molecular weight calculator (similar to the one here: http://www.lenntech.com/calculators/molecular/molecular-weight-calculator.htm )
I'd like some help with the first step of this program. My first step should be take the users input as a string, and than to split the string. I am looking at http://lua-users.org/wiki/StringLibraryTutorial and trying to figure out how to do this. When the user input is CH4 the program should split this string into C, and H4. Here is my code:
H = 1.008
C = 12.011
N = 14.007
O = 15.999
io.write("Enter molecular formula")
input = io.read()
result =
print("the molecular weigth is" .. result)
Can someone show me how I can accept the user input as a string and how to split that string?
Edit: as requested, I have been more specific in my exact question
Here's a possible way that you can start with:
Get the input string from standard input:
input = io.read()
Convert the string like Cr(NO2)2 to a string Cr+(N+O*2)*2. The nature of chemical element names is that it all begins with a uppercase letter, and followed by zero or more lowercase letters, so the rule could be: whenever a uppercase letter or a "(" is encountered (except when it's the first or preceded by a "(" ), insert a "+" before it, whenever a number is encountered, insert a "*" before it.
Calculate the result from the string Cr+(NO*2)*2, that's the beauty of Lua, it's a legal Lua expression, so just load the string and get the result:
str = "Cr+(N+O*2)*2"
func = assert(load("result = " .. str))
func()
print("the molecular weigth is" .. result)
In Lua 5.1, use loadstring() in the place of load().

How does this regex work? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
"Rubymonk Is Pretty Brilliant".match(/ ./, 9)
How is the answer "P" calculated from this regex?
use the match method on the string
passes two arguments, a regular expression and the position in the string to begin the search.
returns the character 'P'
The criteria you posted from the Rubymonk grader answer this succinctly:
passes two arguments, a regular expression and the position in the
string to begin the search
But let's examine that in more detail. match is being passed two arguments:
/ ./, a regular expression
9, the starting position in the string
The regular expression tells us that we're looking for a space () followed by any character (.).
The starting position tells us to start at position 9 (I). So instead of applying that regex against "Rubymonk Is Pretty Brilliant", we're applying it against "Is Pretty Brilliant".
In the string "Is Pretty Brilliant", where is the first place we encounter a space followed by another character? "Is[ P]retty Brilliant", right? Thus match finds a result of P (that's space-P, matching the regex, not just P.)
To see this more clearly and to experiment further with regexes, you can try it in an irb session or in your browser using Rubular.
(Just google for RegEx + ruby, You will find explanation of regex syntax)
/ANYTHING-HERE/
Will look for ANYTHING-HERE in the text.
In Your example its (/ ./,9):
/SPACE DOT/
So it will look for space followed by single character (Dot -> single character).
9 will be "I" from the string. And that is not space, so it will go on 2 characters right. Will find space, and then will find single character "P".
That is the result.

Resources