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.
Related
I have to convert the PathBuf variable to a String to feed my function. My code is like this:
let cwd = env::current_dir().unwrap();
let my_str: String = cwd.as_os_str().to_str().unwrap().to_string();
println!("{:?}", my_str);
it works but is awful with the cwd.as_os_str….
Do you have a more convenient method or any suggestions on how to handle it?
As mcarton has already said it is not so simple as not all paths are UTF-8 encoded. But you can use:
p.into_os_string().into_string()
In order to have a fine control of it utilize ? to send error to upper level or simply ignore it by calling unwrap():
let my_str = cwd.into_os_string().into_string().unwrap();
A nice thing about into_string() is that the error wrap the original OsString value.
It is not easy on purpose: String are UTF-8 encoded, but PathBuf might not be (eg. on Windows). So the conversion might fail.
There are also to_str and to_string_lossy methods for convenience. The former returns an Option<&str> to indicate possible failure and the later will always succeed but will replace non-UTF-8 characters with U+FFFD REPLACEMENT CHARACTER (which is why it returns Cow<str>: if the path is already valid UTF-8, it will return a reference to the inner buffer but if some characters are to be replaced, it will allocate a new String for that; in both case you can then use into_owned if you really need a String).
One way to convert PathBuf to String would be:
your_path.as_path().display().to_string();
As #mcarton mentioned, to_string_lossy() should do the job.
let cwd = env::current_dir().unwrap();
let path: String =String::from(cwd.to_string_lossy());
rustc 1.56.1 (59eed8a2a 2021-11-01)
I am a (learning) Rust fan (years of c/c++ programmer) but man, if it makes simple thing so complicated, UTF-8 and COW.. makes people lost in the translation.
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}".
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.
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());
I have a string, and I want to check whether it begins with a certain character (# in this case). I'd like to know the most efficient way of doing this!
What I'm thinking of is if {[string compare -length 1 $string "#"]}, because it might use strcmp() which will probably be one of the fastest ways to achieve this.
What I think might also be possible is if {[string index $string 1] == "#"}, because it might do *string == '#' which will probably also be very fast.
What do you think?
The fastest method of checking whether a string starts with a specific character is string match:
if {[string match "#*" $string]} { ... }
The code to do the matching sees that there's a star after the first character and then the end of the string, and so stops examining the rest of $string. It also doesn't require any allocation of a result (other than a boolean one, which is a special case since string match is also bytecode-compiled).
Yes [string match "*" $string_name] this is what worked for me. This is really fast as compared to regexp or any other utility.