I have a string that has the value of "\\u{abc}" and I want to change it to "\u{abc}"
My code is as it follows
str = "\\u{abc}"
let newstr = str.remove(at: str.startIndex)
print(newstr)
The output is: u{abc}
How can I remove just the first one ?
I also tried using replacing occurrences and dropfirst
Does anyone know how to fix this
Thanks for your time
i am editing the answer sorry i made huge mistake, here is the correction
var str = "\\u{abc}"
print(str.replacingOccurrences(of: "\\", with: #"\"#))
ouput : \u{abc}
Let's go
if you try to just
print("\u")
Xcode will warn you: Expected hexadecimal code in braces after unicode escape.
so, Xcode expecting some hexadecimal value, and this is where the conflict begins
your solution, specifically with \U is:
let newstr = "\\" + str.dropFirst()
Full is:
var str = "\\u{abc}"
let newstr = "\\" + str.dropFirst()
print(newstr)
Related
I am getting this warning from Xcode Swift 5, here is my code I don't get what is wrong, I use this to remove any new line or tab at the end of my String (line)
My code:
let url: String = String(line.filter { !" \n\t\r".contains($0) })
UPDATE
I was doing it inside an if let and was using the type cast operator here is the solution and the rest of code and an example of the line value.
let line = " http://test.com/testing.php \n"
if let url: String = line.filter({!" \n\t\r".contains($0)}) as String?
{
//More action here
}
Thank you
to me this line looks good, but you may be missing the parentheses for the string filter method. Here's two ways I did it in playground. Let me know if this works for you, or how I can help further.
var line = "\t Hello, line removal \n \t Another new line \n"
let filteredClosure = line.filter { (char) -> Bool in
return !"\n\t\r".contains(char)
}
let filterShorthand = line.filter({!"\n\t\r".contains($0)})
With the line you provided, I would expect white-space to be removed too. If that's what you're looking for, add a space inside the filter string: " \n\t\r"
Code:
path5 = '2.Project\WP101 (237641784)\QR2\5.Project\\'
print ('path5 =',path5)
I get:
path5 = 2.Project\WP101 (237641784)\QR2.Project\
What can I do to stop getting the weird sign after QR2 in the path name?
You need to ignore escape chars.
#ignoring escape sequences
#ignoring single quote escape sequences
str1 = r"Hi, I\'m IncludeHelp"
#ignoring double quotes escape sequences
str2 = r"\"Hello world\""
#ignoring path escape sequences
str3 = r"D:\\work_folder\\python_works"
#ignoring hexadecimal values escape sequences
str4 = r"This is \x49\x6E\x63\x6C\x75\x64\x65\x48\x65\x6C\x70"
print(str1);
print(str2);
print(str3);
print(str4);
Just put an r infront of your string and you are good to go.
In your case:
path5 = r'2.Project\WP101 (237641784)\QR2\5.Project\'
print ('path5 =',path5)
For multiline button text I believe you add "\n" to the string. However I'm having trouble concatenating my function results and the newlinetext
setTitle:
HomeVC.getFriendCount("2",id:"friendid") + "\n newlinetext"
I need help getting my function results concatenated with "\n newlinetext"
You didn't specify an error, so I'm not sure, but I'm betting getFriendCount returns a number.
Try this:
let count = HomeVC.getFriendCount("2",id:"friendid")
let title = "\(count)\n newlinetext"
I've already encountered this. There must be some problem with (string + string) because it just ignores \n, though I never understood why this is. You can fix it by using join function:
let stringsToJoin = [getFriendCount("2",id:"friendid"), "newlinetext"]
let nString = join("\n", stringsToJoin)
Hope it helps!
You can use NSString's stringByAppendingString method
let aString = NSString(string: HomeVC.getFriendCount("2",id:"friendid"))
let concatenatedString = aString.stringByAppendingString("\n newlinetext")
If your method
HomeVC.getFriendCount("2",id:"friendid")
returns and optional string, then you need to unwrap it before concatenating.
Try
HomeVC.getFriendCount("2",id:"friendid")! + "\n newlinetext"
I need to be able to create a String that is "\b". But when I try to, Xcode throws a compile-time error: Invalid escape sequence in literal. I don't understand why though, "\r" works just fine. If I put "\\b" then that's what is actually stored in the String, which is not what I need - I only need one backslash. To me, this seems to be a Swift oddity because it works just fine in Objective-C.
let str = "\b" //Invalid escape sequence in literal
NSString *str = #"\b"; //works great
I need to generate this string because "\b" is the only way to detect when the user pressed 'delete' when using UIKeyCommand:
let command = UIKeyCommand(input: "\b", modifierFlags: nil, action: "didHitDelete:")
How can I work around this issue?
EDIT: It really doesn't want to generate a String that is only "\b", this does not work - it stays the original value:
var delKey = "\rb"
delKey = delKey.stringByReplacingOccurrencesOfString("r", withString: "", options: .LiteralSearch, range: nil)
The Swift equivalent of \b is \u{8}. It maps to ASCII control code 8, just like \b in Objective C. I've tested this and found it to work fine with UIKeyCommand, in this earlier answer of mine.
Example snippet:
func keyCommands() -> NSArray {
return [
UIKeyCommand(input: "\u{8}", modifierFlags: .allZeros, action: "backspacePressed")
]
}
I don't believe it is supported.
Based on the Swift documentation https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html:
String literals can include the following special Unicode characters:
The escaped special characters \0 (null character), \ (backslash), \t
(horizontal tab), \n (line feed), \r (carriage return), \" (double
quote) and \' (single quote)
An arbitrary Unicode scalar, written as
\u{n}, where n is between one and eight hexadecimal digits
The ASCII for the \b is 8. If you do the following, you'll see these results
let bs = "\u{8}"
var str = "Simple\u{8}string"
println(bs) // Prints ""
println("bs length is \(bs.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))") // Prints 1
println(str) // Prints Simplestring
let space = "\u{20}"
println(space) // Prints " "
println("space length is \(space.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))") // Prints 1
str = "Simple\u{20}string"
println(str) // Prints Simple string
It looks like while ASCII 8 "exists", it is "ignored".
I have a string that might contains '<!--more-->' and I am trying to remove it by using this code
local str = string.gsub (string, "<!--more-->", "")
but it doesn't work. The string still contains it.
I try with this
local str = string.gsub (string, "[<!--more-->]", "")
but it has an 'e' left.
The - character is special in patterns. You need to escape it:
local str = string.gsub (string, "<!%-%-more%-%->", "")