Split string in latest occurrence of substring - java

I need help to split string when substring is found, only on latest occurrence
Substring to search(example):123
String: 123hello123boy123guy123girl
Res: 123hello123boy123guy (result on split[0])
Ex: hello123boy
Res:hello
I'm trying with this function, but this split only on first occource.
public static String[] getSplitArray(String toSplitString, String spltiChar) {
return toSplitString.split("(?<=" + spltiChar + ")");
}

If you want to do it in single split(), this should work:
string.split("123((?!123).)*?$")
E.g.:
String s = "foo123bar123hhh123tillHere123Bang";
System.out.println(s.split("123((?!123).)*?$")[0]);
Outputs:
foo123bar123hhh123tillHere
Another approach would be, just split("123") then join the elements you required by 123 as delimiter.

Improvement on Kent's answer: this will actually give you a split array rather than a single-element array with the first part of your String:
String[] text = {
"123hello123boy123guy123girl", // 123hello123boy123guy
"hello123boy"// hello
};
for (String s: text) {
System.out.println(
Arrays.toString(
s.split("123(?=((?!123).)*?$)")
)
);
}
Output
[123hello123boy123guy, girl]
[hello, boy]

You don't need a regex for that, you can simply use String#lastIndexOf:
-> String s = "123hello123boy123guy123girl"
-> s.substring(0, s.lastIndexOf("123"));
| Expression value is: "123hello123boy123guy"
-> s.substring(s.lastIndexOf("123"));
| Expression value is: "123girl"

Related

Split string in reverse

I have a string sub1-sub2-sub3 which I want to split from right to left. Currently I am using split function like this which splits the string from left to right -
String str = "sub1-sub2-sub3";
System.out.println("Result :" + str.split("-", 2));
Elements in output :
sub1
sub2-sub3
Desired output :
sub1-sub2
sub3
Is there a way where I can split my string on - starting from right?
You could do a regex split on -(?!.*-):
String str = "sub1-sub2-sub3";
String[] parts = str.split("-(?!.*-)");
System.out.println(Arrays.toString(parts)); // [sub1-sub2, sub3]
The regex pattern used here will only match the final dash.
A generic solution as utility method that takes limit as input with java 8:
public List<String> splitReverse(String input, String regex, int limit) {
return Arrays.stream(reverse(input).split(regex, limit))
.map(this::reverse)
.collect(Collectors.toList());
}
private String reverse(String i){
return new StringBuilder(i).reverse().toString();
}
reverse() code taken from here
Input: sub1-sub2-sub3
Reverse input: 3bus-2bus-1bus
Split(rev,2): [3bus] , [2bus-1bus]
Reverse each: [sub3] , [sub1-sub2]
One way would be to reverse the string before the split and then reverse the resulting strings.
String str = "sub1-sub2-sub3";
Arrays.stream(StringUtils.reverse(str).split("-",2))
.map(StringUtils::reverse)
.forEach(System.out::println);
Output:
sub3
sub1-sub2
It won't yield the same result as requested but maybe it will help.
P.S. util class used is from apache: org.apache.commons.lang3.StringUtils
A quick and dirty solution i cam up with would be
String str = "sub1-sub2-sub3";
str = StringUtils.reverse(str);
String[] split = str.split("-", 2);
split[0] = StringUtils.reverse(split[0]);
split[1] = StringUtils.reverse(split[1]);
System.out.println("Result :" + split[0] + " #### " + split[1]);
another way would be java how to split string from end
The below solution worked for me fine :
str.substring(0, str.lastIndexOf("-"));
str.substring(str.lastIndexOf("-"));

Remove/Replace the part of string value

