I have been trying to format a string in my text view but I cant work it out. Im very new to xcode.
Am i missing something in this file? I have been looking through stack and this is how you do it..but its not working.
- (NSString *)stripTags:(NSString *)str
{
NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
scanner.charactersToBeSkipped = NULL;
NSString *tempText = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:#"<" intoString:&tempText];
if (tempText != nil)
[html appendString:tempText];
[scanner scanUpToString:#">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation] + 1];
tempText = nil;
}
return html;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *str = newsArticle;
descTextView.text = [NSString stringWithString:str];
// Do any additional setup after loading the view from its nib.
}
This code is a modified version of what was posted as an answer to a similar question here https://stackoverflow.com/a/4886998/283412. This will take your HTML string and strip out the formatting.
-(void)myMethod
{
NSString* htmlStr = #"<some>html</string>";
NSString* strWithoutFormatting = [self stringByStrippingHTML:htmlStr];
}
-(NSString *)stringByStrippingHTML:(NSString*)str
{
NSRange r;
while ((r = [str rangeOfString:#"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
{
str = [str stringByReplacingCharactersInRange:r withString:#""];
}
return str;
}
You are trying to put HTML into a label. You want to use a UIWebView.
#try this one
-(NSString *) stringByStrippingHTML:(NSString *)HTMLString {
NSRange r;
while ((r = [HTMLString rangeOfString:#"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
HTMLString = [HTMLString stringByReplacingCharactersInRange:r withString:#""];
return HTMLString;
}
Related
In IOS,how to remove +-#*() ,which get from address book.
for example:
NSString *txt = #"+1(510)1234567"
txt = [NSString stringWithFormat:#"tel://%#", txt];
[[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:txt]];
it's a invalid number to call.
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlToCall]]; is useless for this type number.
could I have some better options except use
[txt stringByReplacingOccurrencesOfString:#"-" withString:#""]....
I just wanna make a call.
Thanks for your help.
In fact:we can make a call by openURL: the format of call is not "tel://",It is "tel:".
So,what I should do is that:
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:
[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"]
invertedSet]] componentsJoinedByString:#""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", cleanedString]];
That's OK.
————————————————————————————————
#"tel://“ isn't the right url to make a call!
we must use #"tel:"
if not some number ,as +1 (510) 3436 which
BOOL bCanCall = [[UIApplication sharedApplication]canOpenURL:urlToCall]; will return False. you can't make a call.
Try this, it may work
-(NSString *) formatIdentificationNumber:(NSString *)string
{
NSCharacterSet * invalidNumberSet = [NSCharacterSet characterSetWithCharactersInString:#"\n_!##$%^&*()[]{}'\".,<>:;|\\/?+=\t~` "];
NSString * result = #"";
NSScanner * scanner = [NSScanner scannerWithString:string];
NSString * scannerResult;
[scanner setCharactersToBeSkipped:nil];
while (![scanner isAtEnd])
{
if([scanner scanUpToCharactersFromSet:invalidNumberSet intoString:&scannerResult])
{
result = [result stringByAppendingString:scannerResult];
}
else
{
if(![scanner isAtEnd])
{
[scanner setScanLocation:[scanner scanLocation]+1];
}
}
}
return result;
}
or see the link
You can remove like this..
NSString *s = #"+1(510)1234567";
NSCharacterSet *unwantedStr = [NSCharacterSet characterSetWithCharactersInString:#"+()"];
s = [[s componentsSeparatedByCharactersInSet: unwantedStr] componentsJoinedByString: #""];
NSLog(#"%#", s);
Alternativ, some hints here, but you can optimize the code.
- (void)testExample
{
NSMutableCharacterSet *mvalidCharSet = [[NSMutableCharacterSet alloc] init];
[mvalidCharSet formUnionWithCharacterSet:[NSCharacterSet decimalDigitCharacterSet] ];
[mvalidCharSet addCharactersInString: #"+"];
NSCharacterSet *validCharSet = [mvalidCharSet copy]; // not mutable -> more efficient
NSCharacterSet *invalidCharSet = [validCharSet invertedSet];
NSString *txt = #"+1(510)1234567";
NSArray * components = [txt componentsSeparatedByCharactersInSet:invalidCharSet];
NSString * rejoin = [components componentsJoinedByString:#""];
NSLog(#"%#", rejoin);
}
This question already has answers here:
Convert first number in an NSString into an Integer?
(6 answers)
Objective-C: Find numbers in string
(7 answers)
Closed 10 years ago.
I have NSString with couple strings like this that the 465544664646 is change between them :
data-context-item-title="465544664646"
How i parse the 465544664646 string to a Array ?
Edit
NSRegularExpression* myRegex = [[NSRegularExpression alloc] initWithPattern:#"(?i)(data-context-item-title=\")(.+?)(\")" options:0 error:nil];
[myRegex enumerateMatchesInString:responseString options:0 range:NSMakeRange(0, [responseString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
NSRange range = [match rangeAtIndex:1];
NSString *string =[responseString substringWithRange:range];
NSLog(string);
}];
Try this one:
NSString *yourString=#"data-context-item-title=\"465544664646\" data-context-item-title=\"1212121212\"";
NSMutableArray *substrings = [NSMutableArray new];
NSScanner *scanner = [NSScanner scannerWithString:yourString];
[scanner scanUpToString:#"\"" intoString:nil];
while(![scanner isAtEnd]) {
NSString *tempString;
[scanner scanString:#"\"" intoString:nil];
if([scanner scanUpToString:#" " intoString:&tempString]) {
[substrings addObject:tempString];
}
[scanner scanUpToString:#"\"" intoString:nil];
}
//NSLog(#"->%#",substrings); //substrings contains all numbers as string.
for (NSString *str in substrings) {
NSLog(#"->%ld",[str integerValue]); //converted each number to integer value, if you want to store as NSNumber now you can store each of them in array
}
Something like this?
-(NSString *)stringFromOriginalString:(NSString *)origin betweenStartString: (NSString*)start andEndString:(NSString*)end {
NSRange startRange = [origin rangeOfString:start];
if (startRange.location != NSNotFound) {
NSRange targetRange;
targetRange.location = startRange.location + startRange.length;
targetRange.length = [origin length] - targetRange.location;
NSRange endRange = [origin rangeOfString:end options:0 range:targetRange];
if (endRange.location != NSNotFound) {
targetRange.length = endRange.location - targetRange.location;
return [origin substringWithRange:targetRange];
}
}
return nil;
}
You can use
- (NSArray *)componentsSeparatedByString:(NSString *)separator
method like
NSArray *components = [#"data-context-item-title="465544664646" componentsSeparatedByString:#"\""];
Now you should got the string at 2. index
[components objectAtIndex:1]
Now you can create array from that string using the method here
NSString to NSArray
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Remove HTML Tags from an NSString on the iPhone
I would like to know the best method for stripping out all HTML/Javascript etc tags out of an NSString.
The current solution I am using leaves comments and other tags in, what would be the best way to remove them?
I know OF solutions e.g. LibXML, but I would like some examples to work with.
Current solution:
- (NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:html];
while ([theScanner isAtEnd] == NO) {
// find start of tag
[theScanner scanUpToString:#"<" intoString:NULL] ;
// find end of tag
[theScanner scanUpToString:#">" intoString:&text] ;
// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
html = [html stringByReplacingOccurrencesOfString:
[ NSString stringWithFormat:#"%#>", text]
withString:#""];
}
// trim off whitespace
return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;
}
Try this method to remove HTML tags from a String:
- (NSString *)stripTags:(NSString *)str
{
NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
scanner.charactersToBeSkipped = NULL;
NSString *tempText = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:#"<" intoString:&tempText];
if (tempText != nil)
[html appendString:tempText];
[scanner scanUpToString:#">" intoString:NULL];
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation] + 1];
tempText = nil;
}
return html;
}
I'm fetching data from AllContacts; in that data I'm getting contact details such as (998) 989-8989. Using this number, I'm not able to make a call. Can any one help out with this? Thanks in advance.
HI All
At last i have used this following code to resolve this issue
NSString *originalString = #"(998) 989-8989";
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:originalString.length];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:#"0123456789"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
NSLog(#"%#", strippedString);
Thanks All
Sounds like you can just remove spaces, brackets and hypens.
NSString *phoneNumber = #"(998) 989-8989";
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
phoneNumber = [phoneNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
phoneNumber = [#"tel://" stringByAppendingString:phoneNumber];
[[UIApplication sharedApplication] openURL:phoneNumber];
In my app I want to remove numbers except characters a-z from string. How can I get only characters?
This is the short answer which doesnt need any lengthy coding
NSString *newString = [[tempstr componentsSeparatedByCharactersInSet:
[[NSCharacterSet letterCharacterSet] invertedSet]] componentsJoinedByString:#""];`
swift 3:
(tempstr.components(separatedBy:NSCharacterSet.letters.inverted)).joined(separator: "")
eg:
("abc123".components(separatedBy:NSCharacterSet.letters.inverted)).joined(separator: "")
NSString *stringToFilter = #"filter-me";
NSMutableString *targetString = [NSMutableString string];
//set of characters which are required in the string......
NSCharacterSet *okCharacterSet = [NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyz"];
for(int i = 0; i < [stringToFilter length]; i++)
{
unichar currentChar = [stringToFilter characterAtIndex:i];
if([okCharacterSet characterIsMember:currentChar])
{
[targetString appendFormat:#"%C", currentChar];
}
}
NSLog(targetString);
[super viewDidLoad];
}
this was an answer given to me and works fine
I found an answer:
from remove-all-but-numbers-from-nsstring
NSString *originalString = #"(123) 123123 abc";
NSLog(#"%#", originalString);
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:originalString.length];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyz"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
NSLog(#"%#", strippedString);