charsequence

Most efficient way to convert a single char to a CharSequence

好久不见. 提交于 2019-11-30 17:03:36
What's the most efficient way to pass a single char to a method expecting a CharSequence? This is what I've got: textView.setText(new String(new char[] {c} )); According to the answers given here , this is a sensible way of doing it where the input is a character array. I was wondering if there was a sneaky shortcut I could apply in the single-char case. textView.setText(String.valueOf(c)) Looking at the implementation of the Character.toString(char c) method reveals that they use almost the same code you use: public String toString() { char buf[] = {value}; return String.valueOf(buf); } For

Most efficient way to convert a single char to a CharSequence

烂漫一生 提交于 2019-11-29 16:09:08
问题 What's the most efficient way to pass a single char to a method expecting a CharSequence? This is what I've got: textView.setText(new String(new char[] {c} )); According to the answers given here, this is a sensible way of doing it where the input is a character array. I was wondering if there was a sneaky shortcut I could apply in the single-char case. 回答1: textView.setText(String.valueOf(c)) 回答2: Looking at the implementation of the Character.toString(char c) method reveals that they use

using a string resource in a Toast

淺唱寂寞╮ 提交于 2019-11-29 13:21:48
My code is: public static void ToastMemoryShort (Context context) { CharSequence text = getString(R.string.toast_memoryshort); //error here Toast.makeText(context, text, Toast.LENGTH_LONG).show(); return; } but I'm getting "Cannot make a static reference to the non-static method getString(int) from the type Context" in Eclipse. I'm trying to get ready for localising my app (getting all the hard coded strings into resources), so where I have: getString(R.string.toast_memoryshort) I previously had a hard coded string which was fine. I'm not sure what's going on here (Java noob). Can anyone

Choosing between CharSequence and String for an API [duplicate]

泄露秘密 提交于 2019-11-28 09:02:02
This question already has an answer here: When to use CharSequence in an API 5 answers A String is-a CharSequence . Many methods in the Java library accept CharSequence so they operate more generally . Some classe have a String method (for example, Writer.write(String) ) and also implement Appendable with an equivalent CharSequence method (for example, Writer.append(CharSequence) ). If I am writing a class that delegates to such a class, ands needs some text input, I can choose for that input to be a String or a CharSequence . Choosing the later makes the class more flexible, by giving the

Generic OR instead of AND <T extends Number | CharSequence>

风格不统一 提交于 2019-11-27 23:27:55
Is it possible to generically parameterize a method accepting EITHER ClassA OR InterfaceB ? Does Not Compile Due to | Pseudocode public <T extends Number | CharSequence> void orDoer(T someData){ // ... } i.e. instead of writing multiple method signatures, I would like this one method to accept either a Number or CharSequence as an argument Should Pass with a Number OR CharSequence argument orDoer(new Integer(6)); int somePrimitive = 4; orDoer(somePrimitive); orDoer("a string of chars"); If you really want to do that, you'll need to wrap youur accepted classes inside a custom class of your own.

How to convert CharSequence to String?

♀尐吖头ヾ 提交于 2019-11-27 11:01:07
问题 How can I convert a Java CharSequence to a String ? 回答1: By invoking its toString() method. Returns a string containing the characters in this sequence in the same order as this sequence. The length of the string will be the length of this sequence. 回答2: There is a subtle issue here that is a bit of a gotcha. The toString() method has a base implementation in Object . CharSequence is an interface; and although the toString() method appears as part of that interface, there is nothing at

Choosing between CharSequence and String for an API [duplicate]

别来无恙 提交于 2019-11-27 02:36:04
问题 This question already has an answer here: When to use CharSequence in an API 5 answers A String is-a CharSequence. Many methods in the Java library accept CharSequence so they operate more generally. Some classe have a String method (for example, Writer.write(String) ) and also implement Appendable with an equivalent CharSequence method (for example, Writer.append(CharSequence) ). If I am writing a class that delegates to such a class, ands needs some text input, I can choose for that input

How to convert a String to CharSequence?

流过昼夜 提交于 2019-11-26 23:47:48
How to convert String to CharSequence in Java? João Silva Since String IS-A CharSequence , you can pass a String wherever you need a CharSequence , or assign a String to a CharSequence : CharSequence cs = "string"; String s = cs.toString(); foo(s); // prints "string" public void foo(CharSequence cs) { System.out.println(cs); } If you want to convert a CharSequence to a String , just use the toString method that must be implemented by every concrete implementation of CharSequence . Hope it helps. Straight answer: String s = "Hello World!"; // String => CharSequence conversion: CharSequence cs =

Why String.replaceAll() in java requires 4 slashes “\\\\\\\\” in regex to actually replace “\\”?

爷,独闯天下 提交于 2019-11-26 19:46:49
I recently noticed that, String.replaceAll(regex,replacement) behaves very weirdly when it comes to the escape-character "\"(slash) For example consider there is a string with filepath - String text = "E:\\dummypath" and we want to replace the "\\" with "/" . text.replace("\\","/") gives the output "E:/dummypath" whereas text.replaceAll("\\","/") raises the exception java.util.regex.PatternSyntaxException . If we want to implement the same functionality with replaceAll() we need to write it as, text.replaceAll("\\\\","/") One notable difference is replaceAll() has its arguments as reg-ex

Exact difference between CharSequence and String in java [duplicate]

感情迁移 提交于 2019-11-26 19:22:23
This question already has an answer here: CharSequence VS String in Java? 9 answers Why StringBuilder when there is String? 10 answers Choosing between CharSequence and String for an API [duplicate] 1 answer I read this previous post . Can any one say what the exact difference between CharSequence and String is, other than the fact that String implements CharSequence and that String is a sequence of character? For example: CharSequence obj = "hello"; String str = "hello"; System.out.println("output is : " + obj + " " + str); What happens when "hello" is assigned to obj and again to str ?