I have a string such as "abbaaxaa" and I want the characters in the string to be converted into a set such that its elements are "a", "b", and "x"? (I'm a noob coder and I just thought of this implementation to represent the keys for a dictionary, if context matters at all).
I also have code on what I've attempted so far, but it has errors (i.e. where initializing the set and syntax of expressions are concerned) and I am not sure how to go about implementing it... I just know I'm working inside a class (+) method that returns an NSSet.
Use an NSMutableSet. Loop through the string character by character, implement the loop body to read something like
[set addObject:[str substringWithRange:NSMakeRange(idx, 1)]]
Related
I want to make regular expression for the dynamic words which comes from array and detect those word in array of string to make them linkable .
Eg. nsarray *arrOfStrings =#[#"This is sample string for objective c",#"Objective c or swift",#"Test string",#"iOS development",#"Re
gular expression link"];
nsarray *arrLinkWord =#[#"is",#"sample",#"or",#"link",#"Test"];
As well as in arabic also
Thank you
I am facing one issue related some hexa value in string, i need to remove hexadecimal characters from NSString.
The problem is when i print object it prints as "BLANK line". And in debug mode it shows like :
So how can i remove it from the string?
EDIT
Triming whitespace :
result of NSLog is :
2015-12-14 15:37:10.710 MyApp [2731:82236] tmp :''
Database:
Earlier question:
how to detect garbage string value in ios?
As your dataset clearly has garbage values, You can use this method to check if your string is valid or not. Define your validation criteria and simply don't entertain the values which are garbage. But as suggested before by gnasher, you should rather look for the bug which is causing insertion of garbage data in your database. Once you have done that, check if the input string matches your defined criteria. If it does, do what you want. If it doesn't, simply move on.
-(BOOL) isValidString: (NSString*) input
{
NSMutableCharacterSet *validSpecialChars = [NSMutableCharacterSet characterSetWithCharactersInString:#"_~.,"];//Add your desired characters here
[validSpecialChars formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
return [[input stringByTrimmingCharactersInSet:validSpecialChars] isEqualToString:#""];
}
If your string will contain only your defined characters, it will return true. If it contains any other characters (garbage or invalid) it will return false.
I'm not sure exactly what you are looking for, but if you want to remove all the control characters then
string = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet controlCharacterSet]] componentsJoinedByString:#""]
If you need to be faster and are sure the control characters are only at the beginning and ending of a string then
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
NOTE: Removing all control characters will remove all new lines (\n)!
From NSCharacterSet Class Reference:
These characters are specifically the Unicode values U+0000 to U+001F and U+007F to U+009F.
The value you are having a problem with is \x06 which is U+0006.
If you want to remove just \x06, then you can always create a characters set just for it.
NSCharacterSet *hex6 = [NSCharacterSet characterSetWithCharactersInString:#"\x06"];
string = [[string componentsSeparatedByCharactersInSet:hex6] componentsJoinedByString:#""]
First, don't trust the Xcode debugger. Print characterAtIndex:0 to be sure that you really have what you think you have.
Second, deleting stuff is all good and well, but you are doctoring around with a symptom. You should really try to figure out where the contents of _lastUpdatedBy comes from and why it is what it is. You might have a serious bug here and trying to cover it up. For example, there might be a bug that stores rubbish data instead of the correct data, and you are just covering up for that bug.
Hope you can help me, I am a bit of a newbie and have progressed without needing help but now I am stuck.
Firstly could somebody please tell me what this is below, I think it is ASCII but maybe wrong???
"\0\u{01}\0\0\0\u{1E}\0\0\u{02}\u{06}\0ÿ\0\0\0\0\0\u{02}.\u{01}(\0\t\0ü\0\u{07}\0\u{04}"
Secondly how would I convert this to an NSArray so I could get iterate through it and get a value at a given location in the array?
I am using Swift
Many thanks in advance
Charles
As mentioned above, your string looks quite weird, and the \u{xx} values tell Swift to replace that part with the unicode character corresponding to hexadecimal value xx (see unicode-table).
I do not know why you would want to convert it to the Objective-C type NSArray while you could just use the Swift type Array. By that, I mean you can generate an array of the string by using var strArray = Array(str), where your string is stored in str. From that point on, you can use the following code to iterate over the contents of the string:
for var char in strArray {
//do something with char
}
char then loops through all indices of the strArray, holding the value at the current index.
Here's what happens:
Internal database stuff: one class has a string property on it, that stores a phone number. This number is set using the code
CFBridgingRelease(ABMultiValueCopyValueAtIndex(ABRecordCopyValue(record, kABPersonPhoneProperty), 0));
My function: finds all objects of this type, and stores phone numbers of each object in an NSMutableSet.
Debug: I print the description of the set to the console.
Results:
Some of the set's objects look as expected (the majority actually): "+64 27 0124 975"
Some are missing quotation marks: 027 7824 565
Some have weird unicode symbols: "021\U00a0026\U00a017788"
My question:
Why the difference - what does it mean, and do I need to fix anything?
NSLog with %# – as I assume you are using – has some intelligence in how it presents NSStrings as it calls the description method. If the string has anything other than alphanumerics, such as the '+' or '\' above, it will use quotes. The string with unicode characters simply has its characters encoded as shown, and they are automatically converted into this lossless format. You should be able to convert it to something prettier for the console if you really need to with something like this:
NSLog(#"%#", [NSString stringWithCString:[myString.description cStringUsingEncoding:NSASCIIStringEncoding] encoding:NSNonLossyASCIIStringEncoding]);
I have read a multiline file and converted it to a list with the following code:
Lines = string:tokens(erlang:binary_to_list(Binary), "\n"),
I converted it to a string to do some work on it:
Flat = string:join(Lines, "\r\n"),
I finished working on the string and now I need to convert it back to a multiline list, I tried to repeat the first snippet shown above but that never worked, I tried string:join and that didnt work.. how do i convert it back to a list just like it used to be (although now modified)?
Well that depends on the modifications you made on the flattened string.
string:tokens/2 will always explode a string using the separator you provide. So as long as your transformation preserves a specific string as separator between the individual substrings there should be no problem.
However, if you do something more elaborate and destructive in your transformation then the only way is to iterate on the string manually and construct the individual substrings.
Your first snippet above contains a call to erlang:binary_to_list/1 which first converts a binary to a string (list) which you then split with the call to string:tokens/2 which then join together with string:join/2. The result of doing the tokens then join as you have written it seems to be to convert it from a string containing lines separated by \n into one containing lines separated by \r\n. N.B. that this is a flat list of characters.
Is this what you intended?
What you should do now depends on what you mean by "I need to convert it back to a multiline list". Do you mean everything in a single list of characters (string), or in a nested list of lines where each line is a list of characters (string). I.e. if you ended up with
"here is line 1\r\nhere is line 2\r\nhere is line 3\r\n"
this already is a multiline line list, or do you mean
["here is line 1","here is line 2","here is line 3"]
Note that each "string" is itself a list of characters. What do you intend to do with it afterwards?
You have your terms confused. A string in any language is a sequence of integer values corresponding to a human-readable characters. Whether the representation of the value is a binary or a list does not matter, both are technically strings because of the data they contain.
That being said, you converted a binary string to a list string in your first set of instructions. To convert a list into a binary, you can call erlang:list_to_binary/1, or erlang:iolist_to_binary/1 if your list is not flat. For instance:
BinString = <<"this\nis\na\nstring">>.
ListString = "this\nis\na\nstring" = binary_to_list(BinString).
Words = ["this", "is", "a", "string"] = string:tokens(ListString, "\n").
<<"thisisastring">> = iolist_to_binary(Words).
Rejoined = "this\r\nis\r\na\r\nstring" = string:join(Words, "\r\n").
BinAgain = <<"this\r\nis\r\na\r\nstring">> = list_to_binary(Rejoined).
For your reference, the string module always expects a flat list (e.g., "this is a string", but not ["this", "is", "a", "string"]), except for string:join, which takes a list of flat strings.