my string is k= /Users/applefan/Library/Application Support/iPhone Simulator/3.1.3/Applications/422B3239-F521-4985-89FE-EC778C57C0AB/Documents/1.sql
now how to get 1 from 1.sql
i did somethins like this
NSScanner *scanner = [NSScanner scannerWithString:storePath];
[scanner scanUpToString:#".sql" intoString:&k] ;
NSLog(#"test is %#",k);
i did this also
unsigned int intValue;
while([scanner isAtEnd] == NO) {
[scanner scanHexInt:&intValue];
NSLog(#"HEX : %d", intValue);
**}
it gives me all the int value**
but i only want the numeric value after /Documents/
NSString *k = #"/Users/applefan/Library/Application Support/iPhone Simulator/3.1.3/Applications/422B3239-F521-4985-89FE-EC778C57C0AB/Documents/1.sql";
NSString *one = [[[[k componentsSeparatedByString:#"Documents/"] objectAtIndex:1]
componentsSeparatedByString:#".sql"] objectAtIndex:0];
NSLog(#"Is it one? %#", one);
Related
I need to get substring CODE's value (X2.31) from string
NSString *str = #"SHMU=\"\" CODE=\"X2.31\" XTN=\";
How could I get that particular substring?
Try the below one
NSString *str = #"SHMU=\"\" CODE=\"X2.31\" XTN=\"";
NSRange range = [str rangeOfString:#"CODE="];
NSString *substring = [[str substringFromIndex:NSMaxRange(range)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSArray *str1 = [substring componentsSeparatedByString:#" "];
NSLog(#"the sub %#",[[str1 objectAtIndex:0] stringByTrimmingCharactersInSet[NSCharacterSet whitespaceCharacterSet]]);
And by string was "X2.31"
NSString *strOrg = [NSString stringWithFormat:#"%#",#"SHMU=\"\" CODE=\"X2.31\" XTN=\""] ;
NSString *strLeft = [NSString stringWithFormat:#"%#",#"SHMU=\"\" CODE=\""] ;
NSString *strRight = [NSString stringWithFormat:#"%#",#"\" XTN=\""] ;
NSLog(#"%#", [self getDataBetweenString:strOrg LeftString:strLeft RightString:strRight LeftOffset:13]);
- (NSString *)getDataBetweenString:(NSString *)orgString LeftString:(NSString *)leftString RightString:(NSString *)rightString LeftOffset:(NSInteger)leftPos;
{
NSInteger left, right;
NSString *foundData;
NSScanner *scanner=[NSScanner scannerWithString:orgString];
[scanner scanUpToString:leftString intoString: nil];
left = [scanner scanLocation];
[scanner setScanLocation:left + leftPos];
[scanner scanUpToString:rightString intoString: nil];
right = [scanner scanLocation] + 1;
left += leftPos;
foundData = [orgString substringWithRange: NSMakeRange(left, (right - left) - 1)]; return foundData;
}
This is only specific to the str you posted.
Your number must be followed by First X.
float f=[[str componentsSeparatedByString:#"X"][1] floatValue];
I have an .csv file with round about 10.000 Lines.
Now I have a textfield in which the User typing a value than he Click on a Button.
Now this value should be searching in the .csv and then in this Linie all values should be Displayed in a Label
E.g.
.csv:
PLZ, Name, Code 47158, Neuss, DE005116 46356, Moers, DE006151
ViewControler:
Textfield search: 47158
Label1: Neuss
Label2: DE005116
Thanks for helping
1 google search would have made you happy ;)
http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data
EDIT To answer your question below.
while ([scanner scanFloat:&myVariable] && [scanner scanFloat:&myVariable2] && [scanner scanFloat:&myVariable3]) {
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:myVariable], #"theKeyInTheCSV",
[NSNumber numberWithFloat:myVariable2], #"theKeyInTheCSV2",
[NSNumber numberWithFloat:myVariable3], #"theKeyInTheCSV3",
nil]];
}
her is my code:
-(IBAction)Zollamtsname:(id)sender{
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Zollämter Access" ofType:#"csv"];
NSString *myText = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil ];
NSLog(#"%#", [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]);
if ( nil == mytext ) return NO;
NSScanner *scanner = [NSScanner scannerWithString:myText];
[scanner setCharactersToBeSkipped:
[NSCharacterSet characterSetWithCharactersInString:#"\n, "]];
NSMutableArray *newPoints = [NSMutableArray array];
float test1, test2;
while ( [scanner scanFloat:&test1] && [scanner scanFloat:&test2] ) {
[newPoints addObject:
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:test1], #"test1",
[NSNumber numberWithFloat:test2], #"test2",
nil]];
}
[self setPoints:newPoints];
return YES;
}
//[self.Zollamzsnummer setText:#"test"];
}
i get 4 errors and I don't know how so solve them :( 1."return NO" = the void method should not return a value
2. "return YES" = the void method should not return a value
3. [self setPoints:newPoints]; = Not visible #interface for'ViewController_suche'declares the selector setPoints
ANd who I say that the serach value is in the textfield?
My Question is regarding special Character in NSString.
Actual name:- Yusuf Doğan
Retrieve name= Yusuf Do\u011fan
My Actual fb friend name is Yusuf Doğan (Check Special "g with ~ cap").
I take it as NSdata and then converter it in to nsstring.
NSString *stringResponse = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
But the nsstring shows it as
"Yusuf Do\u011fan"
Similar cause with the following.
Ricsi Zoványi as Ricsi Zov\u00e1nyi
Pero Perić as Pero Peri\u0107
Any Solution.
Thanks in advance.
You can try this:
NSString *source = [NSString stringWithString:_username];
[_username release];
NSMutableString *result = [[NSMutableString alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:source];
[scanner setCharactersToBeSkipped:nil];
while (![scanner isAtEnd]) {
NSString *chunk;
// Scan up to the Unicode marker
[scanner scanUpToString:#"\\u" intoString:&chunk];
// Append the chunk read
[result appendString:chunk];
// Skip the Unicode marker
if ([scanner scanString:#"\\u" intoString:nil]) {
// Read the Unicode value (assume they are hexa and four)
unsigned int value;
NSRange range = NSMakeRange([scanner scanLocation], 4);
NSString *code = [source substringWithRange:range];
[[NSScanner scannerWithString:code] scanHexInt:&value];
unichar c = (unichar) value;
// Append the character
[result appendFormat:#"%C", c];
// Move the scanner past the Unicode value
[scanner scanString:code intoString:nil];
}
}
_username = [[NSString stringWithFormat:#"%#",result] retain];
[result release];
}
Instead of "NSUTF8StringEncoding" use "NSUTF16StringEncoding". I think this may solve your issue.
I'm using this method to find the first <> couple into a string (XML content) :
NSScanner* scanner = [NSScanner scannerWithString:contentToParse];
int startPos = 0;
int endPos = 0;
// Open search
if ([scanner scanString:#"<" intoString:nil]) {
startPos = [scanner scanLocation]-1;
NSLog(#"found '<' at pos %i", startPos);
// close search
if ([scanner scanString:#">" intoString:nil]) {
endPos = [scanner scanLocation]-1;
NSLog(#"found '>' at pos %i", endPos);
NSString* tag = [contentToParse substringWithRange:NSMakeRange(startPos, endPos-startPos)];
NSLog(#"Tag found : %#", tag);
}
}
but only "found '<' at pos 0" is logged.
My XML content contains many many <> items...
Why is that method not working ?
scanString:intoString: tries to scan the string parameter at the current location. If such string is not present at the current location, it simply returns NO.
You may want use scanUpToString:intoString: (reference) instead, which scans advancing the scan location until the given string is encountered.
NSScanner *scanner = [NSScanner scannerWithString:contentToParse];
// open search
[scanner scanUpToString:#"<" intoString:nil];
if (![scanner isAtEnd]) {
[scanner scanString:#"<" intoString:nil];
// close search
NSString *tag = nil;
[scanner scanUpToString:#">" intoString:&tag];
if (![scanner isAtEnd]) {
NSLog(#"Tag found : %#", tag);
}
}
I have a large text file (about 10 MB). In the text file there are values like (without the empty lines between the rows, I couldn't format it here properly):
;string1;stringValue1;
;string2;stringValue2;
;string3;stringValue3;
;string4;stringValue4;
I'm parsing all the 'stringX' values to an Array and the 'stringValueX' to another string, using a pretty ugly solution:
words = [rawText componentsSeparatedByString:#";"];
NSEnumerator *word = [words objectEnumerator];
while(tmpWord = [word nextObject]) {
if ([tmpWord isEqualToString: #""] || [tmpWord isEqualToString: #"\r\n"] || [tmpWord isEqualToString: #"\n"]) {
// NSLog(#"%#*** NOTHING *** ",tmpWord);
}else { // here I add tmpWord the arrays...
I've tried to do this using NSScanner by following this example: http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data
But I received memory warnings and then it all crashed.
Shall I do this using NSScanner and if so, can anyone give me an example of how to do that?
Thanks!
In most cases NSScanner is better suited than componentsSeparatedByString:, especially if you are trying to preserve memory.
Your file could be parsed by a loop like this:
while (![scanner isAtEnd]) {
NSString *firstPart = #"";
NSString *secondPart = #"";
[scanner scanString: #";" intoString: NULL];
[scanner scanUpToString: #";" intoString: &firstPart];
[scanner scanString: #";" intoString: NULL];
[scanner scanUpToString: #";" intoString: &secondPart];
[scanner scanString: #";" intoString: NULL];
// TODO: add firstPart and secondPart to your arrays
}
You probably need to add error-checking code to this in case you get an invalid file.
You should use fast enumeration. It's far better than the one using objectEnumerator. Try this
for (NSString *word in words) {
// do the thing you need
}