The filter string - removing some chars - java

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

Related

How to break a string into an array

I have a problem with parsing text, i have transcript of interview and i have a tag which channel is talking (ch1,ch2). And i need to break it into array and i could to search in which channel someone tells specific word.
For example this is a part of interview
<ch1>Hello</ch1> <ch2>Hello</ch2> <ch1>How are you</ch1><ch2>I'm fine</ch2>
This is a string
String text = "<ch1>Hello</ch1> <ch2>Hello</ch2> <ch2>How are you</ch2>
<ch2>I'm fine</ch2>";
And i want output
String output[] = {<ch1>Hello</ch1>,<ch2>Hello</ch2>,....}
Thanks for help.
You can use a regular expression with lookahead and lookbehind:
String dialogue = "<ch1>Hello</ch1> <ch2>Hello</ch2> <ch1>How are you</ch1><ch2>I'm fine</ch2>";
String[] statements = dialogue.split("(?<=</ch[12]>)\\s*(?=<ch[12]>)");
System.out.println(Arrays.asList(statements));
Output:
[<ch1>Hello</ch1>, <ch2>Hello</ch2>, <ch1>How are you</ch1>, <ch2>I'm fine</ch2>]
It's a bit hard to read due to the many < and >, but the pattern is like this:
split("(?<=endOfLastPart)inBetween(?=startOfNextPart)")
text.split("<ch").join("-<ch").split("-").
Can be any string instead of "-" which can be used.

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();

Error when splitting a string

Im having this really weird issue that i haven't been able to figure out for a few hours. Basically im trying to split this getInterfaceBounds-client.ry, what im doing is this
final String className = line.split(".")[0];
im getting a arrayindexoutofbounds exception. I really have no idea why, do you?
Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 0
split uses a regular expression. In regex . means any character, so you need to escape it.
Try:
final String className = line.split("\\.")[0];
See http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum
The change required is :
final String className = line.split("\\.")[0];
Check this example for more details.
String s="getInterfaceBounds-client.ry";
String[] arr = s.split("\\.");
for(String str : arr)
{
System.out.println(str);
}
Ideone link.
Use this instead and it will work. I just tested it.
String line = "getInterfaceBounds-client.ry";
String className = line.split("[.]")[0];
System.out.println(className);
The . is a special character in regex which represent any character.
You can learn more about the different special characters in regex here:
http://www.fon.hum.uva.nl/praat/manual/Regular_expressions_1__Special_characters.html
Use Array variable for split() beacuse we may get more than a value from splitting so it would be helpful if u use array it would avoid confusion of accesing the values for example:
String line = "getInterfaceBounds-client.ry.test";
String test[] = line.split("[.]");
System.out.println(test[0]+test[1]+test[2]);

Split the string

abcd+xyz
i want to split the string and get left and right components with respect to "+"
that is i need to get abcd and xyz seperatly.
I tried the below code.
String org = "abcd+xyz";
String splits[] = org.split("+");
But i am getting null value for splits[0] and splits[1]...
Please help..
The string you send as an argument to split() is interpreted as a regex (documentation for split(String regex)). You should add an escape character before the + sign:
String splits[] = org.split("\\+");
You might also find the Summary of regular-expression constructs worth reading :)
"+" is wild character for regular expression.
So just do
String splits[] = org.split("\\+");
This will work
the expression "+" means one or many in java regular expression.
split takes Regex as a argument hence the comparion given by you fails
So use
String org = "abcd+xyz";
String splits[] = org.split(""\+");
regards!!
Try:
String splits[] = org.split("\\+");

Categories

Resources