2

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.

Lain
  • 2,166
  • 4
  • 23
  • 47
user2252092
  • 61
  • 1
  • 9
  • Just generally speaking, you could use a "regular expression", or you could use componentsSeparatedByString. With the latter you may need to examine each resulting "component" with hasPrefix to find the one that begins "size=". – Hot Licks Apr 06 '13 at 12:20

7 Answers7

2
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
    }
Manu
  • 4,730
  • 2
  • 20
  • 45
  • Just as if you were trying to [parse HTML using regular expressions](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). You **shall not.** –  Apr 06 '13 at 12:07
  • @H2CO3 hmm... regular expressions... * looking guilty * – David Rönnqvist Apr 06 '13 at 12:13
2
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];

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
Larme
  • 24,190
  • 6
  • 51
  • 81
  • Note that rangeOfString returns length==0 if the target string is not found. So checking for length is better than location. If the receiver is nil, the length and location are both returned as 0, not NSNotFound. – Reefwing Dec 12 '17 at 06:35
1

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

Gaurav
  • 8,227
  • 4
  • 34
  • 55
0

Part of an HTML page? Then use the tool that is designed for the task.

0

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];
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
-1

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.

Prasad G
  • 6,702
  • 7
  • 42
  • 65
-1

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.

Levi
  • 7,313
  • 2
  • 32
  • 44