You can use the split() method. It returns an array String[], so you can access to an element by using the syntax someArray[index]. Since you want to get the first elemnt, you can use [0].
String first_word = s.split(",")[0];
Note:
- Indices in most programming languages start with
0. So the first elemnt will be in the index0. The second in the index1. So on.
You can use the split() method. It returns an array String[], so you can access to an element by using the syntax someArray[index]. Since you want to get the first elemnt, you can use [0].
String first_word = s.split(",")[0];
Note:
- Indices in most programming languages start with
0. So the first elemnt will be in the index0. The second in the index1. So on.
If you look at String.split(String regrex), you will find that it will return an array of strings.
name1 = s.split(", ")[0];
The [0] is the first index in an array, so it will be your name1. s.split(", ").length is equal to the size of the array, so if you ever need any index s.split(", ")[num-1] where num is the number of the index you want starting at one.
regex - java - after splitting a string, what is the first element in the array? - Stack Overflow
split string only on first instance - java - Stack Overflow
How to consider first token of splitted string using Java 8 streams - Stack Overflow
java - String split method returning first element as empty using regex - Stack Overflow
Consider the split expression ",1,2,3,4".split(",");
What would you expect? Right, an empty-string to start with. In your case you have a 'nothing' in front of the first 'a' as well as one behind it.
Update: comments indicate this explanation is not enough of an explanation (which it may not be)... but, it really is this simple: the engine starts at the beginning of the string, and it looks to see if what's in front of it matches the pattern. If it does, it assigns what's behind it to a new item in the split.
On the first character, it has "" (nothing behind it), and it looks to see if there's "" (the pattern) in front of it. There is, so it creates a "" match.
It then moves on, and it has 'a' behind it, and again, it again has "" in front of it. So the second result is an "a" string.
An interesting observation is that, if you use split("", -1) you will also get an empty-string result in the last position of the result array.
Edit 2: If I wrack my brains further, and consider this to be an academic exercise (I would not recommend this in real life...) I can think of only one good way to do a regex split() of a String into a String[] array with 1 character in each string (as opposed to char[] - which other people have given great answers for....).
String[] chars = str.split("(?<=.)", str.length());
This will look behind each character, in a non-capturing group, and split on that, and then limit the array size to the number of characters (you can leave the str.length() out, but if you put -1 you will get an extra space at the end)
Borrowing nitro2k01's alternative (below in the comments) which references the string beginning and end, you can split reliably on:
String[] chars = str.split("(?!(^|$))");
You can just use the built in java method from the string class. myString.toCharArray() the empty string is being stored at index 0
string.split("=", limit=2);
As String.split(java.lang.String regex, int limit) explains:
The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.
The
limitparameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter.
The string
boo:and:foo, for example, yields the following results with these parameters:
Regex Limit Result : 2 { "boo", "and:foo" } : 5 { "boo", "and", "foo" } : -2 { "boo", "and", "foo" } o 5 { "b", "", ":and:f", "", "" } o -2 { "b", "", ":and:f", "", "" } o 0 { "b", "", ":and:f" }
Yes you can, just pass the integer param to the split method
String stSplit = "apple=fruit table price=5"
stSplit.split("=", 2);
Here is a java doc reference : String#split(java.lang.String, int)
Don't compile a pattern for each element (via split), but create it before the loop. Also it seems that you want to have a new List as a result, something along the lines:
Pattern p = Pattern.compile("\\.");
List<String> out = result
.stream()
.map(x -> p.splitAsStream(x).findFirst().orElse(x))
.collect(Collectors.toList());
On the other hand if you want to do it in place:
result.replaceAll(x -> p.splitAsStream(x).findFirst().orElse(x));
Because the split() returns the whole String if there’s no separator, you don’t need the if, and you can apply the transformation safely to all elements.
You can replace all your code with just this:
result.replaceAll(s -> s.split("\\.")[0]);
——
Actually, you don’t need to split either: you can use String#replaceAll() to remove everything from the first dot onwards instead of splitting:
result.replaceAll(s -> s.replaceAll("\\..*", ""));
The call to replaceAll("\\..*", "") is a no op if there’s no dot in the String.
You should use matching. Change your expression to:
`^\[(.*?)\.\.(.*)\]$`
And get your results from the two captured groups.
As for why split acts this way, it's simple: you asked it to split on the [ character, but there's still an "empty string" between the start of the string and the first [ character.
Your regex is matching [ and .. and ]. Thus it will split at this occurrences.
You should not use a split but match each number in your string using regex.
You just need to make sure that any trailing or leading dots are removed.
So one solution is:
row.replaceAll("^\\.+|\\.+$", "").split("\\.+");
For this pattern you can use replaceFirstMethod() and then split by dot
String[] arr = row.replaceFirst("\\.+","").split("\\.");
Output will be
["O","O","O"]