0
votes

if I have a UILabel: label1.text (maximum length 6 character) In this example GARAGE is the word in it.

is it possible to have code that would make

label1a: G
label1b: A
label1c: R
label1d: A
label1e: G
label1f: E

I need this so I can compare every character in a word with every character in a other word.

Been struggling with this for a couple of days now :S

edit

I got a random word generator that generates word with a length of 6 like 'GARAGE' or 'SAILOR' it generates it into a label for this We'll call the label: 'randomword.text'

now We got a input label where the character display that we put in with our own made keyboard. this label is 'input.text'

now we can compare those 2 labels with if (randomword.text == input.text') { NSLog: @'these words are the same' };

What I want to do is to seperate every letter in random.text: so have label1 say G, label2 say A, label3 say R, label4 say A, label5 say G, label6 say E.

If I separate my input.text in the same way like I did above with random.text I got another 6 UILabel for example have this one separated in label7,8,9,10,11,12. Now I can compare label 1 to label 7, label 2 to 8. etc That way I can see if Letters are on the place they should be even if the entire word do not equal each other.

1
so basically you wants all characters of label's text, correct? - rishi
yes I want them seperated automaticly so I can compare them but it has to be automatic since the word in label1 is different everytime with a random word generator but I got that all working fine already - Kevin
if you want to split string into characters then you can use - for(int i =0 ;i<[myString length]; i++) { char = [myString characterAtIndex:i]; } - rishi
although it seems to me like you need something else with it also which i am not getting.Can you elaborate some more? - rishi
I don't have code avaiable ATM, since im not at my home. But I have a label1.text with a length of 6, Random words generate into that with a random word generator for example the word GARAGE. now I have another label in what I input words with a length of 6. I want to compare every character in the 2 words to eachother So I want to seperate every character in every word into their own label So I can compare the labels with length 1. I've got the code for doing it with strings but how would I change label1.text into NSString *string1? - Kevin

1 Answers

1
votes

Still confused by all that stuff about labels. I don't think your question has anything to do with labels, but with comparing strings or parts of them. So if you wan't to check if to strings are equal use

[aString isEqualToString:anotherString]; (not ==).

If you want to extract a letter from a string use

[aString characterAtIndex:i]; or [aString substringWithRange:NSMakeRange(i, i+1)] .

If you want to check if a letter is on a certain position (say i) in a string use:

NSString *aLetter;
[[aString substringWithRange:NSMakeRange(i, i+1)] isEqualToString:aLetter];

or

unichar aLetter;
[aString characterAtIndex:i] == aLetter;

If you want to get 'what's shown on a UILabel' as NSString use aLabel.text .

Hope that helps.