I have a slider for testing and I want the characters represented by the slider position to be shown in a text field. But my text field only shows A-Z and a-z when running my solution below. How can I get Unicode characters into my text field?
- (IBAction) doSlider: (id) sender
{
long long theNum = [sender intValue];
NSString *vc_theString = [NSString stringWithFormat:#"%c", theNum];
[charField setStringValue:vc_theString2];
}
%c is inherited from C and limited to an 8-bit range. You should use %C, which will read in the corresponding argument as a unichar.
Related
I have this line of code
// valueX is a long double (long double is a huge floating point)
NSString *value = [NSString stringWithFormat: #"%.10Lg", valueX];
This format specifier is specifying up to 10 decimal digits but I don't want to hard code this to 10.
I have this variable numberOfDigits that I want to be used to define the number of digits. For those itching to down vote this question, it is not so easy as it seems. I cannot substitute the 10 with %# because %.10Lg is a format specifier by itself.
OK, I can create a bunch of strings like #"%.5Lg", #"%.8Lg", #"%.9Lg"... and switch that, but I wonder if there is another way...
There is, if you read the manual pages for format specifiers. You can replace the precision with *, which means it will get taken from a parameter instead.
int numDigits = 10;
NSString *value = [NSString stringWithFormat:#"%.*Lg", numDigits, valueX];
I couldn't find this in the core foundation reference, but I know that this is written in the man 3 printf man page.
Dietrich's answer is the simplest and therefore best. Note that even if there wasn't a built-in way to specify the number of digits with a parameter you could still have done it by first building your format string and then using it:
- (NSString *) stringFromValue: (long double) value digits: (int) digits; {
//First create a format string. Use "%%" to escape the % escape char.
NSString *formatString =[NSString stringWithFormat: #"%%.%dLg", digits];
return [NSString stringWithFormat: formatString, value];
}
I am looking to get the actually unicode value of a character, and put it into an escaped string. I set the title of an NSButton to ± , now I am trying to get the title of this button and convert it back to this "\U00B1";
For Example
unichar theChar = [theButton.title characterAtIndex:0];
//now how would I change the above unichar to an string like #"\\u03b2"
One solution would be:
unichar theChar = ...
NSString *escapeCode = [NSString stringWithFormat:#"\\\\u%04x", theChar];
Note how you need four backslashes to get two in the resulting string.
I am using a NSScanner to detect text bound by [square brackets] in a string, and convert it into HTML hyperlinks. I hope to turn convert this text in the following way:
This [is an] example of the [text] I'm using
should convert to
This is an example of the text I'm using
I've implemented a NSScanner, but in order to make it work properly, I need to extract both the first and second of the square brackets.
Here's the current state of my code:
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:stringWithBrackets];
NSString *stringWithoutBrackets;
while ([theScanner isAtEnd] == NO) {
[theScanner scanUpToString:#"[" intoString:NULL];
[theScanner scanUpToString:#"]" intoString:&text];
<BREAK>
At this breakpoint, I have a string returned, which does not contain the closing bracket. So, for the example text string shown above, the contents of NSString *text at the first breakpoint is
[is an
In order to properly manipulate the string, I need to work with both the opening and closing bracket.
Essentially, my question is: how do I advance the NSScanner on one character, and include that character into the variable 'text'?
You can skip the ] character with scanString:
if ([theScanner scanString:#"]" intoString:NULL]) {
text = [text stringByAppendingString:#"]"];
} else {
// Next character is not ']'
}
The else case would only happen if there is no matching ] character. text contains the part from [ to the end of the string in that case.
Alternatively, you could use NSRegularExpression and e.g. the matchesInString method which returns an array of all ranges in the string matching the regular expression.
a noob question here.
I am trying to make an automatic search and replace process for characters' ASCII values in a string.
so, I have a string constructed from a content of a UITextField
NSString *searchText;
searchText = (mmText.text);
then I do a little loop and check all entered characters for their ASCII values. if they're not in the allowed range I want to search and replace them with something else (? for now)
so let's say I am in the loop and I get to a ASCII 45 character (it's a minus sign):
int asciiCode = 45;
now I would like to find the ASCII 45 character in the string and replace it with a question mark
This is what I am doing at the moment:
NSString *ascStr = [NSString stringWithFormat:#"%c", asciiCode];
NSRange matchSpace;
matchSpace = [searchText rangeOfString: ascStr];
if (matchSpace.location == NSNotFound)
{}
else
NSMutableString *searchandReplace = [NSMutableString stringWithString: searchText];
[searchandReplace replaceCharactersInRange: [searchandReplace rangeOfString: ascStr] withString: #"?"];
mmText.text = searchandReplace;
}
This works fine for a regular ASCII value (0-255), but it doesn't seem to work for the extended ASCII values coming from foreign languages. For example when using the Korean language mode, one of the main character looks like a double crossed W, but when printed via NSLog it looks like a copyright sign. This is probably the reason the search and replace procedure doesn't work for it. It has an ASCII value of 8361.
any ideas ? thank you!
it turns out it was as simple as changing:
NSString *ascStr = [NSString stringWithFormat:#"%c", asciiCode];
to
NSString *ascStr = [NSString stringWithFormat:#"%C", asciiCode];
%c
8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \ddd or the Unicode hexadecimal format \udddd, where d is a digit
%C
16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \ddd or the Unicode hexadecimal format \udddd, where d is a digit
I am trying to grab a number from a text box entry, convert it to string and grab the length of it. (IE: Perform a LEN(someInteger) on a number).
I have been able to get it working, however there are some inconsistencies when the number is 0 or null.
For example;
-(IBAction)update
{
// inputField is the number pad textbox
double myInput = [inputField.text doubleValue];
//
NSNumber *c = [NSNumber numberWithDouble:myInput];
NSString *myOutput = [c stringValue];
NSLog(#"Starting number (number) = %#", c);
NSLog(#"myOutput (string) = %#", myOutput);
NSLog(#"myOutput (length) = %d", ([myOutput length]) );
}
The conversion for number to string works fine. The problem I have is with the length, especially when there are no numbers (or null) entry.
I have a number pad text box entry on an XIB which has a "text" of 0 (the placeholder just says "Enter number here")
When you first start typing into the text box the "text" goes away and you start with a blank text box.
Problem 1 -- when you first enter the text box there is nothing in the textbox, but my NSLog says the output length is 1 character long.
This causes a problem because visually there is nothing in the textbox but the [myOutput length] reports 1 character length.
Problem 2 -- when you start to enter numbers, everything goes well -- but when you start to remove numbers to the point where you clear the text box completely it reports a length of 1 character.
How can it read 1 character long if there is nothing in the text box? I think it must be the "text" (From identity inspector) again.
Summary.
There is a number pad text box entry field. Whenever you update the entry, it calls the IBaction update method.
When you first enter numbers into the text box there is nothing visually displayed in the input field, but the NSLog reports the length is 1 character long.
When you start entering numbers and then start to remove them one at a time till you completely remove all numbers it reports the length as being 1 character long.
In both cases the NSLog should be reporting 0 length, but it never does. I tried doing [myOutput length] - 1 but this gives me weird results.
Any help on this would be great
Thanks.
If there's no text, myInput will be equal to 0.0. myOutput will then be equal to #"0", which has a length of 1.
Why not just use [inputField.text length]?
You could do something like this:
NSNumber *number = nil;
if ([inputField.text length] > 0) {
number = [NSNumber numberWithDouble:[inputField.text doubleValue]];
}
That way you have your number if it exists, otherwise number will be nil.