I have a set of NSString representing the names of the files in a directory. These names are structured this way:
XXXXXXXXX_YYYY_AAAA.ext
All the sections separated by "_" are of variable length and I would only have the first.
How can I separate the first part from the other?
Find the position of the '_' character, then get a substring 0 through that position. Note that substringToIndex: does not include the character at the index position.
NSRange r = [myString rangeOfString:#"_"];
NSString *res = [myString substringToIndex:r.location];
Take a look at the NSString method componentsSeparatedByString:. That will tokenize a string and return you an array. Something like this:
NSArray *array = [#"XXXXXXXXX_YYYY_AAAA.ext" componentsSeparatedByString:#"_"];
NSString *firstToken = [array objectAtIndex:0];
NSArray *array = [yourString componentsSeparatedByString:#"_"];
NSString *Xs = [array objectAtIndex:0];
Try componentsSeparatedByString: under the heading Dividing Strings.
NSString Docs
Related
how to store Height Value separately "6ft 7inch"
6ft in one string and 7inch in another string.
6 and 7 will be dynamically changes ft inch will be constant.
I assume there will always be a single space character separating the 2 height values that you want to extract. You can use -componentsSeparatedByString method to turn the string into array of strings separated by the space character
NSString *originalString = #"6ft 7inch";
NSArray *separatedStrings = [originalString componentsSeparatedByString:#" "];
NSString *feetString = separatedStrings[0];
NSString *inchString = separatedStrings[1];
If there could be cases where there are no spaces in between you'll have to find the index of the key character set and then create substrings using them. Let me know if you need to know how
You just need to split the string.
NSString *combineString=#"6ft 7inch";
NSArray *stringsArray=[combineString componentsSeparatedByString:#" "];
if ([stringsArray count ]> 0) {
//here is the ft string
NSString *ftString=[stringsArray objectAtIndex:0];
if ([stringsArray count ]> 1)
{
//here is the inch string
NSString *inchString=[stringsArray objectAtIndex:1];
}
else
{
NSLog(#"Inch value not found");
}
}
else
{
NSLog(#"Empty String");
}
There are many ways to accomplish that. However, in your case it is the easiest way, so separate the string at the space:
NSString *string = #"6ft 7inch";
NSArray *components = [string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet];
I have this code who chunks a string existing inside a NSString into a NSMutableArray:
NSString *string = #"one/two/tree";
NSMutableArray *parts = [[string componentsSeparatedByString:#"/"] mutableCopy];
NSLog(#"%#-%#-%#",parts[0],parts[1],parts[2]);
This command works perfectly but if the NSString is not obeying this pattern (not have the symbol '/' within the string), the app will crash.
How can I check if it is possible to break the NSString, preventing the app does not crash?
Just check parts.count if you don't have / in your string (or only one), you won't get three elements.
NSString *string = #"one/two/tree";
NSMutableArray *parts = [[string componentsSeparatedByString:#"/"] mutableCopy];
if(parts.count >= 3) {
NSLog(#"%#-%#-%#",parts[0],parts[1],parts[2]);
}
else {
NSLog(#"Not found");
}
From the docs:
If list has no separators—for example, "Karin"—the array contains the string itself, in this case { #"Karin" }.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:
You might be better off using the "opposite" function to put it back together...
NSString *string = #"one/two/three";
NSArray *parts = [string componentsSeparatedByString:#"/"];
NSString *newString = [parts componentsJoinedByString:#"-"];
// newString = #"one-two-three"
This will take the original string. Split it apart and then put it back together no matter how many parts there are.
I have a NSMutableString variable that i append from aString, like this:
NSString *aString = [NSString stringWithFormat:#"{\"MyCode\":%#,\"TotalAmount\":%#,\"PaymentType\":\"%#\",\"BankName\":\"%#\",\"BankAccountNo\":\"%#\",\"Amount\":\"%#\",\"FileName\":\"%#\"}",aCode,total,type,labelBank.txt,labelAcc.txt,aTrasfer,imageName];
[teststring appendString:asstring2];
[teststring appendString:#","];
in this code i sucess to append the string in order they append. But know i want to append a new string in the first position, just like in array object at index 0.Can NSMutableString do this?
Hope my question is clear..thanks
You said it, with an index. Same way as you do with an array.
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex
Your code:
[testString appendString:aString];
[testString insertString:#"," atIndex:0]; /* prepend the comma
Check out the Documentation
Is there any way to split up a NSString to an array with all the letters separated?
Something like:
NSString *str = #"hey dude";
//Array output : h,e,y,d,u,d,e
[stringToCopy getCString:c_buffer maxLength:c_buffer_length encoding:NSUTF8StringEncoding];
I have multiple NSStrings and i wish to merge them into one other, here is my code so far...
NSString *newURL = [_parameters objectForKey:#"url"];
NSString *emailBody = #"Hey!<br>I just snipped my long url with My Cool App for iPhone in just a few seconds!<p><b>"+newURL+#"</b></p>";
If you know the number of your existing strings, you can just concat them:
NSString* longString = [firstString stringByAppendingString:secondString];
or:
NSString* longString = [NSString stringWithFormat:#"A string: %#, a float: %1.2f", #"string", 31415.9265];
If you have an arbitrary number of strings, you could put them in an NSArray and join them with:
NSArray* chunks = ... get an array, say by splitting it;
NSString* string = [chunks componentsJoinedByString: #" :-) "];
(Taken from http://borkware.com/quickies/one?topic=NSString)
Another good resource for string handling in Cocoa is: "String Programming Guide"
You can try
NSString *emailBody = [ NSString stringWithFormat: #"Hey!<br>I just snipped my long url with Snippety Snip for iPhone in just a few seconds, why not check it out?<p><b>%#</b></p>", newURL ];
Given that you've got multiple strings I recommend using an Array:
NSArray *array = [NSArray arrayWithObjects:#"URL", #"person", "body"];
NSString *combined = [array componentsJoinedByString:#""];
Formatting string has better readability and less error-prone:
NSString *newURL = [_parameters objectForKey:#"url"];
NSString *emailBody = [NSString stringWithFormat:#"Hey!<br>I just snipped my long url with Snippety Snip for iPhone in just a few seconds, why not check it out?<p><b>%#</b></p>", newUrl, newUrl];
You can concatenate strings in Cocoa using:
[NSString stringByAppendingString:]
Or you could use the [NSString stringWithFormat] method which will allow you to specify a C-style format string with a variable argument list to populate the escape sequences.