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];
Related
I am new in objective and I'm facing my first problem, and I can not continue my first project.
it's quite simple, I have a NSString :
NSString *myString = #"<font face='Helvetica' size=25 color='#d79198'> Here is some text !</font>";
what I want to do is to get the value of the size "25" which is always 2 char long, so I can calculate my UILabel size.
i know how to detect if there is the substring I am looking for "size=" using :
if ([string rangeOfString:#"bla"].location == NSNotFound)
but I have not found or not understand how to extract the string #"size=XX" and then get the XX as a NSString from *myString
Thank for any help.
NSString *myString = #"<font face='Helvetica' size=25 color='#d79198'> Here is some text !</font>";
NSRange range = [myString rangeOfString:#"size="];
if (range.location != NSNotFound)
{
NSLog(#"Found \"size=\" at %d", range.location);
NSString *sizeString = [myString substringWithRange:NSMakeRange(range.location+5, 2)];
NSLog(#"sizeString: %#", sizeString);
}
This should do the trick. You could also at the end do this: int sizeFont = [sizeString intValue];
NSString *myString = #"<font face='Helvetica' size=25 color='#d79198'> Here is some text !</font>";
if ([myString rangeOfString:#"size"].location != NSNotFound)
{
myString = [myString substringFromIndex:[myString rangeOfString:#"size"].location];
myString = [myString substringToIndex:[myString rangeOfString:#" "].location]; // Now , myString ---> size=25 color='#d79198'> Here is some text !</font>
myString = [myString substringFromIndex:[myString length]-2];// Now, myString ---> size=25
NSLog(#"myString -- %#",myString); // Now, myString ---> 25
}
If you have string like stack:overflow then use it as follow :
NSString *Base=#"stack:overflow"
NSString *one = [[Base componentsSeparatedByString:#":"] objectAtIndex:0];
NSString *two = [[Base componentsSeparatedByString:#":"] objectAtIndex:1];
In this case one = stack and two=overflow
Part of an HTML page? Then use the tool that is designed for the task.
You could calculate the range of the number yourself or use a very simple regular expression to get the substring, something like
(?<=size\=)\d*
This means that you are searching for digits (\d*) that is preceded by "size=" ((?<=size\=))
Which using NSRegularExpression would be
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:#"(?<=size\\=)\\d*"
options:0
error:&error];
NSTextCheckingResult *match =
[regex firstMatchInString:myString
options:0
range:NSMakeRange(0, [myString length])];
NSString *sizeText = [myString substringWithRange:match.range];
Finally you should convert the text "25" into a number using
NSInteger size = [sizeText integerValue];
Use componentsSeparatedByString: method...
NSString *myString = #"<font face='Helvetica' size=25 color='#d79198'> Here is some text !</font>";
NSString *theSizeString = [[[[myString componentsSeparatedByString:#" "] objectAtIndex:2] componentsSeparatedByString:#"="] objectAtIndex:1];
NSLog(#"The sizestring:%#",theSizeString);
I think it will be helpful to you.
You can get the range of the string #"size=". The range has location and length. So what you need next is to call on the myString the substringWithRange: method. The parameter would be an NSRage starting from the location+length of #"size=" and length of 2.
I have a string like: "mocktail, wine, beer"
How can I convert this into: "mocktail", "wine", "beer"?
the following gives you the desired result:
NSString *_inputString = #"\"mocktail, wine, beer\"";
NSLog(#"input string : %#", _inputString);
NSLog(#"output string : %#", [_inputString stringByReplacingOccurrencesOfString:#", " withString:#"\", \""]);
the result is:
input string : "mocktail, wine, beer"
output string : "mocktail", "wine", "beer"
You need to use:
NSArray * components = [myString componentsSeparatedByString: #", "];
NSString *string = #"mocktail, wine, beer";
//remove whitespaces
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//get array of string
NSArray *array = [trimmedString componentsSeparatedByString:#","];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSString *trimmedString in array) {
NSString *newString = [NSMutableString stringWithFormat:#"'%#'", trimmedString];
[newArray addObject:newString];
}
//merge new strings
NSString *finalString = [NSString stringWithFormat:#"%#", [newArray objectAtIndex:0]];
for (NSInteger i = 1; i < [newArray count]; i++) {
finalString = [NSString stringWithFormat:#"%#, %#", finalString, [newArray objectAtIndex:i]];
}
Without knowing spesifically about iOS or objective-c, I assume you could use a split function.
In almost any higher level programming language there is such a function.
Try:
Objective-C split
This gets you an array of Strings. You can then practically do with those what you want to do, e.g. surrounding them with single quotes and appending them back together. :D
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 have an NSDictionary object that contains my data. I am passing in an array of key names and a display format for a string representation of my data.
[self displayMyDataWithTheseKeys:myKeyArray inThisFormat:myFormat];
where, for example,
myKeyArray = [NSArray arrayWithObjects: #"Key1", #"Key2", nil];
myFormat = [NSString stringWithString: #"%# to the %# degree"];
However, myFormat may change and the number of keys in the array may vary as well.
If the number of elements in the array was always 2, this would be trivial. However, how can I handle a variable number of elements?
There isn't really a built-in method for this, but it's relatively easy to parse format strings with NSScanner. Here's a simple example, it only handles %# format specifiers, but as all elements in an NSArray are objects and not primitive types anyway, it shouldn't matter:
NSArray *myKeyArray = [NSArray arrayWithObjects: #"Key1", #"Key2", nil];
NSString *myFormat = [NSString stringWithString: #"%# to the %# degree"];
NSMutableString *result = [NSMutableString string];
NSScanner *scanner = [NSScanner scannerWithString:myFormat];
[scanner setCharactersToBeSkipped:[NSCharacterSet illegalCharacterSet]];
int i = 0;
while (![scanner isAtEnd]) {
BOOL scanned = [scanner scanString:#"%#" intoString:NULL];
if (scanned) {
if (i < [myKeyArray count]) {
[result appendString:[myKeyArray objectAtIndex:i]];
i++;
} else {
//Handle error: Number of format specifiers doesn't
//match number of keys in array...
}
}
NSString *chunk = nil;
[scanner scanUpToString:#"%#" intoString:&chunk];
if (chunk) {
[result appendString:chunk];
}
}
Use: stringByAppendingString
Here's an example on how to use it:
NSString *someString = #"String";
someString = [someString stringByAppendingString:[NSString stringWithFormat:#"%#",variable1]];
someString = [someString stringByAppendingString:[NSString stringWithFormat:#"%#",variable2]];
someString = [someString stringByAppendingString:[NSString stringWithFormat:#"%#",variable3]];
...and so on
If you have an array of keys which you want to put in a string:
NSString *string = #"And the keys are:\n";
for(int i = 0; i < [array count]; i++)
{
NSString *thisKey = (NSString *)[array objectAtIndex:i];
string = [string stringByAppendingString:[NSString stringWithFormat:#"Key number %d is %#",i,thisKey]];
}
my code works great until know and, if I put a double digit number into the text field (like 12) nslog returns 2 single digit numbers (like 1 and 2). Now I need to put these 2 single digit numbers into 2 strings. can somebody help me. thanks in advance.
NSString *depositOverTotalRwy = [NSString stringWithFormat:#"%#", [deposit text]];
NSArray *components = [depositOverTotalRwy
componentsSeparatedByString:#"/"];
NSString *firstThird = [components objectAtIndex:0];
for(int i = 0; i < [firstThird length]; i++)
{
char extractedChar = [firstThird characterAtIndex:i];
NSLog(#"%c", extractedChar);
}
You should be able to use -stringWithFormat:.
NSString *s = [NSString stringWithFormat:#"%c", extractedChar];
EDIT:
You can store them in an array.
NSMutableArray *digits = [NSMutableArray array];
for ( int i = 0; i < [s length]; i++ ) {
char extractedChar = [s characterAtIndex:i];
[digits addObject:[NSString stringWithFormat:#"%c", extractedChar]];
}
Try to print the value of firstThird using NSLog(), see what it exactly hold, you code seem correct,
Use characterAtIndex function for NSString to extract a character at known location
- (unichar)characterAtIndex:(NSUInteger)index
Use as below
NSString *FirstDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:0]];
NSString *SecondDigit = [NSString stringWithFormat:#"%c", [myString characterAtIndex:1]];