This question already has answers here:
Creating NSData from NSString in Swift
(9 answers)
Closed 4 years ago.
I have next:
deviceName = String.init(bytes: temp.prefix(upTo: index), encoding: .windowsCP1251)
where temp - [UInt8]. My question is: how can I convert this string back to Data?
I'm trying to convert like this:
newDataName = Data(newName.windowsCP1251)
But result is:
Value of type String has no member windowsCP1251
It is works with converting using utf8, but it shows russian characters incorrect. I need to use windowsCP1251 only:
newDataName = Data(newName.utf8)
Maybe this is what you looking for:
newDataName = newName.data(using: .windowsCP1251)
Related
This question already has answers here:
How to print double quotes inside ""?
(3 answers)
Closed 2 years ago.
Here, my String is FcvZ4eP+ek!y",+Z4E
, How can i assign it in strData variable
let strData = ""
let strData = "FcvZ4eP+ek!y\",+Z4E"
you need to escape any inner quotes
swift
let strData = "FcvZ4eP+ek!y\"" + ",+Z4E"
Merge multiple string.
This question already has answers here:
Swift Put String to Array from one to all letters
(5 answers)
Closed 2 years ago.
I have a string with no spaces I would like to turn the string into an array.
Let's say the string is "guru1", I would like my array to be ["g", "gu", "gur", "guru", "guru1"]
Thanks in advance.
You can use reduce :
"guru1".reduce([]) { $0 + [($0.last ?? "") + "\($1)"] }
This question already has answers here:
Swift - encode URL
(19 answers)
Closed 3 years ago.
I am encoding my string to hit a web service. I am using addingPercentEncoding(withAllowedCharacters: CharacterSet) to encode my String.
Everything works fine except the '(apostrophe) character gets encoded to %E2%80%99 instead of %27.
if let _keyword = keyword?
.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
parameters?["keyword"] = _keyword
}
For Example:
When keyword is Maggie's, output = "Maggie%E2%80%99s" instead of "Maggie%27s".
Output is fine for others, when keyword is Jelly Extracts output is "Jelly%20Extracts".
So, how do I encode '(apostrophe) properly to %27
Edit: When I pass static text, like "Maggie's.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)" output is correct but when I wrap it in a variable output comes incorrect.
Instead of urlQueryAllowed use alphanumerics.
let originalString = "Maggie's"
let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .alphanumerics)
print(escapedString) // Optional("Maggie%27s")
This question already has answers here:
How to decode UTF8 bytes?
(2 answers)
Closed 5 years ago.
I storing my messages as utf-8 to preserve emoji
but how do I decode it again and show its original form
This is the Sample Message - %e2%9c%8c
String result = java.net.URLDecoder.decode(contacts.getYourmessage(), "UTF-8");
This is the Sample Message - %e2%9c%8c
It is url-encoded string. So you can
var str = WebUtility.UrlDecode("%e2%9c%8c");
which returns ✌
This is for swift:
var str = "%e2%9c%8c"
print(str.removingPercentEncoding)
Output:
Optional("✌")
This question already has answers here:
Converting String to Int with Swift
(31 answers)
Closed 8 years ago.
I'm working in a small project and I faced this problem : text field is String by default so I need to take the input and convert it to integer then calculate I was searching for a way to solve the converting problem and I didn't find any working solution so can any one help me please ?
try like this
In Swift 2.0, toInt(),replaced with initializers. (In this case, Int(someString).)
let myString : String = "50"
let x : Int? = Int(myString)
if (x != null) {
// converted String to Int
}
myString.toInt() - convert the string value into int .