I've got HEX colors as NSStrings and want to check if a particular string contains an F at the first, third, or fifth character, but to ignore it if it contains an F at the second, fourth or sixth character.
This is to identify if the color in question is a light color or not.
I searched for this, but only found answers regarding how to find character ranges.
You can use the characterAtIndex member of NSString.
char fifthChar = [yourString characterAtIndex:5];
As #Daniel Pereira says, you can use the -[NSString characterAtIndex:] method to check characters.
NSString *string = /* Assume this exists */;
if ([string characterAtIndex: 1] == 'F' || \
[string characterAtIndex: 3] == 'F' || \
[string characterAtIndex: 5] == 'F')
{
// F at odd-indexed character
}
else
{
// Do other stuff here...
}
Have you tried to use characterAtIndex? You can use the method to retrieve the string at the first, third and fifth character (of course one at a time) and compare it with F.
Note that all your HEX colors must be 6 characters. Sometimes, you can represent a color with only three hex values (eg. FFF would still give you white in HTML). So if your color is represented using three chars only, and you try to get the char at index 5, the method will throw an 'out of range' exception.
Related
I would like to modify a string that will have make the first letter capitalized and all other letters lower cased, and anything else will be unchanged.
I tried this:
function new_string=switchCase(str1)
%str1 represents the given string containing word or phrase
str1Lower=lower(str1);
spaces=str1Lower==' ';
caps1=[true spaces];
%we want the first letter and the letters after space to be capital.
strNew1=str1Lower;
strNew1(caps1)=strNew1(caps1)-32;
end
This function works nicely if there is nothing other than a letter after space. If we have anything else for example:
str1='WOW ! my ~Code~ Works !!'
Then it gives
new_string =
'Wow My ^code~ Works !'
However, it has to give (according to the requirement),
new_string =
'Wow! My ~code~ Works !'
I found a code which has similarity with this problem. However, that is ambiguous. Here I can ask question if I don't understand.
Any help will be appreciated! Thanks.
Interesting question +1.
I think the following should fulfil your requirements. I've written it as an example sub-routine and broken down each step so it is obvious what I'm doing. It should be straightforward to condense it into a function from here.
Note, there is probably also a clever way to do this with a single regular expression, but I'm not very good with regular expressions :-) I doubt a regular expression based solution will run much faster than what I've provided (but am happy to be proven wrong).
%# Your example string
Str1 ='WOW ! my ~Code~ Works !!';
%# Convert case to lower
Str1 = lower(Str1);
%# Convert to ascii
Str1 = double(Str1);
%# Find an index of all locations after spaces
I1 = logical([0, (Str1(1:end-1) == 32)]);
%# Eliminate locations that don't contain lower-case characters
I1 = logical(I1 .* ((Str1 >= 97) & (Str1 <= 122)));
%# Check manually if the first location contains a lower-case character
if Str1(1) >= 97 && Str1(1) <= 122; I1(1) = true; end;
%# Adjust all appropriate characters in ascii form
Str1(I1) = Str1(I1) - 32;
%# Convert result back to a string
Str1 = char(Str1);
I need to determine if a string contains just an integer. The built-in function isinteger is not working.
To avoid loops I'd like to apply this task on cell arrays of strings.
For example:
Q = { 'qf5' ; '4' ; 'true' ; 'false' ; '4.00' ; '4E0' ; '4e0' ; '657' };
desired result:
integers = 0 1 0 0 0 0 0 1
For a single string I figured out an ugly workaround, but I can't imagine that this is the only possible way, and also it requires a loop to use it on cell arrays:
myString = '4';
integer = uint64( str2double( myString ) );
newString = int2str( integer );
isStringInteger = strcmp(newString,myString);
Which essential function am I missing?
You can do it with regexp; and to avoid the loop you use cellfun:
~cellfun('isempty', regexp(Q, '^-?\d+$'))
This considers an "integer" as a string of digits, possibly with one minus sign at the beginning.
Note that cellfun with the builtin function 'isempty' is very fast.
Well, the string is not an integer, therefore the question as such is not correct. What you want to check is whether the string is a representation of an integer. The isinteger function is also not what you want, because it does not check whether the actual content of a numeric variable is an integer, but whether the data type is an integer type.
As far as I can tell, there is no built-in way to check whether a string represents an integer. One approach to implement such a check would be to see whether all the characters in the string represent digits:
isintstr = all(myString >= '0') && all(myString <= '9')
This code takes advantage of the fact that the decimal digits are encoded in sequence in ASCII and Unicode.
To allow for leading and trailing white space, use
isintstr = all(strtrim(myString) >= '0') && all(strtrim(myString) <= '9')
I am used to doing this in C or C++, ie:
myChar++;
should increment a letter.
I am trying to do the same in Objective-C, except that I have a NSString to start off with (the NSString is always just one letter). I have tried converting the NSString to a char *, but this method is deprecated and other ways of achieving this don't seem to work.
How should I convert an NSString to a char * - or, is there a way to increment a character in objective-c without needing a char * somehow?
Thanks :)
// Get the first character as a UTF-16 (2-byte) character:
unichar c = [string characterAtIndex:0];
// Increment as usual:
c++;
// And to turn it into a 1-character string again:
[NSString stringWithCharacters:&c length:1];
Of course, this assumes incrementing a Unicode character makes sense, which does for ASCII-range characters but probably not for others.
How about NSString's
- (unichar)characterAtIndex:(NSUInteger)index;
Would that work?
I have on one string like #"K_h_10_K_d_10_K_c_13_T_c_13_T_s_13"
I separate them by #"_"
using appCardString=[substringAppCard componentsSeparatedByString:#"_"];
then I have to convert them in to char and want to put in char[] ....
how can I do that ..
please help me ....
It's crashing here
appusedFaces[i]=[[NSString stringWithFormat:#"%#",[appCardString objectAtIndex:i]] charValue];
This will work:
appusedFaces[i]=[[appCardString objectAtIndex:i] characterAtIndex:0];
Though you should add a check that the string has at least one character. You should also be aware that char can only hold character codes up to 255 (unichar can handle any Unicode character).
It also looks like you have some numeric codes in your test string. Checking if the string has more than one character and then calling [[appCardString objectAtIndex:i] intValue] for those characters will handle these.
I am working on an iPhone application and need to make sure the entered text is composed of only a to z and numbers.
I don't want the user to use other languages letters like those with accents or dots above them.
EDIT: I am new to this RegEx, so if you please give me a code or a link, i will be really thankful.
Use a regular expression, like [0-9a-zA-Z]+. Check out the RegexKit Framework for Objective C, a regular expression library that works on the iPhone.
You can then do something like:
NSString *str = #"abc0129yourcontent";
BOOL isMatch = [str isMatchedByRegex:#"^[0-9a-zA-Z]+$"];
One more approach (may be not so elegant as with RegEx):
NSCharacterSet* tSet = [NSCharacterSet characterSetWithCharactersInString:
#"abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
NSCharacterSet* invSet = [tSet invertedSet];
NSString* legalS = #"abcdA1";
NSString* illegalS = #"asvéq1";
if ([legalS rangeOfCharacterFromSet:invSet].location != NSNotFound)
NSLog(legalS); // not printed
if ([illegalS rangeOfCharacterFromSet:invSet].location != NSNotFound)
NSLog(illegalS); // printed
The simplest way - assuming you want to allow for punctuation as well is to check that all the characters are between ascii values 32 (space) and 126 (~). The ASCII table illustrates the point.
All the accented characters are what we used to call "high" ascii when I first started programming.
If you don't want to allow punctuation you'll have to do several range checks:
48 to 57 for numbers
65 to 90 for upper case
97 to 122 for lower case
which might make the RegEx approach more readable (I can't believe I just wrote that)
Easy implementation (take from above, edited just for fast copy-paste):
Objective-C:
BOOL onlyEnglishAlphanumerical = ([self.textField.text rangeOfCharacterFromSet:[[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"] invertedSet]].location == NSNotFound);
self.textField.layer.borderWidth = (onlyEnglishAlphanumerical) ? 0 : 1;
if (onlyEnglishAlphanumerical)
{
//Do the deal
}
Swift:
if (self.textField.text.rangeOfCharacterFromSet(NSCharacterSet.init(charactersInString:"abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ").invertedSet) == nil)
{
//Do the deal
}