How to print text from array with some other string? [duplicate] - objective-c

This question already has answers here:
Append string with variable
(4 answers)
Shortcuts in Objective-C to concatenate NSStrings
(30 answers)
Closed 5 years ago.
I'm new to Objective-C and I couldn't find a direct solution for my problem where it is explained how to add string behind or in front of an array value .
I try to get some text from an array and add some other string behind or infront of it (will be used in tableviewcell). In Android it's working just with + signs, that's why I couldn't handle it here.
I try to do something like below:
"some text" +[arrayName objectAtIndex:indexPath.row] + "some text"

You need to use stringWithFormat to concatenate those values, or you can use NSMutableString as well
Use this
NSString* finalText = [NSString stringWithFormat:#"some text %# some text",[arrayName objectAtIndex:indexPath.row]];
Another way
NSMutableString* finalString = [NSMutableString stringWithString:#"test Text"];
[finalString appendString:[arrayName objectAtIndex:indexPath.row]];
[finalString appendString:#"test Text 2"];

Short:
[NSString stringWithFormat:#"%#/%#/%#", "some text", [arrayName objectAtIndex:indexPath.row], "some text"];
For further answers see: https://stackoverflow.com/a/510444/9310455

Related

How to print a String object within a String in Objective C [duplicate]

This question already has answers here:
Shortcuts in Objective-C to concatenate NSStrings
(30 answers)
Closed 3 years ago.
I have a String object:
NSString* name = #"John Smith";
and another String object:
NSString* intro =#"Hello this is ";
I want to be able to change the name from the UI make it within the introduction, so I tried:
_introduction = (#"Hello this is, %#", _name);
However, only the name is getting printed when I do:
NSlog(#"%#",_introduction);
I want both sentences to be printed by printing the intro object.
I'm not sure what (#"Hello this is, %#", _name); does or if that's even correct syntax, but typically to do what you're trying, you'd do something like this:
introduction = [NSString stringWithFormat:#"%# %#", intro, name];

How to Localize NSString with format [duplicate]

This question already has an answer here:
iOS: How to localize strings with multiple interpolated parameters?
(1 answer)
Closed 5 years ago.
How to localize the NSString with format.
int value = 20;
NSString *str = #"hello";
textLabel.text = [NSString stringWithFormat:#"%d %#", value, str];
I tried
textLabel.text = [NSString stringWithFormat:NSLocalizedString(#"%d %#", #"%d %#"), value, str];
but didn't work. Any help is appreciated.
Your localized string itself must be a format pattern:
"ValueAndStringFMT" = "Value %1$d and string %2$#";
And in your code:
textLabel.text = [NSString
stringWithFormat:NSLocalizedString(#"ValueAndStringFMT"),
value, str
];
Why %1$d and not just %d? So you can change the order. E.g. in some language you may like to have the order swapped:
"ValueAndStringFMT" = "Cadena %2$# y valor %1$d";
Of course, that is somewhat dangerous as if someone uses more placeholders than your string call offers or uses the wrong types, your app may crash. If you want to be safe, you do a search an replace instead:
"ValueAndStringFMT" = "Value [[VALUE]] and string [[STRING]]";
And in your code:
NSString * string = NSLocalizedString(#"ValueAndStringFMT");
string = [string stringByReplacingOccurrencesOfString:#"[[VALUE]]"
withString:#(value).stringValue
];
string = [string stringByReplacingOccurrencesOfString:#"[[STRING]]"
withString:str
];
textLabel.text = string;
That way the worst case scenario is that a placeholder is not expanded, meaning the placeholder is visibly printed on screen but at least your app won't crash because someone messed up the localization strings file.
If you need to localize one of the format variables, then you need to do that first in an own step:
NSString * str = NSLocalizedString(#"hello");

Objective C remove end of string after certain character [duplicate]

This question already has answers here:
Remove Characters and Everything After from String
(2 answers)
Closed 8 years ago.
I can't seem to find the answer to this anywhere. I can do it in c but objective c is difficult.
I want to cut the end of a string after a certain character
so user#example.com will become user (cut at '#')
How do I do this?
This will give you the first chunk of text that comes before your special character.
NSString *separatorString = #"#";
NSString *myString = #"user#example.com";
NSString *myNewString = [myString componentsSeparatedByString:separatorString].firstObject;
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:
You can use a combination of substringToIndex: and rangeOfString: methods, like this:
NSString *str = #"user#example.com";
NSRange pos = [str rangeOfString:#"#"];
if (pos.location != NSNotFound) {
NSString *prefix = [str substringToIndex:pos.location];
}
Notes:
You need to check the location against NSNotFound to ensure that the position is valid.
substringToIndex: excludes the index itself, so the # character would not be included.

How to cut a string at a specific point? [duplicate]

This question already has answers here:
NSString tokenize in Objective-C
(9 answers)
Closed 10 years ago.
So, I have a vast quantity of NSStrings and my problem is I need to cut them into smaller strings at a specific point. This may sound complicated but what I need basically is this:
NSString *test =" blah blah blah - goo goo goo.";
NSString *str1 = "blah blah blah ";
NSString *str2 = "goo goo goo";
How do I code for when there's a hyphen for the string to just cut off there. Is there a way to do this? I found ways to cut of the string after a certain amount of letters but I need it at the hyphen every time.
NSArray *arr = [string componentsSeparatedByString:#"-"];
should do the trick.
You could do this many ways. Two answers above show a few approaches. Many Objective-C solutions will include NSRange usage. You could also do more flexible things with NSScanner or NSRegularExpression.
There is not going to be one right answer.
NSString *cutString = [text substringFromIndex:3];
cutString = [text substringToIndex:5];
cutString = [text substringWithRange:NSMakeRange(3, 5)];

How to get the substring of a string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to get substring of nsstring?
how do I get a substring in iOS?
I am new to iphone.In my project i have a string such as chapter1 but actually i want the substring 1 only how it is possible.if any body know this please help me
Check out the substringFromIndex: or substringToIndex: methods of NSString. You can find the documentation for NSString here.
Ex:
NSString *string = #"Chapter1";
NSString *newString = [string subStringFromIndex: ([string length] - 1)]; // => "1"