I have two String^ objects and i'd like to do a case-insensitive comparision for equality. That is "blah" should be considered equal to "BLAH". I know that String::CompareOrdinal is available to me, but that is case-sensitive. Is there a simple case-insensitive solution?
Disclaimer :) - I know that String^ is not a full-featured string class and it's recommended not to use it for general string operations...but in this specific situation I would actually prefer to keep the objects as String^ type. So there is the challenge, can you achieve this with String^'s ? :)
Use _wcsicmp or _wcsicmp_l. For example, given two non-null Platform::Strings, r and s:
_wcsicmp(r->Data(), s->Data());
Related
how should I define a function that takes in a string and returns the string in upper and lowercases according to even and odd indexing?
def myfunc(string):
for some in string:
if string.index%2==0:
I have written this much but I do not know what should I type now.
please help.
Here you can find the string methods Specially look at upper() and lower() which will be your friend here.
I currently have a string as follows which I received through an API call:
\n\nIt\U2019s a great place to discover Berlin and a comfortable place
to come home to.
And I want to convert it into something like this which is more readable:
It's a great place to discover Berlin and a comfortable place to come
home to.
I've taken a look at this post, but that's manually writing down every conversion, and there may be more of these unicode scalar characters introduced.
What I understand is \u{2019} is unicode scalar, but the format for this is \U2019 and I'm quite confused. Are there any built in methods to do this conversion?
This answer suggests using the NSString method stringByFoldingWithOptions.
The Swift String class has a concept called a "view" which lets you operate on the string under different encodings. It's pretty neat, and there are some views that might help you.
If you're dealing with strings in Swift, read this excellent post by Mike Ash. He discusses the idea of what a string really is with great detail and has some helpful hints for Swift 2.
Assuming you are already splitting the string and can get the offending format separately:
func convertFormat(stringOrig: String) -> Character {
let subString = String(stringOrig.characters.split("U").map({$0})[1])
let scalarValue = Int(subString)
let scalar = UnicodeScalar(scalarValue!)
return Character(scalar)
}
This will convert the String "\U2019" to the Character represented by "\u{2019}".
Recently I am dealing with escaping/encoding issues. I have a bunch of APIs that receive and return Strings encoded/escaped differently. In order to clean up the mess I'd like to introduce new types XmlEscapedString, HtmlEscapedString, UrlEncodedString, etc. and use them instead of Strings.
The problem is that the compiler cannot check the encoding/escaping and I'll have runtime errors.
I can also provide "conversion" functions that escape/encode input as necessary. Does it make sense ?
The compiler can enforce that you pass the types through your encoding/decoding functions; this should be enough, provided you get things right at the boundaries (if you have a correctly encoded XmlEscapedString and convert it to a UrlEncodedString, the result is always going to be correctly encoded, no?). You could use constructors or conversion methods that check the escaping initially, though you might pay a performance penalty for doing so.
(Theoretically it might be possible to check a string's escaping at compile time using type-level programming, but this would be exceedingly difficult and only work on literals anyway, when it sounds like the problem is Strings coming in from other APIs).
My own compromise position would probably be to use tagged types (using Scalaz tags) and have the conversion from untagged String to tagged string perform the checking, i.e.:
import scalaz._, Scalaz._
sealed trait XmlEscaped
def xmlEscape(rawString: String): String ## XmlEscaped = {
//perform escaping, guaranteed to return a correctly-escaped String
Tag[String, XmlEscaped](escapedString)
}
def castToXmlEscaped(escapedStringFromJavaApi: String) = {
require(...) //confirm that string is properly escaped
Tag[String, XmlEscaped](escapedStringFromJavaApi)
}
def someMethodThatRequiresAnEscapedString(string: String ## XmlEscaped)
Then we use castToXmlEscaped for Strings that are already supposed to be XML-escaped, so we check there, but we only have to check once; the rest of the time we pass it around as a String ## XmlEscaped, and the compiler will enforce that we never pass a non-escaped string to a method that expects one.
Just a straight forward beginner question, I am coding Lua stuff for Garrys Mod, learning by reading wiki and other codings.
if (self.Owner:SteamID( ) == "STEAM_0:1:44037488" ) then
the above is the code I want to use, to check to see if the STEAM ID (which I believe is a string) is equal to my exact string.
Is this viable? Or is there another way I should do it?
This should work exactly as you expect it to. In lua '==' for string will return true if contents of the strings are equal.
As it was pointed out in the comments, lua strings are interned, which means that any two strings that have the same value are actually the same string.
One thing to consider while learning Lua (from www.lua.org/source/5.2/lstring.h.html):
/*
** as all string are internalized, string equality becomes
** pointer equality
*/
#define eqstr(a,b) ((a) == (b))
String comparison in Lua is cheap, string creation may be not.
According to http://wiki.garrysmod.com/page/Player/SteamID, SteamID() returns a string so you should be able to write
if self.Owner:SteamID() == "STEAM_0:1:44037488" then
...do stuff...
end
If you ever need to confirm the type of an object, use type and print, like in this case print('type is', type(self.Owner:SteamID())) should print 'type is string'.
In lua, as answered above, '==' checks for equality.
Not saying you did this, because you didnt, but a common mistake is thinking that '=' is equality. '=' is assignment, '==' is equality.
How does one convert a string to a lowercase or perform some kind of equivalency comparison ignoring case? There is an ignore case on the Ascii type but it seems convoluted and I don't see a way to convert str to Ascii.
std::ascii::AsciiExt.eq_ignore_ascii_case does what you want:
use std::ascii::AsciiExt;
fn main() {
assert!("foo".eq_ignore_ascii_case("FOO"));
}
(The search in the docs is quite good now; searches like "case" and "ascii" return good sets of results which contain this solution.)
From that same trait, std::ascii::StrAsciiExt.to_ascii_upper and std::ascii::StrAsciiExt.to_ascii_lower are also very handy.