I want to split a string at a certain point, but keep the character I'm splitting it at. For example:
Input:
a:b
Output:
a
:
b
Is there anyway I can use this with Java's split() method? Thanks :)
I want to split a string at a certain point, but keep the character I'm splitting it at. For example:
Input:
a:b
Output:
a
:
b
Is there anyway I can use this with Java's split() method? Thanks :)
You can use StringTokenizer
class in Java.
StringTokenizer stk = new StringTokenizer(inputStr,":", true);
This will return tokens as well as delimiter.
Quick solution from my mind
____:____
____
____
is just a string which I am sure doesn't exist in the string. You can use anyother substring as well depending upon your use case
Order of time complexity O(3*N)
For Example
Your string is a:b
.
Split it with :
String Array you get is ["a", "b"]
Join with ____:____
String you get is a_:_b
Split again with ____
You get ["a", ":", "b"] (desired output)
Just giving an idea.
Scanner = new Scanner(System.in);
String input = s.next();
for (String str : input.split()) {
System.out.println(str);
System.out.println(":");
}