Regex get string from string - ios

I have NSString that contain this string :
c&&(b.signature=Rk(c));return ql(a,b)}
The RK can be any two chars.
I try to get the RK from the string with (RegexKitLit):
NSString *functionCode = [dataStr2 stringByMatching:#".signature=(.*?)\(" capture:1L];
and functionCode is always nil.Any idea what wrong?

Don't bother with regular expressions for this. If the format of the string is always the same then you can simply do:
NSString *dataStr2 = #"c&&(b.signature=Rk(c));return ql(a,b)}";
NSString *functionCode = [dataStr2 substringWithRange:NSMakeRange(16, 2)];
If the string is not quite so fixed then base it on the position of the =.
NSString *dataStr2 = #"c&&(b.signature=Rk(c));return ql(a,b)}";
NSRange equalRange = [dataStr2 rangeOfString:#"="];
NSString *functionCode = [dataStr2 substringWithRange:NSMakeRange(equalRange.location + equalRange.length, 2)];

Related

Get one digit after decimal point iOS Objective C

I am trying to get one digit after decimal and store it as double.
For eg : -
float A = 146.908295;
NSString * string = [NSString stringWithFormat:#"%0.01f",A]; // op 146.9
double B = [string doubleValue]; // op 146.900000
i want output as 146.9 in double or float form..,before duplicating or downvoting make sure the answer to this output is given..
Thanks
Edited:-
NSString * str = [NSString stringWithFormat:#"%0.01f",currentAngle];
tempCurrentAngle = [str doubleValue];;
tempCurrentAngle = tempCurrentAngle - 135.0;
if (tempCurrentAngle == 8.7) {
NSLog(#"DONE ");
}
here currentAngle is coming from continueTrackingWithTouch method, which will be in float..here it does not enter in if loop even when tempCurrentAngle value changes to 8.700000 .
You can compare string values instead of double like,
double currentAngle = 143.7; // I have taken static values for demo.
double tempCurrentAngle = 0.0;
NSString * str = [NSString stringWithFormat:#"%0.01f",currentAngle];
tempCurrentAngle = [str doubleValue];;
tempCurrentAngle = tempCurrentAngle - 135.0;
NSString *strToCompare = [NSString stringWithFormat:#"%0.01f",tempCurrentAngle];
if ([strToCompare isEqualToString:#"8.7"] ) {
NSLog(#"DONE ");
}
If you debug once line by line then you will get idea that why it was not entering in if caluse.
tempCurrentAngle get 143.69999999999999 when you convert str to double then you reduce 135.0 from it so it's value will be 8.6999999999999886 and then you compare it with 8.7 then it will definitely not being equal! but if you convert tempCurrentAngle string with one decimal point then it will be 8.7! so you should compare string values instead of double!

how to convert uitextfield integer into string

I am new to iOS. I need to convert textfield integer value into string. I created textfield name as value1 and string as str1.
str1 = [NSString stringWithFormat:#"%d", value1];
By default, UITextField returns a NSString value.
So you can get any value like this :
NSString *str1 = value1.text;
As simple as that, but if you want to convert it to Int then,
int strInt = str1.intValue;
NSInteger strInteger = str1.integerValue;
If want to converts to a Double value,
double strDouble = str1.doubleValue;
CGFloat strCGFloat = str1.doubleValue;
And yes, if you don't need to perform anything on str1 then you shouldn't need to create an instance, you can directly convert it like this.
int strInt = value1.text.intValue;
NSInteger strInteger = value1.text.intValue;

Get position of NSString in string - iOS

I am developing an iOS app and one of the things I need to do it to go over URLs and replace the first protocol section with my own custom protocol.
How can I delete the first few characters of a NSString before the "://"?
So for example I need convert the following:
http://website.com --> cstp://website.com
ftp://website.com --> oftp://website.com
https://website.com --> ctcps://website.com
The main problem I face, is that I can't just delete the first 'x' number of characters from the URL string. I have to detect how many characters there are till the "://" characters are reached.
So how can I count how many characters there are from that start of the string to the "://" characters?
Once I know this, I can then simply do the following to delete the characters:
int counter = ... number of characters ...
NSString *newAddress = [webURL substringFromIndex:counter];
Thanks for your time, Dan.
http://website.com is a URL, and http is the scheme part of the URL. Instead of string manipulation I would recommend to use the
NSURLComponents class which is made exactly for this purpose: inspect, create and modify URLs:
NSString *originalURL = #"http://website.com";
NSURLComponents *urlcomp = [[NSURLComponents alloc] initWithString:originalURL];
if ([urlcomp.scheme isEqualToString:#"http"]) {
urlcomp.scheme = #"cstp";
} else if ([urlcomp.scheme isEqualToString:#"ftp"]) {
urlcomp.scheme = #"otfp";
}
// ... handle remaining cases ...
NSString *modifiedURL = [urlcomp string];
NSLog(#"%#", modifiedURL); // cstp://website.com
If the number of cases grows then a dictionary mapping is easier to
manage:
NSDictionary *schemesMapping = #{
#"http" : #"cstp",
#"ftp" : #"otfp"
#"https" : #"ctcps" };
NSURLComponents *urlcomp = [[NSURLComponents alloc] initWithString:originalURL];
NSString *newScheme = schemesMapping[urlcomp.scheme];
if (newScheme != nil) {
urlcomp.scheme = newScheme;
}
NSString *modifiedURL = [urlcomp string];
You can use:
NSRange range = [urlString rangeOfString:#"://"];
range.location will give you the first index from where the "://" starts and you can use it as:
NSString *newAddress = [urlString substringFromIndex:range.location];
and append your prefix:
NSString *finalAddress = [NSString stringWithFormat:#"%#%#", prefixString, newAddress];

Replace <br> in an NSString with a new line

I have an NSString like this
NSString *string = #"textTextTextTextText<br>textTextTextText<br>TextTextText"
I want to set this NSString to be the text of my UICell with a new line on each tag found on the string. How could I do that?
I've tried this, without success:
cell.textLabel.text = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:#"<br>"];
How about:
string = [string stringByReplacingOccurrencesOfString: #"<br>" withString: #"\n"]
or, if you're using Swift Strings
var string = "textTextTextTextText<br>textTextTextText<br>TextTextText"
string = Array(string).reduce("") {$0 + ($1 == "<br>" ? "\n" : $1)}
NSString * result =
[string stringByReplacingOccurrencesOfString:#"<br>" withString:#"\n"];
SWIFT 5:
If you are using Swift string (you can transform your NSString to String):
var string = "line1<br>line2<br>line3"
string = string..replacingOccurrences(of: "<br>", with: "\n"))

How to capture last 4 characters from NSString

I am accepting an NSString of random size from a UITextField and passing it over to a method that I am creating that will capture only the last 4 characters entered in the string.
I have looked through NSString Class Reference library and the only real option I have found that looks like it will do what I want it to is
- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange
I have used this once before but with static parameters 'that do not change', But for this implementation I am wanting to use non static parameters that change depending on the size of the string coming in.
So far this is the method I have created which is being passed a NSString from an IBAction else where.
- (void)padString:(NSString *)funcString
{
NSString *myFormattedString = [NSString stringWithFormat:#"%04d",[funcString intValue]]; // if less than 4 then pad string
// NSLog(#"my formatedstring = %#", myFormattedString);
int stringLength = [myFormattedString length]; // captures length of string maybe I can use this on NSRange?
//NSRange MyOneRange = {0, 1}; //<<-------- should I use this? if so how?
}
Use the substringFromIndex method,
OBJ-C:
NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-4, 0)]; //in case string is less than 4 characters long.
SWIFT:
let trimmedString: String = (s as NSString).substringFromIndex(max(s.length-4,0))
Try This,
NSString *lastFourChar = [yourNewString substringFromIndex:[yourNewString length] - 4];
You can check this function in Swift 5:
func subString(from myString: NSString, length: Int) {
let myNSRange = NSRange(location: myString.length - length, length: length)
print(myString.substring(with: myNSRange))
}
subString(from: "Menaim solved the issue", length: 4) // Output: ssue

Resources