This question already has answers here:
How do I decode HTML entities in Swift?
(23 answers)
Closed 7 years ago.
I have an RSS feed and in its description element (here: www.marketoloji.com/?feed=rss2) I have ascii characters for the ones like ' or & and they are seen as ’ / &. How can I render that description string in Swift so that I wont see ascii characters?
You can use NSAttributedString to easily convert html code for you using the NSHTMLTextDocumentType option:
extension String {
var htmlString:String {
return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
}
}
let htmlCode = "’ / &"
htmlCode.htmlString // "’ / &"
Related
This question already has an answer here:
Swift 3 incorrect string interpolation with implicitly unwrapped Optionals
(1 answer)
Closed 6 years ago.
Since Swift 3.0 I have some troubles with Strings, especially with concatenation. 1st example would be what I used since I started using Swift to define my url strings.
internal let host: String! = "https://host.io/"
let urlString = "\(host)oauth/access_token"
where host is defined as at the beginning of the class. This worked perfectly until Swift 3.0, now that prints out like this:
Optional("https://host.io/")oauth/access_token
which is very strange. Now I have to write this
let urlString = host + "oauth/access_token"
To get the expected output.
https://host.io/oauth/access_token
Another - I guess similar problem I'm having with Strings is this. I'm again concatenating strings but this time I'm using + ilke with urlString - but this time that doesn't work. Line of code looks like this:
self.labelName.text = currentUser.name + " " + String(describing: ageComponents.year)
which unfortunately produces string like this: "My Name Optional(26)". In this case I don't have a solution String(describing: ageComponents.year) is not an optional and it doesn't allow me to do things like String(describing: ageComponents.year) ?? "whatever"
Anyone seen something similar?
In Swift 3 all properties of the native struct DateComponents are optionals unlike the Foundation NSDateComponents counterparts.
var year: Int? { get set }
You need to unwrap it. If you specified the unit year in ageComponents you can do that safely.
This question already has answers here:
How do I get the first character out of a string?
(7 answers)
Closed 6 years ago.
In JavaScript, I'd write:
s.charAt(0)
What would it be in Rust? How should I handle the s.length == 0 case?
Use s.chars().next(). This will return None if the string is empty or Some(c) otherwise.
This question already has answers here:
Convert string (without any separator) to list
(9 answers)
Closed 7 years ago.
I have a string s="12345678"
I need to store these numbers in a list like below format.
s=['1','2','3','4','5','6','7','8']
So can anybody help me how to do this.
Thanks,
Try this:
s = "12345678"
a = [c for c in s]
This question already has answers here:
Visual Studio debugger - Displaying integer values in Hex
(7 answers)
Closed 9 years ago.
I am using the below code
int.Parse("376") the result is coming as
int.Parse("376") = 0x00000178 int
and i tried to do as
Convert.Toint32("376") also then the result is same
please help me how to convert string to number?
It is working fine. 0x00000178 is the hexadecimal representation of 376.
Your Hex button is enabled in Visual Studio.
0x00000178 is the hexadecimal representation for 376, so using int.Parse or Convert.ToInt32 is OK.
However, I suggest to use the int.TryParse() method:
int i;
if (int.TryParse(yourString, out i))
{
// the string is converted successfully to an int, now you can find the int value in the variable 'i'
}
else
{
// Can't convert to an int: the string contains probably some characters that aren't digits
}
It is working fine. 0x178 is hex based for 376 in decimal.
This question already has answers here:
String to Table in Lua
(2 answers)
Closed 9 years ago.
I am newbie in lua. I need to convert following string to lua table. How could I do this?
str = "{a=1, b=2, c={d=3,e=4} }"
I want to convert this string to lua table, so that I can access it like this:
print(str['a']) -- Output : 1
print(str['c']['d']) -- Output : 3
You could simply add a str = to the beginning of the string and let the interpreter load that string as a chunk for you. Note that loadstring doesn't run the chunk but returns a function. So you add () to call that function right away and actually execute the code:
loadstring("str = "..str)()
This would do the same thing:
str = loadstring("return "..str)()
If you don't generate the string yourself, that can be dangerous though (because any code would be executed). In that case, you might want to parse the string manually, to make sure that it's actually a table and contains no bad function calls.