Java, How to split String with shifting - java

How can I split a string by 2 characters with shifting.
For example;
My string is = todayiscold
My target is: "to","od","da","ay","yi","is","sc","co","ol","ld"
but with this code:
Arrays.toString("todayiscold".split("(?<=\\G.{2})")));
I get: `"to","da","yi","co","ld"
anybody helps?

Try this:
String e = "example";
for (int i = 0; i < e.length() - 1; i++) {
System.out.println(e.substring(i, i+2));
}

Use a loop:
String test = "abcdefgh";
List<String> list = new ArrayList<String>();
for(int i = 0; i < test.length() - 1; i++)
{
list.add(test.substring(i, i + 2));
}

Following regex based code should work:
String str = "todayiscold";
Pattern p = Pattern.compile("(?<=\\G..)");
Matcher m = p.matcher(str);
int start = 0;
List<String> matches = new ArrayList<String>();
while (m.find(start)) {
matches.add(str.substring(m.end()-2, m.end()));
start = m.end()-1;
}
System.out.println("Matches => " + matches);
Trick is to use end()-1 from last match in the find() method.
Output:
Matches => [to, od, da, ay, yi, is, sc, co, ol, ld]

You cant use split in this case because all split does is find place to split and brake your string in this place, so you cant make same character appear in two parts.
Instead you can use Pattern/Matcher mechanisms like
String test = "todayiscold";
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile("(?=(..))");
Matcher m = p.matcher(test);
while(m.find())
list.add(m.group(1));
or even better iterate over your Atring characters and create substrings like in D-Rock's answer

Related

Substring first, second, third, ... , n match

Given: String s = "{aaa}{bbb}ccc.
How to get an array (or list) which elements will be:
0th element: aaa
1st element: bbb
2nd element: ccc
This is my try:
String x = "{aaa}{b}c";
return Arrays.stream(x.split("\\}"))
.map(ss -> {
Pattern pattern = Pattern.compile("\\w*");
Matcher matcher = pattern.matcher(ss);
matcher.find();
return matcher.group();
})
.toArray(String[]::new);
(assume only Java <= 8 allowed)
A simple replace should be enough if your strings are well formed like your examples:
String[] myStrings = {"{aaa}bbb", "{aaa}{bbb}{ccc}ddd", "{aaa}{bbb}{ccc}{ddd}eee"};
for(String str : myStrings){
String[] splited = str.replace("}{", "}").replace("{", "").split("}");
System.out.println(Arrays.toString(splited));
}
prints:
[aaa, bbb]
[aaa, bbb, ccc, ddd]
[aaa, bbb, ccc, ddd, eee]
private static List<String> parse ()
{
String x = "{aaa}{b}c";
Pattern pattern = Pattern.compile ("[^{\\}]+(?=})");
List < String > allMatches = new ArrayList < String > ();
Matcher m = pattern.matcher (x);
while (m.find ())
{
allMatches.add (m.group ());
}
String lastPart = x.substring(x.lastIndexOf("}")+1);
allMatches.add(lastPart);
System.out.println (allMatches);
return allMatches
}
Make sure you do a check for lastIndexOf >-1, if your string may or may not contain last part without braces.
This way is a bit simpler than using regex (and may be a bit faster too):
String[] strings = new String[100];
int index = 0;
int last = 0;
for(int i = 1; i < s.length(); i++){
if(s.charAt(i) == "}"){
strings[index++] = s.substring(last + 1, i - 1);
last = i + 1;
}
}
strings[index++] = s.substring(last, s.length());
If you want to use regex, the pattern needs to identify sequences of one or more letters, you can try the pattern (?:{([a-z]+)})*([a-z]+).

find overlapping regex pattern

I'm using regex to find a pattern
I need to find all matches in this way :
input :"word1_word2_word3_..."
result: "word1_word2","word2_word3", "word4_word5" ..
It can be done using (?=) positive lookahead.
Regex: (?=(?:_|^)([^_]+_[^_]+))
Java code:
String text = "word1_word2_word3_word4_word5_word6_word7";
String regex = "(?=(?:_|^)([^_]+_[^_]+))";
Matcher matcher = Pattern.compile(regex).matcher(text);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Output:
word1_word2
word2_word3
word3_word4
...
Code demo
You can do it without regex, using split:
String input = "word1_word2_word3_word4";
String[] words = input.split("_");
List<String> outputs = new LinkedList<>();
for (int i = 0; i < words.length - 1; i++) {
String first = words[i];
String second = words[i + 1];
outputs.add(first + "_" + second);
}
for (String output : outputs) {
System.out.println(output);
}

How to use regex to split a string containing numbers and letters in java

My task is splitting a string, which starts with numbers and contains numbers and letters, into two sub-strings.The first one consists of all numbers before the first letter. The second one is the remained part, and shouldn't be split even if it contains numbers.
For example, a string "123abc34de" should be split as: "123" and "abc34de".
I know how to write a regular expression for such a string, and it might look like this:
[0-9]{1,}[a-zA-Z]{1,}[a-zA-Z0-9]{0,}
I have tried multiple times but still don't know how to apply regex in String.split() method, and it seems very few online materials about this. Thanks for any help.
you can do it in this way
final String regex = "([0-9]{1,})([a-zA-Z]{1,}[a-zA-Z0-9]{0,})";
final String string = "123ahaha1234";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
matcher.group(1) contains the first part and matcher.group(2) contains the second
you can add it to a list/array using these values
You can use a pretty simple pattern : "^(\\d+)(\\w+)" which capture digits as start, and then when letters appear it take word-char
String string = "123abc34de";
Matcher matcher = Pattern.compile("^(\\d+)(\\w+)").matcher(string);
String firstpart = "";
String secondPart = "";
if (matcher.find()) {
firstpart = matcher.group(1);
secondPart = matcher.group(2);
}
System.out.println(firstpart + " - " + secondPart); // 123 - abc34de
This is not the correct way but u will get the result
public static void main(String[] args) {
String example = "1234abc123";
int index = 0;
String[] arr = new String[example.length()];
for (int i = 0; i < example.length(); i++) {
arr = example.split("");
try{
if(Integer.parseInt(arr[i]) >= 0 & Integer.parseInt(arr[i]) <= 9){
index = i;
}
else
break;
}catch (NumberFormatException e) {
index = index;
}
}
String firstHalf = example.substring(0,Integer.parseInt(arr[index])+1);
String secondHalf = example.substring(Integer.parseInt(arr[index])+1,example.length());
System.out.println(firstHalf);
System.out.println(secondHalf);
}
Output will be: 1234 and in next line abc123

Java: String to integer array

I have a string, which is a list of coordinates, as follows:
st = "((1,2),(2,3),(3,4),(4,5),(2,3))"
I want this to be converted to an array of coordinates,
a[0] = 1,2
a[1] = 2,3
a[2] = 3,4
....
and so on.
I can do it in Python, but I want to do it in Java.
So how can I split the string into array in java??
It can be done fairly easily with regex, capturing (\d+,\d+) and the looping over the matches
String st = "((1,2),(2,3),(3,4),(4,5),(2,3))";
Pattern p = Pattern.compile("\\((\\d+),(\\d+)\\)");
Matcher m = p.matcher(st);
List<String> matches = new ArrayList<>();
while (m.find()) {
matches.add(m.group(1) + "," + m.group(2));
}
System.out.println(matches);
If you genuinely need an array, this can be converted
String [] array = matches.toArray(new String[matches.size()]);
Alternative solution:
String str="((1,2),(2,3),(3,4),(4,5),(2,3))";
ArrayList<String> arry=new ArrayList<String>();
for (int x=0; x<=str.length()-1;x++)
{
if (str.charAt(x)!='(' && str.charAt(x)!=')' && str.charAt(x)!=',')
{
arry.add(str.substring(x, x+3));
x=x+2;
}
}
for (String valInArry: arry)
{
System.out.println(valInArry);
}
If you don't want to use Pattern-Matcher;
This should be it:
String st = "((1,2),(2,3),(3,4),(4,5),(2,3))";
String[] array = st.substring(2, st.length() - 2).split("\\),\\(");

Need a regexp to extract a Sub String of a String

I have a string, The string looks like :
abc/axs/abc/def/gh/ij/kl/mn/src/main/resources/xx.xml
I want to get the content after n occurrences and before m occurrences of the character /.
For instance, from the string above, I want:
mn/src/main
Please suggest some solution for this.
the regex you require is this :
(?:.*?\/){7}(.*?)(.*)(?:\/.*?){2}$
a generic regex:
(?:.*?\/){n}(.*?)(.*)(?:\/.*?){m}$
substitute 7 and 2 with n and m and you will get your result
demo here:
http://regex101.com/r/bW2yF3
Use split().
String path = "abc/axs/abc/def/gh/ij/kl/mn/src/main/resources/xx.xml"
String [] tokens = path.split("/");
Now just print it:
for (int i = n; i < m; i++){
System.out.print(tokens[i] + (i != m - 1 ? "/" : ""));
}
If you must use regex:
String s = "abc/axs/abc/def/gh/ij/kl/mn/src/main/resources/xx.xml";
int n = 7;
int m = 10;
Pattern p = Pattern.compile("(?:[^/]*/){" + n + "}((?:[^/]*/){" + (m - n - 1) + "}[^/]*)/.*");
Matcher matcher = p.matcher(s);
if (matcher.matches()) {
System.out.println(matcher.group(1));
}

Categories