How to convert a String into a &'static str
Updated for Rust 1.0
You cannot obtain &'static str
from a String
because String
s may not live for the entire life of your program, and that's what &'static
lifetime means. You can only get a slice parameterized by String
own lifetime from it.
To go from a String
to a slice &'a str
you can use slicing syntax:
let s: String = "abcdefg".to_owned();
let s_slice: &str = &s[..]; // take a full slice of the string
Alternatively, you can use the fact that String
implements Deref<Target=str>
and perform an explicit reborrowing:
let s_slice: &str = &*s; // s : String
// *s : str (via Deref<Target=str>)
// &*s: &str
There is even another way which allows for even more concise syntax but it can only be used if the compiler is able to determine the desired target type (e.g. in function arguments or explicitly typed variable bindings). It is called deref coercion and it allows using just &
operator, and the compiler will automatically insert an appropriate amount of *
s based on the context:
let s_slice: &str = &s; // okay
fn take_name(name: &str) { ... }
take_name(&s); // okay as well
let not_correct = &s; // this will give &String, not &str,
// because the compiler does not know
// that you want a &str
Note that this pattern is not unique for String
/&str
- you can use it with every pair of types which are connected through Deref
, for example, with CString
/CStr
and OsString
/OsStr
from std::ffi
module or PathBuf
/Path
from std::path
module.
Get/Set non-static String to static string to pass through Java
First thing first; I understand that it is not possible to call a static method from a non-static method
First things first, you can call a static method from a non-static method. You can't call a non-static method from a static method.
You can set a static variable from a non-static method if that is your intention:
static String foo;
void myNonStaticMethod() {
foo = "bar";
}
Java, how to convert a string to class and call its static method?
To create new instance you need to do the following
Class c = Class.forName("Item");
Item i = (Item)c.newInstance();
If you want to invoke static method you just call it on class instead of instance
Item.test();
Or you can use reflection without directly reference to class
Class c = Class.forName("Item");
Method method = c.getMethod("test");
method.invoke(null);
How to replace a static string with a variable and convert to long?
Your text contains a trailing space. You should remove it by trim()
method. Try the following code & it should work for you.
String macAddr = last_mac.getText().trim();
Convert String to Reverse String Using Recursion in Java
This solution might be helpful. The comments explain the code pretty much.
public static String reverse_recursion(String str) {
String[] arry = str.split(" ", 2); //Split into a maximum of 2 Strings
if (arry.length > 1) { //If there is more than 1 word in arry
//Return the reverse of the rest of the str (arry[1])
//and concatenate together with the first word (arry[0])
return reverse_recursion(arry[1]) + " " + arry[0];
}
return arry[0]; //If less than or equal to 1 word, just return that word
}
How to change static string value by another class?
ConnectionStringExcel
is initialized using filename
but it won't track future changes.
You can convert ConnectionStringExcel
into a readonly property with this getter
public static string ConnectionStringExcel => @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filename + "; Extended Properties= 'Excel 12.0 Xml;HDR=YES;'";
This will cause the string to be constructed with every call to ConnectionStringExcel
EDIT
If you're using an old version of the .net framework you can use
public static string ConnectionStringExcel
{
get
{
return @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
filename + "; Extended Properties= 'Excel 12.0 Xml;HDR=YES;'";
}
}
Related Topics
Nssortdescriptor Sorting Using Nsdate in Swift
API Violation - Multiple Calls Made to -[Xctestexpectation Fulfill]
Swift Firestore Check If Documents Exists
Xcode Playgrounds Shared Directory Not Working
Swift 4.1 Deinitialize and Deallocate(Capacity:) Deprecated
Change the Font of a Datepicker
Tap Gesture Not Working as Expected When Added to Uiview in Collectionview Cell
Use of Undeclared Type Autoreleasingunsafepointer Xcode 6 Beta 6
Avcapturevideopreviewlayer Is Not Visible on The Screenshot
Uibutton Font Size Isn't Changing
Ambigious Reference to Member Request() Issues with Alamofire After Migration to Swift 3
Showing Notification Banner on MAC with Swift
How to Switch an Xcode Project to Use Swift Version 1.2 in the Xcode 7 Beta
Swift Ui: Center Text into Circle
Add a Xib File to a Swift Package
Delay a Repeating Animation in Swiftui with Between Full Autoreverse Repeat Cycles