Fetch Substring from NSString - ios

I have the following string :
Item 1#Item 2#Item 3#Item 4#Item 5#Item 6#Item 7#Item 8#Item 9#Item 10#Item 11#Item 12#Item 13#Item 14#Item 15#
I want to extract each Item name. How can it be done?

try this
NSString *temp = #"Item 1#Item 2#Item 3#Item 4#Item 5#Item 6#Item 7#Item 8#Item 9#Item 10#Item 11#Item 12#Item 13#Item 14#Item 15#";
NSArray *arr = [temp componentsSeparatedByString:#" "];
NSLog(#"%#", arr);

Separate each string
NSString *str = #"Item 1#Item 2#Item 3#Item 4#Item 5#Item 6#Item 7#Item 8#Item 9#Item 10#Item 11#Item 12#Item 13#Item 14#Item 15#";
NSArray *components = [str componentsSeparatedByString:#"#"];

NSString *string=#"Item 1#Item 2#Item 3#Item 4#Item 5#Item 6#Item 7#Item 8#Item 9#Item 10#Item 11#Item 12#Item 13#Item 14#Item 15#";
NSArray *array=[[NSArray alloc] initWithObjects:[string componentsSeparatedByString:#"#"]];

Related

Fetching data from SQLite and want to get only the last value of column id

I am fetching data from SQLite and want to get only the last value of column id in XCode.The code is
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSString *valvar;
valvar = [_uid lastObject];
NSNumber *custval = [_uid valueForKey: #"#lastObject"];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",custval,"1"];
NSLog(#"%#", imgval1);
Please tell me how can I get only the value because by using the above code I am getting array with last value of id.
I think this your case, try this it maybe help you
NSArray *temp=[NSArray arrayWithObjects:#"1",#"2",#"3", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
// NSString *tmmp=[temp0ne lastObject];
NSArray *finalStr=[uid lastObject];
NSLog(#"Dictionary is---->%#",[finalStr lastObject]);
Output:
3_1
EDIT
NSArray *temp=[NSArray arrayWithObjects:#"(1)",#"(2)",#"(3)", nil];
NSArray *temp0ne=[[NSArray alloc]initWithArray:temp];
NSString *tmmp=[temp0ne lastObject];
NSString *final=[tmmp stringByReplacingOccurrencesOfString:#"(" withString:#""];
final=[final stringByReplacingOccurrencesOfString:#")" withString:#""];
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",final,"1"];
NSLog(#"%#", imgval1);
I don't know is this correct way or not try this....otherwise have look this link
I don't fully understand your code structure hehe. Try this:
NSString *selquery = #"select id from watchlists";
if (self.uid != nil) {
self.uid = nil;
}
self.uid = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:selquery]];
NSNumber *custval = [_uid objectAtIndex:[_uid count]-1];
*
NSString *str = [NSString stringWithFormat#"%#",custval];
str = [str stringByReplacingOccurrencesOfString:#"("
withString:#""];
NSString *finalCustval = [NSString stringWithFormat#"%#",str];
finalCustval = [finalCustval stringByReplacingOccurrencesOfString:#")"
withString:#""];
*
NSString *imgval1 = [NSString stringWithFormat:#"%#_%s",finalCustval ,"1"];
NSLog(#"%#", imgval1);
UPDATE
try adding the ones with *.

how to convert NSString to NSArray [duplicate]

This question already has answers here:
Comma-separated string to NSArray in Objective-C
(2 answers)
Closed 8 years ago.
I have a string like
NSString* str = #"[90, 5, 6]";
I need to convert it to an array like
NSArray * numbers = [90, 5 , 6];
I did a quite long way like this:
+ (NSArray*) stringToArray:(NSString*)str
{
NSString *sep = #"[,";
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:sep];
NSArray *temp=[str componentsSeparatedByCharactersInSet:set];
NSMutableArray* numbers = [[NSMutableArray alloc] init];
for (NSString* s in temp) {
NSNumber *n = [NSNumber numberWithInteger:[s integerValue]];
[numbers addObject:n];
}
return numbers;
}
Is there any neat and quick way to do such conversion?
Thanks
First remove the unwanted characters from the string, like white spaces and braces:
NSString* str = #"[90, 5, 6]";
NSCharacterSet* characterSet = [[NSCharacterSet
characterSetWithCharactersInString:#"0123456789,"] invertedSet];
NSString* newString = [[str componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:#""];
You will have a string like this: 90,5,6. Then simply split using the comma and convert to NSNumber:
NSArray* arrayOfStrings = [newString componentsSeparatedByString:#","];
NSMutableArray* arrayOfNumbers = [NSMutableArray arrayWithCapacity:arrayOfStrings.count];
for (NSString* string in arrayOfStrings) {
[arrayOfNumbers addObject:[NSDecimalNumber decimalNumberWithString:string]];
}
Using the NSString category from this response it can be simplified to:
NSArray* arrayOfStrings = [newString componentsSeparatedByString:#","];
NSArray* arrayOfNumbers = [arrayOfStrings valueForKey: #"decimalNumberValue"];
NSString* str = #"[90, 5, 6]";
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:#"[] "];
NSArray *array = [[[str componentsSeparatedByCharactersInSet:characterSet]
componentsJoinedByString:#""]
componentsSeparatedByString:#","];
Try like this
NSArray *arr = [string componentsSeparatedByString:#","];
NSString *newSTR = [str stringByReplacingOccurrencesOfString:#"[" withString:#""];
newSTR = [newSTR stringByReplacingOccurrencesOfString:#"]" withString:#""];
NSArray *items = [newSTR componentsSeparatedByString:#","];
You can achieve that using regular expression 
([0-9]+)
NSError* error = nil;
NSString* str = #"[90, 5, 6]";
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:#"([0-9]+)" options:0 error:&error];
NSArray* matches = [regex matchesInString:str options:0 range:NSMakeRange(0, [str length])];
Then you have a NSArray of string, you just need to iterate it and convert the strings to number and insert them into an array.

How to display an NSArray in a UITextView

I have a array that's I want to have in a uitextview. This is how it's currently displaying. How do I remove the ( ( ) )'s?
Here's a screenshot
Here's my code:
NSArray *arrayConditions = [[NSArray alloc] initWithObjects:[item objectForKey:#"conditions"], nil];
NSString * resultConditions = [arrayConditions description];
self.conditionsDeal.text = resultConditions;
NSLog(#"conditions are %#", [item objectForKey:#"conditions"]);
thanks for any help
NSArray *arrayConditions = [[NSArray alloc] initWithObjects:[item objectForKey:#"conditions"], nil];
// change this line only for the concatenation
NSString * resultConditions = [arrayConditions componentsJoinedByString:#"\n"];
self.conditionsDeal.text = resultConditions;
// this line always provides you the result of the '-description' method of the 'NSArray'
NSLog(#"conditions are %#", [item objectForKey:#"conditions"]);
NSMutableString *textViewText = [NSMutableString string];
for (NSString *str in arrayConditions)
[textViewText appendFormat:#"%#\n", str];
self.conditionsDeal.text = textViewText;
Convert that array into NSMutableString.
Separate that string by "," or space and write it UITextView.

NSString substring processing

I have a string with the following value:
[{ProjLead=irshad, ProjName=irshad proj, ProjectId=365}]
I would like to get the value for ProjLead, ProjName and ProjectId separately. How can I do it?
You can combine some methods from NSString class.
Something like this:
NSString *string = #"[{ProjLead=irshad, ProjName=irshad proj, ProjectId=365}]";
NSString *parametersString = [string substringWithRange:NSMakeRange(2, string.length - 4)];
NSArray *parameters = [parametersString componentsSeparatedByString: #", "];
NSMutableArray *values = [NSMutableArray array];
for (NSString *parameter in parameters) {
[values addObject:[[parameter componentsSeparatedByString:#"="] objectAtIndex:1]];
}
values array is what you want.

I have a NSString like this: "Firstname Lastname". How do I convert it to "Firstname L."?

I would like to change it to first name and last initial.
Thanks!
NSString* nameStr = #"Firstname Lastname";
NSArray* firstLastStrings = [nameStr componentsSeparatedByString:#" "];
NSString* firstName = [firstLastStrings objectAtIndex:0];
NSString* lastName = [firstLastStrings objectAtIndex:1];
char lastInitialChar = [lastName characterAtIndex:0];
NSString* newNameStr = [NSString stringWithFormat:#"%# %c.", firstName, lastInitialChar];
This could be much more concise, but I wanted clarity for the OP :) Hence all the interim variables and var names.
This would do it:
NSArray *components = [fullname componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *firstnameAndLastnameInitial = [NSString stringWithFormat:#"%# %#.", [components objectAtIndex:0], [[components objectAtIndex:1] substringToIndex:1]];
This assumes that fullname is an instance of NSString and contains two components separated by whitespace, so you will need to check for that as well.
You can use this code snippet, first separate string using componentsSeparatedByString, then join them again but only get the first character of Lastname
NSString *str = #"Firstname Lastname";
NSArray *arr = [str componentsSeparatedByString:#" "];
NSString *newString = [NSString stringWithFormat:#"%# %#.", [arr objectAtIndex:0], [[arr objectAtIndex:1] substringToIndex:1]];
Get an array of the parts of the name individually:
NSString *sourceName = ...whatever...;
NSArray *nameComponents =
[sourceName
componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
Then, I guess:
NSString *compactName =
[NSString stringWithFormat:#"%# %#.",
[nameComponents objectAtIndex:0],
[[nameComponents lastObject] substringToIndex:1]];
That'll skip any middle names, though if there's only one name, like say 'Jeffry' then it'll output 'Jeffry J.'. If you pass in the empty string then it'll raise an exception when you attempt to get objectAtIndex:0 since that array will be empty. So you should check [nameComponents count].

Resources