NSString* abc = #"\u003ca\\tb\\tc\u003e";
How can I convert it to <a b c>
For horizontal tab, you should use "\t". And for "<" or ">" unicode character pass its hexadecimal value and log it by character specifier (%c) as i used below. It must work for you as I have tried in my xcode and it worked for me.
NSString * requiredStrg = [NSString stringWithFormat:#"%c a\tb\tc %c",0x3c,0x3e];
NSLog(#"%#",requiredStrg);
Related
I have a list of string and generate it to HTML dynamically with li tag. I want to assign that value to id attribute as well. But the problem is the string item has some special characters like :, ', é, ... I just want the output to include the number(0-9) and the alphabet (a-z) only.
// Input:
listStr = ["Pop & Suki", "PINK N' PROPER", "L'Oréal Paris"]
// Output:
result = ["pop_suki", "pink_n_proper", "loreal_paris"] ("loral_paris" is also good)
Currently, I've just lowercased and replace " " to _, but don't know how to eliminate special character.
Many thanks!
Instead of thinking of it as eliminating special characters, consider the permitted characters – you want just lower-case alphanumeric characters.
Elm provides Char.isAlphaNum to test for alphanumeric characters, and Char.toLower to transform a character to lower case. It also provides the higher function String.foldl which you can use to process a String one Char at a time.
So for each character:
check if it's alphanumeric
if it is, transform it to lower case
if not and it is a space, transform it to an underscore
else drop the character
Putting this together, we create a function that processes a character and appends it to the string processed so far, then apply that to all characters in the input string:
transformNextCharacter : Char -> String -> String
transformNextCharacter nextCharacter partialString =
if Char.isAlphaNum nextCharacter then
partialString ++ String.fromChar (Char.toLower nextCharacter)
else if nextCharacter == ' ' then
partialString ++ "_"
else
partialString
transformString : String -> String
transformString inputString =
String.foldl transformNextCharacter "" inputString
Online demo here.
Note: This answer simply drops special characters and thus produces "loral_paris" which is acceptable as per the OP.
The answer that was ticked is a lot more efficient than the code I have below. Nonetheless, I just want to add my code as an optional method.
Nonetheless, if you want to change accents to normal characters, you can install and use the elm-community/string-extra package. That one has the remove accent method.
This code below is inefficient as you keep on calling library function on the same string of which all of them would go through your string one char at a time.
Also, take note that when you remove the & in the first index you would have a double underscore. You would have to replace the double underscore with a single underscore.
import Html exposing (text)
import String
import List
import String.Extra
import Char
listStr = ["Pop & Suki", "PINK N' PROPER", "L'Oréal Paris"]
-- True if alpha or digit or space, otherwise, False.
isDigitAlphaSpace : Char -> Bool
isDigitAlphaSpace c =
if Char.isAlpha c || Char.isDigit c || c == ' ' then
True
else
False
main =
List.map (\x -> String.Extra.removeAccents x --Remove Accents first
|> String.filter isDigitAlphaSpace --Remove anything that not digit alpha or space
|> String.replace " " "_" --Replace space with _
|> String.replace "__" "_" --Replace double __ with _
|> String.toLower) listStr --Turn the string to lower
|> Debug.toString
|> Html.text
I would like to use a regex for "spaces", "dashes (-)", "apostrophes (')", and "letters" in my objective-c app.
I have the following, but it does not allow spaces.
NSString *fullNameRegex = #"^[a-zA-Z'\\-]$";
Could someone help me add the spaces please? Thank you!
You can use
NSString *fullNameRegex = #"^[\\sa-zA-Z'-]*$";
^^^ ^
Add a whitespace \s char class, and do not forget to let your string have 0 or more (with *) or 1 or more (with + quantifier).
Creating NSXMLNode with string:
NSXMLNode *node1 = [NSXMLNode textWithStringValue:#"<"];
NSLog(#"node1=%#",node1);
NSXMLNode *node2 = [NSXMLNode textWithStringValue:#">"];
NSLog(#"node2=%#",node2);
produces the following output:
node1=<
node2=>
Why is the "<" character escaped (i.e. converted into "<") while the ">" character is not?
Is this a bug?
Which node is handled correctly?
To quote the XML Spec:
The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. [...] The right angle bracket (>) may be represented using the string " > ; ", and must, for compatibility, be escaped using either " > ; " or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.
In short, there are circumstances in which > does not have to be escaped, such as if it appears in an attribute.
No.
Both are.
If you ask for the string in canonical format, both characters will be escaped:
NSXMLNode *node3 = [NSXMLNode textWithStringValue:#">"];
NSLog(#"node3=%#",[node3 canonicalXMLStringPreservingComments:NO]);
Output:
node3=>
I have a UIWebView, it has a table, and I want it 80% of the UIWebView size, but in de NSString I use as html:
"<table width = \"80%\", align = \"center\">"
I have a warning:
Invalid conversion specifier '"'**
because it is waiting for a d or # or whatever.
I try to see the html, using NSLog, and the % is missing
<table width = "80", align = "center">
how can I tell the NSString the % value? Thank you in advance
use '%%' instead of '%'
NSLog(#"<table width = \"80%%\", align = \"center\">");
I assume you are defining NSString using stringWithFormat so you should use '%%'. escape sequence for % is %%.
I was wondering if anyone might know what the regular expression would be to turn this:
West4thStreet
into this:
West 4th Street
I'm going to add the spaces to the string in Objective-C.
Thanks!
I don't know exactly where you want to put in spaces, but try something like [a-z.-][^a-z .-] and then put a space between the two characters in each match.
Something like this perl regex substitution would put a space before each group of capital letters or numbers. (You'd want to trim space before the string in this case also.) I assume you don't want it to break up eg: 45thStreet to 4 5th Street
Letters I'm less certain of.
s/([A-Z]+|[0-9]+)/ \1/g
I created a pattern to not match the beginning of the line for my personal amusement:
s/([^\^])([A-Z]+|[0-9]+)/\1 \2/g
This should work, if all your strings truly match the format of your example:
([A-Z][a-z]+)(\d+[a-z]+)([A-Z][a-z]+)
You can then separate the groups with spaces.
Another option would be to not use RegExKit and use code to loop through each character in the string and insert a space after each capital letter or after first decimal..
NSMutableString *myText2 = [[NSMutableString alloc] initWithString:#"The1stTest"];
bool isNumber=false;
for(int x=myText2.length-1;x>1;x--)
{
bool isUpperCase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[myText2 characterAtIndex:x]];
bool isLowerCase = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:[myText2 characterAtIndex:x]];
if([[NSCharacterSet decimalDigitCharacterSet] characterIsMember:[myText2 characterAtIndex:x]])
isNumber = true;
if((isUpperCase || isLowerCase) && isNumber)
{
[myText2 insertString:#" " atIndex:x+1];
isNumber=false;
}
if(isUpperCase)
[myText2 insertString:#" " atIndex:x];
}
NSLog(#"%#",myText2); // Output: "The 1st Test"