I have a string "I love stack overflow very much"
how to remove spaces between character and make groups of 8 character? how to add dummy data if no of character does not divided by 8 to make group of 8?how to add dummy data to make groups of 8 characrers?
Asked
Active
Viewed 87 times
-7

Tim Biegeleisen
- 502,043
- 27
- 286
- 360

hamzasgd
- 15
- 5
-
How far have you tried? To generate a random char, you can reference http://stackoverflow.com/questions/5202866/in-java-how-do-you-randomly-select-a-letter-a-z – Antony Dao Nov 24 '16 at 04:12
-
Before your question gets closed, please show us what actual output you want, and also any code you have tried. – Tim Biegeleisen Nov 24 '16 at 04:19
2 Answers
-1
Here is a one-liner solution using a regex which you can try:
String input = "I love stack overflow very much";
String[] parts = input.replaceAll("\\s", "").split("(?<=\\G.{8})");
String dummy = "X"; // or whatever placeholder you want
parts[parts.length-1] += new String(new char[8 - parts[parts.length-1].length()]).replace("\0", dummy);
System.out.println(Arrays.toString(parts));
This solution pads the final term in the array on the right with Xs, should the final term have less than 8 characters.
Output:
[Ilovesta, ckoverfl, owverymu, chXXXXXX]

Tim Biegeleisen
- 502,043
- 27
- 286
- 360
-
-
It does NOT satisfy *all* of the requirements: "*add dummy data if no of character does not divided by 8*" – Remy Lebeau Nov 24 '16 at 04:23
-
@RemyLebeau I updated my answer to add right padding to the final string, should that be necessary. – Tim Biegeleisen Nov 24 '16 at 04:29
-
-
-
in your output last one is 'ch' but i want that group exactly 8 character using dummy data – hamzasgd Nov 24 '16 at 07:21
-
@HamzaMasood No, it _is_ 8 characters, I padded it to the right with spaces. – Tim Biegeleisen Nov 24 '16 at 07:23
-
-
-
The third line pads the last array entry to the right with spaces. – Tim Biegeleisen Nov 24 '16 at 07:26
-
-
you said reverse your input but out put is this [ch , owverymu,ckoverfl, Ilovesta ] I need this [[atsevoll, lfrevokc, umyrevwo, hc ] – hamzasgd Nov 24 '16 at 08:07
-
@sgd You have showed zero effort or gratitude for my time. I won't help you anymore, sorry. – Tim Biegeleisen Nov 24 '16 at 08:08
-
-2
public class Run {
public static void main(String[] args) {
String string = "I love stack overflow very much";
//replacing all newline and and then making tokens
String[] words = string.replaceAll("\\s", "").split("(?<=\\G.{8})");
for (String st : words) {
if (st.length() == 8) { // if length of the string is 8, just print the string
System.out.println(st);
} else {
System.out.print(st);
// printing dummy characters after the final string
for (int i = 0; i < 8 - st.length(); i++) {
System.out.print("X"); // assuming dummy character = X;
}
}
}
System.out.println("\n");
// Reverse
for (String st : words) {
if (st.length() == 8) {
System.out.println(new StringBuilder(st).reverse().toString());
} else {
String revString = st;
for (int i = 0; i < 8 - st.length(); i++) {
revString += "X";
}
System.out.println(new StringBuilder(revString).reverse().toString());
}
}
}
}
OUTPUT
Ilovesta
ckoverfl
owverymu
chXXXXXX
atsevolI
lfrevokc
umyrevwo
XXXXXXhc

rimonmostafiz
- 1,341
- 1
- 15
- 33
-
2While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) of how and why this solves the problem would really help to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply. – Makyen Nov 24 '16 at 04:44
-
-
2You shouldn't just give the answer like this. The poster of the question appears to be trying to figure out his homework. Can you re-edit to not include the details of the answer? Thanks! – Alvin Bunk Nov 24 '16 at 05:12
-
-
-
see this http://stackoverflow.com/questions/7569335/reverse-a-string-in-java – rimonmostafiz Nov 24 '16 at 07:31
-