Reputation: 438
I've got a string that shows the stock amount using "-" as separators.
It's built up like this: localStock
-wareHouseStock
-supplierStock
Now I want to update the supplierStock at the end of the string, but as you can see in the code below it goes wrong when the original string returns more than a single-space value (such as 20).
Is there a way to remove all characters until the last "-" (or remove characters after the second "-")?
NSMutableString *string1 = [NSMutableString stringWithString: p1.colorStock];
NSLog(@"string1: %@",string1);
NSString *newString = [string1 substringToIndex:[string1 length]-2];
NSLog(@"newString: %@",newString);
NSString *colorStock = [NSString stringWithFormat:@"%@-%@",newString,p2.supplierStock];
NSLog(@"colorstock: %@",colorStock);
p1.colorStock = colorStock;
NSLog1
string1: 0-0-0
newString: 0-0
colorstock: 0-0-20
NSLog2
string1: 0-0-20
newString: 0-0-
colorstock: 0-0--20
EDIT: Got it working thanks to Srikar!
NSString *string1 = [NSString stringWithString: p1.colorStock];
NSLog(@"string1: %@",string1);
NSString *finalString = [string1 stringByReplacingOccurrencesOfString:[[string1 componentsSeparatedByString:@"-"] lastObject] withString:p2.supplierStock.stringValue];
NSLog(@"finalString: %@",finalString);
p1.colorStock = finalString;
Upvotes: 2
Views: 2460
Reputation: 8448
None of these examples show how to do this if you are unaware of how many of these separator occurrences you're going to have in the original string.
Here's what I believe the correct the correct code should be for dismantling the original string and rebuilding it until you reach the final separator, regardless of how many separators it contains.
NSString *seperator = @" ";
NSString *everythingBeforeLastSeperator;
NSArray *stringComponents = [originalString componentsSeparatedByString:seperator];
if (stringComponents.count!=0) {
everythingBeforeLastSeperator = [stringComponents objectAtIndex:0];
for (int a = 1 ; a < (stringComponents.count - 1) ; a++) {
everythingBeforeLastSeperator = [NSString stringWithFormat:@"%@%@%@", everythingBeforeLastSeperator, seperator, [stringComponents objectAtIndex:a]];
}
}
return everythingBeforeLastSeperator;
Upvotes: 0
Reputation: 73588
Why not use componentsSeparatedByString
followed by lastObject
?
NSString *supplierStock = [[string1 componentsSeparatedByString:@"-"] lastObject];
The above works if the "stock amount" is always in sets of 3's separated by a "-". Also since you always want supplierStock, lastObject
is perfect for your needs.
Of course after splitting string1
with -
you get a NSArray
instance and you can access the individual components using objectAtIndex:index
. So if you want localStock
you can get by
NSString *localStock = [[string1 componentsSeparatedByString:@"-"] objectAtIndex:0];
Upvotes: 10
Reputation: 14251
With a string that looks like
NSString *myString = @"Hello-World";
you can separate it with the componentsSeparatedByString:
method of the NSString object as
NSArray *myWords = [myString componentsSeparatedByString:@"-"];
The myWords
- array will then contain the two NSString
objects Hello
and World
.
To access the strings:
NSString *theHelloString = [myWords objectAtIndex:0];
NSString *theWorldString = [myWords objectAtIndex:1];
Hope it helps!
Upvotes: 1
Reputation: 122391
I would suggest splitting the string into the 3 parts using [NSString componentsSeparatedByString:@"-"]
and then building it back up again:
NSArray *components = [p1.colorStock componentsSeparatedByString:@"-"];
p1.colorStock = [NSString stringWithFormat:@"%@-%@-%@",
[components objectAtIndex:0],
[components objectAtIndex:1],
p2.supplierStock];
Upvotes: 3