I have string where only a certain part should be selected. until i reach a character.
Ex. 5000 - 10000 i want only 5000 until the - or the white space.
input.replace("","");
What Regular expression should i be using.
Something like this:
final String beforeDash = input.split("-")[0].trim();
This should solve your problem:
String[] parts = input.split("-");
The string you are looking for is then in parts[0].
If you want to split on the whitespace instead of the dash, use string.split(" ").
You could try the below code which matches the first space or - upto the last character. Replacing those matched characters with an empty string will gave you the desired output.
input.replaceAll("[\\s-].*","");
You could also use string.split function.
String[] parts = input.split("[\\s-]");
System.out.println(parts[0]);
The above split function would split the input according to a space or a hyphen. Printing the index 0 from the splitted parts will give you the desired output.
Related
I was solving a problem on string.split(), but I couldn't find the solution to remove white-space at the start. Actually the problem is in the regular expression. It needs to be changed. I tried to solve by changing the expression several times but didn't work out. What will be the regular expression? Here is the code given below:
String s = " YES leading spaces are valid, problemsetters are evillllll";
String delims = "[\\s._,?!'#\\t]+";
String[] words = s.split(delims);
System.out.println(words.length - 1);
for(String w:words) {
System.out.println(w);
}
System.out.println(Arrays.toString(words));
output:
8
YES
leading
spaces
are
valid
problemsetters
are
evillllll
[, YES, leading, spaces, are, valid, problemsetters, are, evillllll]
You can use String class trim() method to remove the space before using split
I have a rather large String that i need to split so I can put it into an array. As it is, there will be a semicolon followed by an Integer, followed by a space and this is where I need to split it.
Say for instance, I have a String:
first aaa;0 second bbb;1 third ccc;2
I need to split it so that it becomes:
first aaa;0
second bbb;1
third ccc;2
I assume I can use something like:
Pattern pattern = Pattern.compile(^([0-9]*\s");
myArray = pattern.split(string_to_split);
I just don't understand RegEx that well yet.
Thanks to anyone taking a look
Also, the pattern where it should be split will always be a semicolon, followed by only one digit and then the space.
Just split your input string according to the below regex.
(?<=;\\d)\\s
Code:
String s = "first aaa;0 second bbb;1 third ccc;2";
String[] tok = s.split("(?<=;\\d)\\s");
System.out.println(Arrays.toString(tok));
Output:
[first aaa;0, second bbb;1, third ccc;2]
Explanation:
(?<=;\d) Positive lookbehind is used here. It sets the matching marker just after to the ;<number>. That is, it asserts what precedes the space character is must be a semicolon and a number.
(?<=;\d)\s Now it matches the following space character.
Splitting your input string according to that matched space will give you the desired output.
Part of my application I encountered this problem. The String line variable contains 12.2 Andrew and I'm trying to split them separately but it doesn't work and comes with a NumberFormatException error. Could you guys help me on that please?
String line = "12.2 Andrew";
String[] data = line.split("(?<=\\d)(?=[a-zA-Z])");
System.out.println(Double.valueOf.(data[0]));
Did you look at your data variable? It didn't split anything at all, since the condition never matches. You are looking for a place in the input immediately after a number and before a letter, and since there is a space in between this doesn't exist.
Try adding a space in the middle, that should fix it:
String[] data = line.split("(?<=\\d) (?=[a-zA-Z])");
Your split is not working, and not splitting the String.
Therefore Double.parseDouble is parsing the whole input.
Try the following:
String line = "12.2 Andrew";
String[] data = line.split("(?<=\\d)(?=[a-zA-Z])");
System.out.println(Arrays.toString(data));
// System.out.println(Double.valueOf(data[0]));
// fixed
data = line.split("(?<=\\d).(?=[a-zA-Z])");
System.out.println(Arrays.toString(data));
System.out.println(Double.valueOf(data[0]));
Output
[12.2 Andrew]
[12.2, Andrew]
12.2
If you print content of data[0] you will notice that it still contains 12.2 Andrew so you actually didn't split anything. That is because your regex says:
split on place which has digit before and letter after it
which for data like
123foo345bar 123 baz
effectively can only split in places marked with |
123|foo345|bar 123 baz
^it will not split `123 baz` like
`123| baz` because after digit is space (not letter)
`123 |baz` before letter is space (not digit)
so regex can't match it
What you need is to "split on space which has digit before and letter after it" so use
String[] data = line.split("(?<=\\d)\\s+(?=[a-zA-Z])");
// ^^^^ - this represent one ore more whitespaces
Here's a problem I'm facing when I try to break my string (which I took as an input).
IDK why it is taking this extra 'space' in the beginning of the arrayTry.
Why: " H i ."? Why not "H i ."
Please help me out.
2) Furthermore, how can i take paragraph as an input?
How will the 'new line' be shown in the broken string array?
Thanks.
There is no extra space. That is just an empty String with a space behind it. Namely, the one you wrote in your loop:
System.out.println(arrayTry[i] + " ");
So, this means that arrayTry[0] is an empty String.
When you split on "", it will return an array of Strings, where there is one character per String, and apparently an extra empty String in the beginning and at the end.
To split on newlines, just write it using the \n escape character for newline:
String[] paragraphs = input.split("\n+");
Trim your string (ie remove leading and trailing spaces) before splitting.
String strng = input.nextline();
strng = strng.trim();
strng.split(" ");
I have a string like
String myString = "hello world~~hello~~world"
I am using the split method like this
String[] temp = myString.split("~|~~|~~~");
I want the array temp to contain only the strings separated by ~, ~~ or ~~~.
However, the temp array thus created has length 5, the 2 additional 'strings' being empty strings.
I want it to ONLY contain my non-empty string. Please help. Thank you!
You should use quantifier with your character:
String[] temp = myString.split("~+");
String#split() takes a regex. ~+ will match 1 or more ~, so it will split on ~, or ~~, or ~~~, and so on.
Also, if you just want to split on ~, ~~, or ~~~, then you can limit the repetition by using {m,n} quantifier, which matches a pattern from m to n times:
String[] temp = myString.split("~{1,3}");
When you split it the way you are doing, it will split a~~b twice on ~, and thus the middle element will be an empty string.
You could also have solved the problem by reversing the order of your delimiter like this:
String[] temp = myString.split("~~~|~~|~");
That will first try to split on ~~, before splitting on ~ and will work fine. But you should use the first approach.
Just turn the pattern around:
String myString = "hello world~~hello~~world";
String[] temp = myString.split("~~~|~~|~");
Try This :
myString.split("~~~|~~|~");
It will definitely works. In your code, what actually happens that when ~ occurs for the first time,it count as a first separator and split the string from that point. So it doesn't get ~~ or ~~~ anywhere in your string though it is there. Like :
[hello world]~[]~[hello]~[]~[world]
Square brackets are split-ed in to 5 different string values.