Separate part of string from whole string [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective C: How to extract part of a String (e.g. start with '#')
I want to divide string in to few parts. My string is:
NSString * myString = #"Multiply top and bottom to get < mathtype x3y2z3 over x3yz2 >,now cancel down the powers of < i >x, y, z< /i >.If you prefer, you can cancel down before you start multiplying.";
I need three parts from this.
1. Multiply top and bottom to get
2. < mathtype x3y2z3 over x3yz2 >
3. ,now cancel down the powers of < i >x, y, z< /i >.If you prefer, you can cancel down before you start multiplying.
How can I do this?

If the above fails you could always convert it to an array of characters and loop through it.
- (const char *)cStringUsingEncoding:(NSStringEncoding)encoding
there are more details in the NSString Docs

Related

Swift 4: modifying numbers in String [duplicate]

This question already has answers here:
Leading zeros for Int in Swift
(12 answers)
Closed 5 years ago.
In my application i create multiple files and i would like them to have consecutive numbering as part of the name. For example log_0001, log_0002
I use string interpolation, so in code it looks like
fileName = "log_\(number)"
number = number + 1
However, if i keep number as just Int, i will have log_1 instead of log_0001
I've only figured that i could check if number has 1/2/3/4 digits and add '000/00/0' manually.
Are there any String modifications allowing to put the required number of '0' to make it 4 symbols?
"I've only figured that i could check if number has 1/2/3/4 digits and add '000/00/0' manually."
In this case try the following code: Save the digit length in a variable, for example digitLength.
if (digitLength == 1) { print("log_000")
} else if (digitLength == 2) { print("log_00")
} else if (digitLength == 3) { print("log_0")
}
else { print("log_") }
print(number)
and then the variable.

Deleting Specific Substrings in Strings [Swift] [duplicate]

This question already has answers here:
Any way to replace characters on Swift String?
(23 answers)
Closed 5 years ago.
I have a string var m = "I random don't like confusing random code." I want to delete all instances of the substring random within string m, returning string parsed with the deletions completed.
The end result would be: parsed = "I don't like confusing code."
How would I go about doing this in Swift 3.0+?
It is quite simple enough, there is one of many ways where you can replace the string "random" with empty string
let parsed = m.replacingOccurrences(of: "random", with: "")
Depend on how complex you want the replacement to be (remove/keep punctuation marks after random). If you want to remove random and optionally the space behind it:
var m = "I random don't like confusing random code."
m = m.replacingOccurrences(of: "random ?", with: "", options: [.caseInsensitive, .regularExpression])

How to quickly check if an Integer lies within a given range [duplicate]

This question already has answers here:
Can I use the range operator with if statement in Swift?
(6 answers)
Closed 6 years ago.
I have an integer x and I want to check if it lies between a given boundary / within a given range.
The straightforward approach would be
let contains = x > lowerBounds && x < higherBounds
Is there a more swifty approach to this?
You can create a range and check if it contains x:
let contains = (lowerBounds...upperBounds).contains(x)
e.g.:
let successful = (200..<300).contains(httpStatusCode)
Or you can use the pattern matching operator:
let contains = lowerBounds...uppperBounds ~= x

Is there a way to store the exponent of my vector values as a variable in itself? [duplicate]

This question already has answers here:
How to get Exponent of Scientific Notation in Matlab
(2 answers)
Closed 7 years ago.
testVector =
1.0e+10 *
3.5688 3.1110 5.2349
Is it possible to take out the exponent (not sure what it's called) of a vector and store it as a variable? E.g. in this case the variable would have value 1.0e+10
You can find the value of the exponent using log10:
testVector = [3.5688e+10 3.1110e+10 5.2349e+10];
lowExp = min(floor(log10(testVector)));
eVal = 10^lowExp;
Result:
eVal = 1.0000e+10
Then you'll need to divide your original vector by eVal:
newTestV = testVector/eVal
newTestV =
3.5688 3.1110 5.2349

Check if textfield text is almost equal to string

I have a UITextField called textfield. And I have this code to check if the text in the textfield is equal to "exampletext"
if ([textfield.text isEqualToString:#"exampletext"]) {
NSLog(#"Correct");
} else {
NSLog(#"Wrong");
}
But I also want to check if the text in the textfield is almost equal to "exampletext", if the text is almost the same as "exampletext". Like if the text was "eampletex" I want to NSLog(#"Close")
Are there any ways to check if the textfield text is like 50% equal to "exampletext"?
Or any ways to check if the textfield text has 50% the same characters as "exampletext"?
Or something else like that?
What you are looking for is an implementation of the levenshtein distance, levenshtein("hello", "hallo") => 1, levenshtein("hello", "ellos") => 2. You can check this library.
Once you have the distance between the two strings, you could get it as a percentage calculating: percentage = 100 * levenshtein(original,other) / length(original)
Here's my go at it. Create a custom character set from the string you want to match. Check each character in the texfield.text against that character set, and if the number of matches is close to the number of letters in the string, do something..
NSString *testString = #"wordToCompare";
NSString *textFromTextfield = textfield.text;
//create a custom character set from the word you want to compare to...
NSCharacterSet *characterSetForString = [NSCharacterSet characterSetWithCharactersInString:testString];
//keep track of how many matches...
int numberOfCharsThatMatchSet = 0;
for (int x = 0; x < [textFromTextField length]; x++) {
unichar charToCheck = [textFromTextField characterAtIndex:x];
if ([characterSetForString characterIsMember:charToCheck] == YES) {
numberOfCharsThatMatchSet++;
}
NSLog(#"%d", numberOfCharsThatMatchSet);
}
// if the number of matches is the same as the length of the word + or - 2...
if ((numberOfCharsThatMatchSet > [testString length] - 2 ) && (numberOfCharsThatMatchSet < [testString length] + 2 )) {
NSLog(#"close match...");
}
Not sure if this is 100% what you're looking for, but maybe it will help anyway...
I'm sure there might be some open source out there somewhere that would do this for you..however, one approach I can think of that will give you a bit of a lead...
Sort out the characters of both your strings into arrays. Determine which string you want to be the master string and grab the string length of it.
Now compare each character. Ex: Word 1: hello, Word 2: ello.
Each time a letter is found add one to a count. If by the end of your looping your count is 80% of the original length you grabbed from the master string or greater then you most likely have a partial match.
So for our example Word 1 will be our master string and its length is 5. "ello" contains 4/5 characters and therefore is matches 80% of the original string.
I don't think there is an easy way (with several lines of code) of solving this. There are several algorithms you might consider and pick the one which suits your needs most.
You should look at this question. Although it has been designed and answered for another language, you asked for a way or method so you have your solution there.