Android - String - java

Probably a noob question but, after some search I could not find the answer.
I've receive this String like the following one: "Distribui\u00e7\u00e3o Alimentar", and I want to set it as text of a EditText. How can I "replace" the \u00e7 and \u00e3o to "ç" and "ã"?
Thanks in advance.

With the string replace function
String newString = stringName.replaceAll("\u007", "c');
String newString2 = stringName.replaceAll("\u00e7", "a");
and you could concatenate the two with the concat method

Related

How to split a string and get only segament after the question mark

how do you split a string and get the sentence only after the question mark. For example say you have the line : hello?myNameIs...
how could you only get what's after the question mark
many thanks
If all your use cases will be similar to the simple example you've stated, you could use a simple split.
The code:
String delim = "\\?";
String s = "hello?myNameIs";
String token[] = s.split(delim);
System.out.println(token[1]);
Gives the output:
myNameIs
Of course, you can tinker with it to solve your specific problems.
try this one
String sArray[] = src.split(Pattern.quote("?"));

The filter string - removing some chars

is there a function in Java which removed from a string unwanted chars given by me? If not, what the most effective way to do it. I would like realize it in JAVA
EDIT:
But, I want reach for example:
String toRescue="#8*"
String text = "ra#dada882da(*%"
and after call function:
string text2="#88*"
You can use a regular expression, for example:
String text = "ra#dada882da(*%";
String text2 = text.replaceAll("[^#8*]", "");
After executing the above snippet, text2 will contain the string "#88*".
The Java String has many methods which can help you, such as
String.replace(char old, char new);
String.split(regex);
String.substring(int beginIndex);
These and many others are described in the javadoc : http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Can a string be converted to a string containing escape sequences in Java?

Say I enter a string:-
Hello
Java!
I want the output as:-
Hello\nJava!
Is there any way in which I could get the output in this format?
I am stuck on this one and not able to think about any logic which could do this for me.
It looks like http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html does what you want.
EG
String escaped = StringEscapeUtils.escapeJava(String str)
Will return "Hello\nJava!" when supplied with your string.
Hm..You can replace the new line character(\n) with \\n. For example:
String helloWithNewLine = "Hello\nJava";
String helloWithoutNewLine = helloWithNewLine.replace("\n", "\\n");
System.out.println(helloWithoutNewLine);
Output:
Hello\nJava

Split a String In Java against the split rule?

I have a string like this:
String str="\"myValue\".\"Folder\".\"FolderCentury\"";
Is it possible to split the above string by . but instead of getting three resulting strings only two like:
columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";
Or do I have to use an other java method to get it done?
Try this.
String s = "myValue.Folder.FolderCentury";
String[] a = s.split(java.util.regex.Pattern.quote("."));
Hi programmer/Yannish,
First of all the split(".") will not work and this will not return any result. I think java String split method not work for . delimiter, so please try java.util.regex.Pattern.quote(".") instead of split(".")
As I posted on the original Post (here), the next code:
String input = "myValue.Folder.FolderCentury";
String regex = "(?!(.+\\.))\\.";
String[] result=input.split(regex);
System.out.println("result: "+Arrays.toString(result));
Produces the required output (an array with two values):
result: [myValue.Folder, FolderCentury]
If the problem you're trying to solve is really that specific, you could do it even without using regular expression matches at all:
int lastDot = str.lastIndexOf(".");
columnArray[0] = str.substring(0, lastDot);
columnArray[1] = str.substring(lastDot + 1);

Java TextArea and String (print only 1 word of String)

I got problem with my TextArea
String A contain text a,b,c,d
I converted String to textarea using method TextArea.setText(A);
My problem is that textarea print out abcd instead of it I want it printed in lines example
A
B
C
D
I did read book and tried google but I can't find solution to my problem ;(
Sounds like you need to follow the javadoc that JB Nizet linked to above, and take advantage of the String.replace() method. It takes two CharSequences, first the characters to match, the second the characters to replace it with. Find the ", " and replace with "\n". So
CharSequence theseChars = new CharSequence(", ");
CharSequence withTheseChars = new CharSequence("\n");
String newString = A.replace(theseChars, withTheseChars);
And that should get the job done.
I have used the most basic stuff of Java.
I think this is easy to understand
String s = "a,b,c,d";
String s1 =s.replace(",", "");
String s2 = s1.replace("", "\n").toUpperCase();

Categories

Resources