What Objective-C NSString method need to check if a string is contained in another? [duplicate] - nsstring

This question already has answers here:
How do I check if a string contains another string in Objective-C?
(21 answers)
Closed 3 years ago.
What method of NSString checks if a string found by a localizedCaseInsensitiveCompare: keyword is contained in another?
NSString *listOfNames = #"anas, ward, qusai, zainab";
NSString *keyword = #"Ward";
if ([listOfNames localizedCaseInsensitiveCompare:keyword]) {
NSLog(#"\nMatch found!\n");
} else {
NSLog(#"\nNo match found!\n");
}

I assume that you mean you're using localizedCaseInsensitiveContainsString: since the method you've cited doesn't do what you're saying.
There's a corresponding method to get the range of the search term: -[NSString localizedStandardRangeOfString:] You use the returned range to index back into the source string.
You can also use the rangeOfString:options:range:locale: if you need to search with a locale other than the current one.

The localizedCaseInsensitiveCompare most likely internally converts both strings to lowercase and then does a compare using a localized collation algorithm.
From the docs:
"Localized string comparisons are based on the Unicode Collation Algorithm, as tailored for different languages by CLDR (Common Locale Data Repository). Both are projects of the Unicode Consortium. Unicode is a registered trademark of Unicode, Inc."
Probably a reasonable thing to do is convert each string to lowercase, using something like listOfNames.lowercaseString

Related

Qt - QString Numerical Format String (cformat), Options?

I have user-provided format string (e.g. "%.2f") and a QVariant type that I am attempting to combine to output into a (formatted) string.
I had gone down the path of using QString::asprintf(const char *cformat, ...) to achieve this, where I would supply the appropriate converted data type, like this:
QString result_str = QString::asprintf(disp_fmt.toUtf8(),variant_type.toUInt());
This works fine for the most part, especially when I have a floating point as the input. However, if my format string in this particular integer (.toUInt()) conversion case includes decimal formatting (e.g. "%.2f"), then I get a constant result of "0.00". This caught me by surprise as I expected to instead just get ".00" tacked onto the integer, as I have seen in other languages like Perl.
What am I missing here? Also, I know asprintf() was added fairly recently and the documentation already now advises to use QTextStream or arg() instead. I don't believe this to be an option, however, for me to use this style of format string. Thanks.
The format string is expecting a double, but you're providing an int. It works if you provide an actual double, like this:
QString result_str = QString::asprintf(disp_fmt.toUtf8(),variant_type.toDouble());
Also note, this behavior is identical to how the standard C library functions work (std::sprintf, etc).

JSONPath - Filter on keys that contain string [duplicate]

This question already has answers here:
Find a JSON property name that starts with something using JSON Path
(2 answers)
Closed 1 year ago.
Is there a JSONPath expression to filter on keys that match a given pattern ?
I would like to get all the values that contain name in the keys without explicitly listing those keys.
Input
{"foo":{"bar":{"name1":"john","name2":"jane"}}}
Output (expected)
["john","jane"]
Thanks !
The vanilla approach to get the value of two keys is to use a $['x', 'y'] union.
In your case this could look like this:
$.foo.bar['name1', 'name2']
Unfortunately, not every JSON path engine will return the result in this way (and there are some minor syntax differences as well, i.e some prefer double quotes or no quotes, etc). E.g. when you test your input with the path above online here: https://jsonpath.herokuapp.com/
Using the Jayway tab yields:
{
"name1" : "john",
"name2" : "jane"
}
while the Gatling tab gives the expected result:
[
"john",
"jane"
]
So, give it a try with your JSON path engine at hand. If the result is not as expected post-processing the full or intermediate JSON result in a host programing language might be easier.
If you're having trouble with JsonPath because of the lack of standard syntax and specification, you can use JMESPath instead. It has implementations that pass the TCK in many languages.
foo.bar.[name1, name2]

Unable to extract (US) Zipcode from doc file

I am need to get the Zipcode from the Resume.doc file..
but not succceded,,,
Its working with static string , I mean it validates the static string but unable to parse the zipcode from doc file,,
I am sharing my code ...
protected void zipcodeGetter()
{
var path = "C:\\Users\\Jatinder\\Desktop\\LUCENE\\Resume\\Jeffrey.doc";
Document doc = new Document();
string html = File.ReadAllText(path);
using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default))
{
html = sr.ReadToEnd();
}
const string MatchPhondePattern = #"^\d{5}(?:[-\s]\d{4})?$";
Regex rx = new Regex(MatchPhondePattern, RegexOptions.Compiled | RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = rx.Matches(html);
// Report the number of matches found.
int noOfMatches = matches.Count;
//Do something with the matches
foreach (Match match in matches)
{
//Do something with the matches
string tempPhoneNumber = match.Value.ToString(); ;
}
}
can anyone help me with this
Your code just won't work with that regular expression.
This problem is complicated and your best option is to use a service from a company that does this. They will have a robust system.
Here is a quote from an article on regex and addresses:
We get a lot of questions from programmers about parsing addresses. We see a lot of people trying to use regular expressions for street addresses, and as the address user experience experts, we cringe whenever another programmer falls prey to this trap. We hope that this information will save you some trouble, and if your searching is in vain, please feel free to ask us any questions you have about addresses. ...
Should you use regular expressions to parse street addresses? The short answer is, "Probably not." Because of the wide variance in address content and formatting, addresses aren't "regular"—an indispensable factor in using regular expressions to process information.
Now, some notes and hints about your regular expressions.
I used RegExr to make an example of the regular expression you used. As you can see, there are no highlighted regions, meaning your regular expression won't work.
If you just want to match five consecutive digits, the regular expression is: [0-9]{5}. Here is an example.
You can't just use ^ and $ because, for example, there might be a space or a period before or after the zip code and ^ and $ in your code would mean you're looking for beginnings and ends of lines.
The problem with not having any other qualifiers, however, is you will match long numbers, too. In other words, with a string like 1234567890, you will match [0-9]{5} because there are five consecutive digits in that string.
It's hard to qualify the regular expression with possible punctuation or spaces before or after the match, because what if the match is at the beginning or end of the line? It will miss some.
Here is a regex that might be useful to you. It seems to work in a lot of cases. You can see the example here, with more explanation.
(?<=\W|^)\d{5}(-?\d{4})?(?=\W|$)
(Full disclosure: I work for SmartyStreets and we have an API that does this. Check out the API docs if you're interested.)

Tokenizing a string in xquery [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Tokenizing in Xquery
Let string a = "http://ontheland.com/letus/tag/present?page=20&type=0", need to tokenize this string where i want to get "20" as integer. The main issue with this is how to tokenize "&". I have been trying with "&", but no use. Can you please assist the exact problem with this.
You need to use & as the entity for &:
let $a := <a><![CDATA[http://ontheland.com/letus/tag/present?page=20&type=0]]></a>
return xs:int(tokenize(tokenize($a,'&')[1],'=')[last()])
You can try this example live at http://www.zorba-xquery.com/html/demo#H8dHNTZwa2AnbIWhKEfMQ9HKT84=
Unless that string is CDATA, your XML is not going to be well-formed and your xquery probably won't work. If it is CDATA, you should be able to tokenize by just using &.
Example:
XML
<a><![CDATA[http://ontheland.com/letus/tag/present?page=20&type=0]]></a>
XPath (to be used in XQuery)
xs:int(tokenize(tokenize(/a,'&')[1],'=')[last()])
Result
20
Tested using Saxon HE

Multiple Base64 encoded parameters that appear as 1 in a URL query string

I need to pass 2 parameters in a query string but would like them to appear as a single parameter to the user. At a low level, how can I concatinate these two values and then later separate them? Both values are Base64 encoded.
?Name=abcyxz
where both abc and xyz are separate Base64 encoded strings.
why don't you just do something like this
temp = base64_encode("var1=abc&var2=yxz")
and then call
?Name=temp
Later you can decode the whole string and split the vars.
(sry for pseudo code :P)
Edit: a small quote from wikipedia
The current version of PEM (specified in RFC 1421) uses a 64-character alphabet consisting of upper- and lower-case Roman alphabet characters (A–Z, a–z), the numerals (0–9), and the "+" and "/" symbols. The "=" symbol is also used as a special suffix code. The original specification, RFC 989, additionally used the "*" symbol to delimit encoded but unencrypted data within the output stream.
You should either use some separator or store the length of the first item.
First of all, I would be curious as to why you can't just pass two parameters. But with that as a given, just choose any character that's a valid character in a URL query string, but won't show up in your base64 encoding, such as ~

Resources