I have to remove the words from the given string.
Example :
Input:
"H|013450107776|10/15/2019
D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful"
Output:
"H|013450107776|10/15/2019
D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|Successful"
Note:"PAYMENT FOR SERVICE" is a dynamic string value it can be any thing.
I have tried using replace() and regex function but i am not able to get the exact output.
The following code will work.
public static String replace(String original, String toRemove) {
Arrays.stream(original.split("\\|"))
.filter(s -> !(s.equals(toRemove)))
.collect(Collectors.joining("|"));
}
First, create a Stream of Strings (Stream<String>) that are originally separated by |.
Second, filter them, so only Strings that are not equal to toRemove remain in the Stream.
Thrid, collect using joining with a joining character |.
Splitting your string on "|" and assuming the word you want to replace is always at the same position, the below does what you need :
String s = "H|013450107776|10/15/2019D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful";
String result = s.replace(s.split("\\|")[8], "");
System.out.println(result);
It prints out :
H|013450107776|10/15/2019D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00||Successful
Here is a trick I like to use:
String input = "H|013450107776|10/15/2019D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful";
System.out.println(input);
input = "|" + input + "|";
String output = input.replaceFirst("\\|PAYMENT FOR SERVICE\\|", "|");
output = output.substring(1, output.length()-1);
System.out.println(output);
To see how this works, consider the following input:
A|B|C
Let's say that we want to remove A. We first form the string:
|A|B|C|
Then, we replace |A| with just |, to give:
|B|C|
Finally, we strip those initial added pipe separators to give:
B|C
String str = "H|013450107776|10/15/2019D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful";
System.out.println(str.replaceAll("PAYMENT FOR SERVICE|", ""));`
public static void main(String[] args) {
String str = "H|013450107776|10/15/2019D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful";
String scut = "PAYMENT FOR SERVICE";
System.out.println(str.substring(0,str.indexOf(scut)) + str.substring(str.indexOf(scut)+scut.length()+1,str.length()));
}
replace all uppercase words between || with "|"
for example: "|A G G SLG SD GSD G|" -> "|"
input.replaceAll("\\|[A-Z\\s]+\\|","|")
\\s - any whitespace symbol
A-Z - symbols between A and Z
more info : https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

String split function in Java

I have a String and I want to split it by ","
If suppose I have a String like,
String test = "aa,bb,cc";
now I can split it by,
String[] spl = test.split(",");
And the spl.length is 3
If suppose my String is
String test = ",,,";
Here the splitted String length is 0. But my expected answer is 3.
My test String is dynamaic value and it may varies like, Now think I have a String like
String test = ",aa,dd,,,,,ff,gg"
Now the splited array length is 4. But I expected answer is 9
And I need to split by "," and I need the aa position at spl[1] and dd position as spl[2] and ff position as spl[7]
Can someone give the suggestion about to solve this issue..
Use split() with -1 as limit
public static void main(String[] args) {
String test = ",,,";
System.out.println(Arrays.toString(test.split(",", -1))); // adds leading and trailing empty Strings .
// so effectively its like adding "" before , after and between each ","
String test1 = "aa,bb,cc";
System.out.println(Arrays.toString(test1.split(",",-1)));
}
O/P :
[, , , ] -- > Length =4
[aa, bb, cc]
To get the behavior you want you can just replace "," by " ,":
String test = ",,";
test = test.replace(",", " ,");
System.out.println((test.split(",").length));
With the split() function, java separates a String by the Substring of your choice. If there is nothing between them, the field will not be null, it will just be skipped.
In other programming languages, you could come across something like this:
String example = ',,,'
String[] example2 = example.split(',')
print(example2.length())
This could also deliver 4. Because there are 4 spaces around the ',' chars:
1,2,3,4

java split () method

I've got a string '123' (yes, it's a string in my program). Could anyone explain, when I use this method:
String[] str1Array = str2.split(" ");
Why I got str1Array[0]='123' rather than str1Array[0]=1?
str2 does not contain any spaces, therefore split copies the entire contents of str2 to the first index of str1Array.
You would have to do:
String str2 = "1 2 3";
String[] str1Array = str2.split(" ");
Alternatively, to find every character in str2 you could do:
for (char ch : str2.toCharArray()){
System.out.println(ch);
}
You could also assign it to the array in the loop.
str2.split("") ;
Try this:to split each character in a string .
Output:
[, 1, 2, 3]
but it will return an empty first value.
str2.split("(?!^)");
Output :
[1, 2, 3]
the regular expression that you pass to the split() should have a match in the string so that it will split the string in places where there is a match found in the string. Here you are passing " " which is not found in '123' hence there is no split happening.
Because there's no space in your String.
If you want single chars, try char[] characters = str2.toCharArray()
Simple...You are trying to split string by space and in your string "123", there is no space
This is because the split() method literally splits the string based on the characters given as a parameter.
We remove the splitting characters and form a new String every time we find the splitting characters.
String[] strs = "123".split(" ");
The String "123" does not have the character " " (space) and therefore cannot be split apart. So returned is just a single item in the array - { "123" }.
To do the "Split" you must use a delimiter, in this case insert a "," between each number
public static void main(String[] args) {
String[] list = "123456".replaceAll("(\\d)", ",$1").substring(1)
.split(",");
for (String string : list) {
System.out.println(string);
}
}
Try this:
String str = "123";
String res = str.split("");
will return the following result:
1,2,3

How does String.split work?

Why does the following code return the output below?
I would expect that 2 and 3 provide the same string splitting of 1.
Log.d(TAG, " 1 ---------------------------");
String originalText = "hello. .hello1";
Pattern p = Pattern.compile("[a-zA-Z]+|\\s|\\W|\\d");
Matcher m = p.matcher(originalText);
while (m.find()) {
Log.d(TAG, m.group(0));
}
Log.d(TAG, "2 --------------------------- " + originalText);
String [] scrollString = p.split(originalText);
int i;
for (i=0; i<scrollString.length; i++)
Log.d(TAG, scrollString[i]);
Log.d(TAG, "3 --------------------------- " + originalText);
scrollString = originalText.split("[a-zA-Z]+|\\s|\\W|\\d");
for (i=0; i<scrollString.length; i++)
Log.d(TAG, scrollString[i]);
OUTPUT:
1 ---------------------------
hello
.
.
hello
1
2 ---------------------------
3 ---------------------------
No. 1 will find the pattern and return that, whereas No. 2 and 3 will return the text in between the found pattern (which serves as the delimiter in those cases).
Your subject doesn't match what you are asking.
The Subject asks about String.split() you are doing Pattern.split() which one do you really want help with?
When using String.split(); you pass in the regular expression to apply to the string, not the string you want to split!
JavaDoc for String.split();
final String s = "this is the string I want to split";
final String[] sa = s.split(" ");
you are calling .split on p ( Pattern.split(); )
Pattern p = Pattern.compile("[a-zA-Z]+|\\s|\\W|\\d");
String [] scrollString = p.split(originalText);
these too methods have different behaviors.
the split() methods don't add the captured part of the string (the delimiter) to the result array
if you want the delimiters you'll have to play with lookahead and lookbehind (or use version 1)
No. Every character in your string is covered by the split pattern, hence taken as something you don't want. Therefore, you get the empty result.
You can imagine that your pattern first finds "hello", then split hopes to find something, but alas!, it finds another "separation" character.

Categories

Resources