problems with NSString - iphone

I am sorry of the question, but i have searched but i have not found the solution.
I have a textField, and i would like to add a caractère to the text of my textField. how to do this, please ?
if ( myConddition) {
otherTextField.text = myTextField.text ( a would like to add a caractér("1") to the first position ).
thanks for your answers

otherTextField.text = [NSString stringWithFormat:#"1%#", myTextField.text];

Use below
NSMutableString* myString = [NSMutableString alloc] appendFormat:#"%#%#",#"1", myTextField.text]];
myTextField.text = myString;
[myString release];

otherTextField.text = [#"1" stringByAppendingString:myTextField.text];

Assuming you've created #property declarations...
[[self otherTextField] setText:[NSString stringWithFormat:#"1%#", [[self myTextField] text]]];
P.S. Eww at the use of dot notation by you boys and girls.

Related

Xcode write NSLog result in UILabel

I use NSLog(#"%#");
And the NSLog result is '23204239423'
I don't explain you why ... it's just an example.
I want the NSLog result appear in UILabel, is it possible ?
I tried : Label.text = (#"%#"); , it doesn't work.
Do you know how i can do it ?
Thanks.
NSLog usually takes a string like:
NSLog(#"%#", string);
So instead just do this:
label.text = [NSString stringWithFormat:#"%#", string];
You have to put a variable
NSString *text = #"My Text";
label.text = text;
Or
label.text = #"Your text";

Get first sentence of textview

I am trying to get the first sentence of a text view. I have the following code but am getting an out of bounds error. Thank You. Or are there any ways that aren't really complex.
-(IBAction)next:(id)sender
{
NSRange ran = [[tv.text substringFromIndex:lastLocation] rangeOfString:#". "];
if(ran.location != NSNotFound)
{
NSString * getRidOfFirstHalfString = [[tv.text substringFromIndex:lastLocation] substringToIndex:ran.location];
NSLog(#"%#",getRidOfFirstHalfString);
lastLocation+=getRidOfFirstHalfString.length;
}
How about:
NSString *finalString = [[tv.text componentsSeparatedByString:#"."] objectAtIndex:0] // Get the 1st part (left part) of the separated string
Go through the textview's text and divide the text into separate components where you find a period by calling componentsSeperatedByString on tv.text. You want the first sentence, which would be the 0th object in the array.
I know you've already accepted an answer to this question, but you might want to consider using the text view's tokenizer instead of just searching for the string ". " The tokenizer will automatically handle punctuation like !, ?, and closing quotes. You can use it like this:
id<UITextInputTokenizer> tokenizer = textView.tokenizer;
UITextRange *range = [tokenizer rangeEnclosingPosition:textView.beginningOfDocument
withGranularity:UITextGranularitySentence
inDirection:UITextStorageDirectionForward];
NSString *firstSentence = [textView textInRange:range];
If you want to enumerate all of the sentences, you can do it like this:
id<UITextInputTokenizer> tokenizer = textView.tokenizer;
UITextPosition *start = textView.beginningOfDocument;
while (![start isEqual:textView.endOfDocument]) {
UITextPosition *end = [tokenizer positionFromPosition:start toBoundary:UITextGranularitySentence inDirection:UITextStorageDirectionForward];
NSString *sentence = [textView textInRange:[textView textRangeFromPosition:start toPosition:end]];
NSLog(#"sentence=%#", sentence);
start = end;
}
Try checking that the substring was actually found.
NSRange ran = [tv.text rangeOfString:#". "];
if(ran.location != NSNotFound)
{
NSString * selectedString = [tv.text substringToIndex:ran.location];
NSLog(#"%#",selectedString);
}
You could alternatively try using NSScanner like this:
NSString *firstSentence = [[NSString alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:tv.text];
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"."];
[scanner scanUpToCharactersFromSet:set intoString:&firstSentence];
I'm not sure if you want this, but since you want the first sentence, you could append a period (you probably know how to do this, but it doesn't hurt to show it anyway):
firstSentence = [firstSentence stringByAppendingFormat:#"."];
Hope this helps!
PS: If it didn't work for you, maybe the text view doesn't actually contain any text.

Adding string Elements in NSArray

I have mutable string array named arrayout.
It is having 3 element .Now I want to add 1 String element that array.But when I try to add,it is taking null value....Cant get what to do...Please help...
My code is :
NSString *ds1 = #"--";
[arrayout arrayByAddingObject:ds1];
NSLog(#"arrrrr '%#'",arrayout);
Try this out:
NSString *ds1 = #"--";
[arrayout addObject:ds1];
NSLog(#"arrrrr '%#'",arrayout);
Hope this helps you.
You can also do it this way:
NSMutableArray *arrayout = [[NSMutableArray alloc] init]; // alloc here
[arrayout insertObject:#"SomeText Here" atIndex:[arrayout count]]; // insert here
NSLog(#"Appended Array: '%#'",arrayout); // Print here
this will populate arrayout with SomeText Here.
Hope it helps!
Why are you concatenating strings like this ? You can just do something simple like
NSString* newString = [NSString stringWithFormat:#"%#/%#/%#", string1, string2, string3];

NSString Manipulation

I would like to manipulate the NSString like as
(0) Likes (1). (see. (2))
(0) = Raman
(1) = You
(2) = ThisGift
to
Raman Likes You. (see. ThisGift)
I dont know what approch can solve this problem.
Thanks in Advance,
Regards
Venkat.
-[NSString stringByReplacingOccurrencesOfString:withString:].
You use it like this:
NSString * source = #"(0) Likes (1). (see. (2))";
source = [source stringByReplacingOccurrencesOfString:#"(0)" withString:#"Raman"];
NSLog(#"%#", source); //logs "Raman Likes (1). (see. (2))"
If you're allowed to change the template format, you can use format strings.
NSString *template1 = #"%1$# Likes %2$#. (see. %3$#)",
*template2 = #"%2$# got a %3$# from %1$#.";
NSString *msg1 = [NSString stringWithFormat:template1,#"Raman",#"You",#"ThisGift"],
*msg2 = [NSString stringWithFormat:template2,#"Raman",#"You",#"ThisGift"];
or (if the format string can always depend on the arguments being in replacement order):
NSString *template = #"%# Likes %#. (see. %#)";
NSString *msg = [NSString stringWithFormat:template,#"Raman",#"You",#"ThisGift"];
See -[NSString stringByReplacingOccurrencesOfString:withString:]
NSString *foo = #"(0) likes (1). (see (2))";
NSString *bar;
bar = [foo stringByReplacingOccurrencesOfString:#"(0)"
withString:#"Raman"];
bar = [bar stringByReplacingOccurrencesOfString:#"(1)"
withString:#"You"];
bar = [bar stringByReplacingOccurrencesOfString:#"(2)"
withString:#"ThisGift"];

How to get a string from a UITextField?

How would I go about getting a string from a UITextField in the iPhone SDK? I'm trying to insert it into another concatenated string.
This should do it:
NSString *myString = myTextField.text;
NSString *s = textfield.text;
yourString = [yourString appendString:s];
lblAns.text=[NSString stringWithFormat:#"%i",[[txtField1 text] intValue] + [[txtField2 text] intValue]];
you can do addition of two textboxs value like this...
for Swift 3.0
place these lines where you need
let yourString: String = ""
yourString = textField.text
Actually text attribute of textField provide the value to String.