Remove every 8th char from a string
I have a string, which I want to iterate through and remove every 8th char. I have been trying with an modulo operation which check if i % 8 == 0
. However, since I remove every 8th char the length of the string decreases, and I am therefore unable to perform that operation.
StringBuilder str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
str.deleteCharAt(i);
}
}
System.out.println(str + " " + str.length());
The length of the string is in the beginning 64, and after the for loop 57, which should be 56.
java string
|
show 1 more comment
I have a string, which I want to iterate through and remove every 8th char. I have been trying with an modulo operation which check if i % 8 == 0
. However, since I remove every 8th char the length of the string decreases, and I am therefore unable to perform that operation.
StringBuilder str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
str.deleteCharAt(i);
}
}
System.out.println(str + " " + str.length());
The length of the string is in the beginning 64, and after the for loop 57, which should be 56.
java string
You can't modify the size of an array while iterating through it. Make a copy, modify the copy, and when you're done you can forget the original
– emsimpson92
Nov 19 '18 at 17:01
2
Why not create a new string where you add the chars if they are mod 0 through 7, and ignore the 8th?
– Compass
Nov 19 '18 at 17:01
Note that deleting elements from an array would mean you'd have to shift the other elements left by one place and handle the indices accordingly (e.g. index 15 would become 14 when you remove the element at index 7 etc.). Because of that it's often easier to either make a copy and just skip elements or if it needs to be inplace do it back-to-front.
– Thomas
Nov 19 '18 at 17:04
2
You could also use a regex:String shortened = str.replaceAll( "(.{7}).", "$1" );
This will replace each sequence of 8 characters with the first 7 thus effectively skipping every 8th.
– Thomas
Nov 19 '18 at 17:06
Why are you using a String of 64 binary digits when you could just use along
?
– DodgyCodeException
Nov 19 '18 at 17:48
|
show 1 more comment
I have a string, which I want to iterate through and remove every 8th char. I have been trying with an modulo operation which check if i % 8 == 0
. However, since I remove every 8th char the length of the string decreases, and I am therefore unable to perform that operation.
StringBuilder str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
str.deleteCharAt(i);
}
}
System.out.println(str + " " + str.length());
The length of the string is in the beginning 64, and after the for loop 57, which should be 56.
java string
I have a string, which I want to iterate through and remove every 8th char. I have been trying with an modulo operation which check if i % 8 == 0
. However, since I remove every 8th char the length of the string decreases, and I am therefore unable to perform that operation.
StringBuilder str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
str.deleteCharAt(i);
}
}
System.out.println(str + " " + str.length());
The length of the string is in the beginning 64, and after the for loop 57, which should be 56.
java string
java string
edited Nov 26 '18 at 13:05


mike
3,16032553
3,16032553
asked Nov 19 '18 at 16:58
Bab
13210
13210
You can't modify the size of an array while iterating through it. Make a copy, modify the copy, and when you're done you can forget the original
– emsimpson92
Nov 19 '18 at 17:01
2
Why not create a new string where you add the chars if they are mod 0 through 7, and ignore the 8th?
– Compass
Nov 19 '18 at 17:01
Note that deleting elements from an array would mean you'd have to shift the other elements left by one place and handle the indices accordingly (e.g. index 15 would become 14 when you remove the element at index 7 etc.). Because of that it's often easier to either make a copy and just skip elements or if it needs to be inplace do it back-to-front.
– Thomas
Nov 19 '18 at 17:04
2
You could also use a regex:String shortened = str.replaceAll( "(.{7}).", "$1" );
This will replace each sequence of 8 characters with the first 7 thus effectively skipping every 8th.
– Thomas
Nov 19 '18 at 17:06
Why are you using a String of 64 binary digits when you could just use along
?
– DodgyCodeException
Nov 19 '18 at 17:48
|
show 1 more comment
You can't modify the size of an array while iterating through it. Make a copy, modify the copy, and when you're done you can forget the original
– emsimpson92
Nov 19 '18 at 17:01
2
Why not create a new string where you add the chars if they are mod 0 through 7, and ignore the 8th?
– Compass
Nov 19 '18 at 17:01
Note that deleting elements from an array would mean you'd have to shift the other elements left by one place and handle the indices accordingly (e.g. index 15 would become 14 when you remove the element at index 7 etc.). Because of that it's often easier to either make a copy and just skip elements or if it needs to be inplace do it back-to-front.
– Thomas
Nov 19 '18 at 17:04
2
You could also use a regex:String shortened = str.replaceAll( "(.{7}).", "$1" );
This will replace each sequence of 8 characters with the first 7 thus effectively skipping every 8th.
– Thomas
Nov 19 '18 at 17:06
Why are you using a String of 64 binary digits when you could just use along
?
– DodgyCodeException
Nov 19 '18 at 17:48
You can't modify the size of an array while iterating through it. Make a copy, modify the copy, and when you're done you can forget the original
– emsimpson92
Nov 19 '18 at 17:01
You can't modify the size of an array while iterating through it. Make a copy, modify the copy, and when you're done you can forget the original
– emsimpson92
Nov 19 '18 at 17:01
2
2
Why not create a new string where you add the chars if they are mod 0 through 7, and ignore the 8th?
– Compass
Nov 19 '18 at 17:01
Why not create a new string where you add the chars if they are mod 0 through 7, and ignore the 8th?
– Compass
Nov 19 '18 at 17:01
Note that deleting elements from an array would mean you'd have to shift the other elements left by one place and handle the indices accordingly (e.g. index 15 would become 14 when you remove the element at index 7 etc.). Because of that it's often easier to either make a copy and just skip elements or if it needs to be inplace do it back-to-front.
– Thomas
Nov 19 '18 at 17:04
Note that deleting elements from an array would mean you'd have to shift the other elements left by one place and handle the indices accordingly (e.g. index 15 would become 14 when you remove the element at index 7 etc.). Because of that it's often easier to either make a copy and just skip elements or if it needs to be inplace do it back-to-front.
– Thomas
Nov 19 '18 at 17:04
2
2
You could also use a regex:
String shortened = str.replaceAll( "(.{7}).", "$1" );
This will replace each sequence of 8 characters with the first 7 thus effectively skipping every 8th.– Thomas
Nov 19 '18 at 17:06
You could also use a regex:
String shortened = str.replaceAll( "(.{7}).", "$1" );
This will replace each sequence of 8 characters with the first 7 thus effectively skipping every 8th.– Thomas
Nov 19 '18 at 17:06
Why are you using a String of 64 binary digits when you could just use a
long
?– DodgyCodeException
Nov 19 '18 at 17:48
Why are you using a String of 64 binary digits when you could just use a
long
?– DodgyCodeException
Nov 19 '18 at 17:48
|
show 1 more comment
9 Answers
9
active
oldest
votes
The main problem with your code is that you don't adjust i
when removing characters.
Let's visualize that. You want to remove the following marked characters ("every 8th element"):
1100110001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Now we're at i = 7
and remove that character, but because you don't adjust i
accordingly the markers keep the same:
110011001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Let's do that for 1 = 15
to i = 55
:
11001100101101100000000000000000000000000000000000000000000000 //i = 15
1100110010110110000000000000000000000000000000000000000000000 //i = 23
110011001011011000000000000000000000000000000000000000000000 //i = 31
11001100101101100000000000000000000000000000000000000000000 //i = 39
1100110010110110000000000000000000000000000000000000000000 //i = 47
110011001011011000000000000000000000000000000000000000000 //i = 55
^ ^ ^ ^ ^ ^ ^ ^
As you can see, all but the last marker point to a valid character but you won't reach i = 63
because after the first time you remove a character there only are 63 left in the string and thus a max index of 62.
That's why your resulting string has 57 instead of 56 characters, the last "remove" operation doesn't run (and the others except the first remove the wrong elements).
To fix that iterate backwards, i.e. from i = str.length() - 1
to i = 0
. Then you can remove every element where (i + 1) % 8 == 0
.
Alternatively, as I said in my comment, use a regex: String shortened = str.replaceAll( "(.{7}).", "$1" );
This will match any sequence of 7 characters followed by another (8th) character and replaces that with the first group of 7 (thus skipping the 8th).
I like your regex. Elegant solution!
– mike
Nov 26 '18 at 13:12
add a comment |
There is not deleteCharAt
method in String
, so I suppose you meant StringBuilder
?
You can just reverse the direction of the for loop, so that it starts from the end of the string:
String str = "11111111811111118";
StringBuilder builder = new StringBuilder(str);
System.out.println(str + " " + str.length());
for (int i = str.length() - 1; i >= 0; i--) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
builder.deleteCharAt(i);
}
}
System.out.println(builder+ " " + builder.length());
By deleting chars from the end of the string, the indices of the chars to be removed no longer changes as you move along the string.
Using original string:StringIndexOutOfBoundsException: index 64,length 64
– Andreas
Nov 19 '18 at 17:08
@Andreas Oops! Fixed.
– Sweeper
Nov 19 '18 at 17:09
add a comment |
Why don't you use regex and achieve it in two lines of code like this,
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
String replacedStr = str.replaceAll("([01]{7})[01]", "$1");
System.out.println(str.toString() + " " + str.length());
System.out.println(replacedStr.toString() + " " + replacedStr.length());
}
This gives perfectly correct output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
Alternatively, you can follow this traditional solution like you attempted.
Strings in java are immutable. So instead you should create a StringBuilder object and keep copying every character, except 8th character.
For correctly counting every 8th character, initialize your for loop index run from 1 rather than 0, like in this code, which will eradicate every 8th character effectively where you wanted to do if (i%8==0)
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder sb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 1; i <= str.length(); i++) {
// Every 8th element should be discarded
if (i % 8 == 0) {
// str.deleteCharAt(i);
} else {
sb.append(str.charAt(i-1));
}
}
System.out.println(sb.toString() + " " + sb.length());
}
And this gives following output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
You can verify here where only every 8th character is gone in this output.
But the second output should have a length of 56
– Bab
Nov 19 '18 at 17:07
add a comment |
The problem is that Strings are starting with 0. Therefore the 8th element has the index 7 and has to be removed as well, which you don't do in your loop. I'd write it like that (but noting that this might not be the most elegant solution):
public static void main(String args)
{
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
int idx = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
idx++;
if (idx == 8) {
idx = 0;
continue;
}
sb.append(str.charAt(i));
}
System.out.println(sb.toString() + " " + sb.length());
}
Outputs:
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
add a comment |
Assuming that the string does not contain the char with ASCII value 0, convert the string to a char array and change every 8th char with the char with ASCII value 0, then reconstruct the string and replace all chars with ASCII value 0 with "":
String str = "0123456701234567012345670123456701234567012345670123456701234567";
System.out.println("initial = " + str);
char array = str.toCharArray();
for (int i = 7; i < array.length; i = i + 8) {
array[i] = 0;
}
str = String.valueOf(array).replace(String.valueOf(Character.toChars(0)), "");
System.out.println("final = " + str);
will print:
initial = 0123456701234567012345670123456701234567012345670123456701234567
final = 01234560123456012345601234560123456012345601234560123456
add a comment |
Your code won't compile as there is no deleteCharAt()
of the String class. To solve the issue create a new StringBuilder to hold the new values and iterate over str
:
public static void main(String args) throws IOException {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder newSb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
if (i == 0 || ((i + 1) % 8 != 0)) {
newSb.append(str.charAt(i));
}
}
System.out.println(newSb+ " " + newSb.length());
}
Here we skip every 8th index and only append the remaining chars to n. This outputs :
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
add a comment |
An alternative way is using substring()
method.
substring(int beginIndex, int endIndex) Returns a new string that is a
substring of this string.
In every turn add 7 chars of the string to the new string and skip the 8th element of the string: sb.append(str.substring(start, start+7));
In first turn:
str.substring(0, 7) -> "1100110"
start += 8; -> start = 8;
In second turn:
str.substring(8, 15) -> "0101101"
start += 8; -> start = 23;
...
So the 8th element/the element has the index 7 ("0")
has been skipped.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
int length = str.length();
int start = 0;
StringBuilder sb = new StringBuilder();
while((start+7)<length) {
sb.append(str.substring(start, start+7));
start += 8;
}
if(start<length) {
sb.append(str.substring(start, length));
}
System.out.println(sb + " " + sb.length());
System.out.println(str + " " + str.length());
Output:
11001100101101000000000000000000000000000000000000000000 56
1100110001011011000000000000000000000000000000000000000000000000 64
add a comment |
String
doesn't have a deleteCharAt()
method. If it did, it would return the update string, since String
is immutablem so code would have had to be str = str.deleteCharAt(i);
.
You could use StringBuilder
instead, since it does have a deleteCharAt()
method.
To delete every 8th character, start at the end. That way index values are unaffected by already deleted characters, which is your current problem.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
StringBuilder buf = new StringBuilder(str);
for (int i = (buf.length() - 1) / 8 * 8; i >= 0; i -= 8)
buf.deleteCharAt(i);
str = buf.toString();
System.out.println(str + " " + str.length());
Output
1100110001011011000000000000000000000000000000000000000000000000 64
10011001011011000000000000000000000000000000000000000000 56
UPDATE
The above code deletes the 1st, 9th, 17th, ... character, i.e. characters at index 0, 8, 16, ..., which is in accordance with "remove every 8th char" and "check if i % 8 == 0" mentioned in the question.
If code should delete the 8th, 16th, 24th, ... character, i.e. characters at index 7, 15, 23, ..., then change initialization of i
as follows:
for (int i = (buf.length() - 8) & ~7 | 7; i >= 0; i -= 8)
buf.deleteCharAt(i);
Output
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
1
You removed first character but OP wants to remove every 8th character.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:43
@PushpeshKumarRajwanshi I am removing "every 8th" character, starting with the 1st character, which is what the checki % 8 == 0
does. OP didn't specify that it should start with 8th character, i.e.i % 8 == 7
.
– Andreas
Nov 19 '18 at 17:48
OP hasn't mentioned anywhere that he wants to remove starting from the very first character in his post. Every 8th character means, you skip the first 7 character then remove the next character, and continue like that. In your output string, you have clearly removed the very first digit, which I think you should correct. Andi % 8 == 0
selects the very first character which is not what OP wants.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:53
add a comment |
Since StringBuilder::deleteCharAt
changes the size of the underlying sequence, you need to process the target string in reverse order.
This solution is based on streams.
// create target string
String s = Stream.generate(() -> IntStream.range(0, 10))
.limit(10)
.map(stream -> stream.mapToObj(Objects::toString).collect(Collectors.joining()))
.collect(Collectors.joining());
StringBuilder sb = new StringBuilder(s);
// delete first element or not?
boolean removeFirst = false;
IntStream.range(removeFirst ? 0 : 1, s.length())
.boxed()
.sorted(Collections.reverseOrder()) // reverse number stream
.filter(i -> i % 8 == 0) // only keep multiples of 8
.forEach(sb::deleteCharAt);
System.out.println(s);
System.out.println(sb.toString());
This is the output it produces
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456790123457890123567890134567891234567901234578901235678901345678912345679012345789
The first element missing is the 8, then 6 (16), then the 4 (24), etc.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379398%2fremove-every-8th-char-from-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
The main problem with your code is that you don't adjust i
when removing characters.
Let's visualize that. You want to remove the following marked characters ("every 8th element"):
1100110001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Now we're at i = 7
and remove that character, but because you don't adjust i
accordingly the markers keep the same:
110011001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Let's do that for 1 = 15
to i = 55
:
11001100101101100000000000000000000000000000000000000000000000 //i = 15
1100110010110110000000000000000000000000000000000000000000000 //i = 23
110011001011011000000000000000000000000000000000000000000000 //i = 31
11001100101101100000000000000000000000000000000000000000000 //i = 39
1100110010110110000000000000000000000000000000000000000000 //i = 47
110011001011011000000000000000000000000000000000000000000 //i = 55
^ ^ ^ ^ ^ ^ ^ ^
As you can see, all but the last marker point to a valid character but you won't reach i = 63
because after the first time you remove a character there only are 63 left in the string and thus a max index of 62.
That's why your resulting string has 57 instead of 56 characters, the last "remove" operation doesn't run (and the others except the first remove the wrong elements).
To fix that iterate backwards, i.e. from i = str.length() - 1
to i = 0
. Then you can remove every element where (i + 1) % 8 == 0
.
Alternatively, as I said in my comment, use a regex: String shortened = str.replaceAll( "(.{7}).", "$1" );
This will match any sequence of 7 characters followed by another (8th) character and replaces that with the first group of 7 (thus skipping the 8th).
I like your regex. Elegant solution!
– mike
Nov 26 '18 at 13:12
add a comment |
The main problem with your code is that you don't adjust i
when removing characters.
Let's visualize that. You want to remove the following marked characters ("every 8th element"):
1100110001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Now we're at i = 7
and remove that character, but because you don't adjust i
accordingly the markers keep the same:
110011001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Let's do that for 1 = 15
to i = 55
:
11001100101101100000000000000000000000000000000000000000000000 //i = 15
1100110010110110000000000000000000000000000000000000000000000 //i = 23
110011001011011000000000000000000000000000000000000000000000 //i = 31
11001100101101100000000000000000000000000000000000000000000 //i = 39
1100110010110110000000000000000000000000000000000000000000 //i = 47
110011001011011000000000000000000000000000000000000000000 //i = 55
^ ^ ^ ^ ^ ^ ^ ^
As you can see, all but the last marker point to a valid character but you won't reach i = 63
because after the first time you remove a character there only are 63 left in the string and thus a max index of 62.
That's why your resulting string has 57 instead of 56 characters, the last "remove" operation doesn't run (and the others except the first remove the wrong elements).
To fix that iterate backwards, i.e. from i = str.length() - 1
to i = 0
. Then you can remove every element where (i + 1) % 8 == 0
.
Alternatively, as I said in my comment, use a regex: String shortened = str.replaceAll( "(.{7}).", "$1" );
This will match any sequence of 7 characters followed by another (8th) character and replaces that with the first group of 7 (thus skipping the 8th).
I like your regex. Elegant solution!
– mike
Nov 26 '18 at 13:12
add a comment |
The main problem with your code is that you don't adjust i
when removing characters.
Let's visualize that. You want to remove the following marked characters ("every 8th element"):
1100110001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Now we're at i = 7
and remove that character, but because you don't adjust i
accordingly the markers keep the same:
110011001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Let's do that for 1 = 15
to i = 55
:
11001100101101100000000000000000000000000000000000000000000000 //i = 15
1100110010110110000000000000000000000000000000000000000000000 //i = 23
110011001011011000000000000000000000000000000000000000000000 //i = 31
11001100101101100000000000000000000000000000000000000000000 //i = 39
1100110010110110000000000000000000000000000000000000000000 //i = 47
110011001011011000000000000000000000000000000000000000000 //i = 55
^ ^ ^ ^ ^ ^ ^ ^
As you can see, all but the last marker point to a valid character but you won't reach i = 63
because after the first time you remove a character there only are 63 left in the string and thus a max index of 62.
That's why your resulting string has 57 instead of 56 characters, the last "remove" operation doesn't run (and the others except the first remove the wrong elements).
To fix that iterate backwards, i.e. from i = str.length() - 1
to i = 0
. Then you can remove every element where (i + 1) % 8 == 0
.
Alternatively, as I said in my comment, use a regex: String shortened = str.replaceAll( "(.{7}).", "$1" );
This will match any sequence of 7 characters followed by another (8th) character and replaces that with the first group of 7 (thus skipping the 8th).
The main problem with your code is that you don't adjust i
when removing characters.
Let's visualize that. You want to remove the following marked characters ("every 8th element"):
1100110001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Now we're at i = 7
and remove that character, but because you don't adjust i
accordingly the markers keep the same:
110011001011011000000000000000000000000000000000000000000000000
^ ^ ^ ^ ^ ^ ^ ^
Let's do that for 1 = 15
to i = 55
:
11001100101101100000000000000000000000000000000000000000000000 //i = 15
1100110010110110000000000000000000000000000000000000000000000 //i = 23
110011001011011000000000000000000000000000000000000000000000 //i = 31
11001100101101100000000000000000000000000000000000000000000 //i = 39
1100110010110110000000000000000000000000000000000000000000 //i = 47
110011001011011000000000000000000000000000000000000000000 //i = 55
^ ^ ^ ^ ^ ^ ^ ^
As you can see, all but the last marker point to a valid character but you won't reach i = 63
because after the first time you remove a character there only are 63 left in the string and thus a max index of 62.
That's why your resulting string has 57 instead of 56 characters, the last "remove" operation doesn't run (and the others except the first remove the wrong elements).
To fix that iterate backwards, i.e. from i = str.length() - 1
to i = 0
. Then you can remove every element where (i + 1) % 8 == 0
.
Alternatively, as I said in my comment, use a regex: String shortened = str.replaceAll( "(.{7}).", "$1" );
This will match any sequence of 7 characters followed by another (8th) character and replaces that with the first group of 7 (thus skipping the 8th).
edited Nov 19 '18 at 17:31
answered Nov 19 '18 at 17:22
Thomas
69.1k989124
69.1k989124
I like your regex. Elegant solution!
– mike
Nov 26 '18 at 13:12
add a comment |
I like your regex. Elegant solution!
– mike
Nov 26 '18 at 13:12
I like your regex. Elegant solution!
– mike
Nov 26 '18 at 13:12
I like your regex. Elegant solution!
– mike
Nov 26 '18 at 13:12
add a comment |
There is not deleteCharAt
method in String
, so I suppose you meant StringBuilder
?
You can just reverse the direction of the for loop, so that it starts from the end of the string:
String str = "11111111811111118";
StringBuilder builder = new StringBuilder(str);
System.out.println(str + " " + str.length());
for (int i = str.length() - 1; i >= 0; i--) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
builder.deleteCharAt(i);
}
}
System.out.println(builder+ " " + builder.length());
By deleting chars from the end of the string, the indices of the chars to be removed no longer changes as you move along the string.
Using original string:StringIndexOutOfBoundsException: index 64,length 64
– Andreas
Nov 19 '18 at 17:08
@Andreas Oops! Fixed.
– Sweeper
Nov 19 '18 at 17:09
add a comment |
There is not deleteCharAt
method in String
, so I suppose you meant StringBuilder
?
You can just reverse the direction of the for loop, so that it starts from the end of the string:
String str = "11111111811111118";
StringBuilder builder = new StringBuilder(str);
System.out.println(str + " " + str.length());
for (int i = str.length() - 1; i >= 0; i--) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
builder.deleteCharAt(i);
}
}
System.out.println(builder+ " " + builder.length());
By deleting chars from the end of the string, the indices of the chars to be removed no longer changes as you move along the string.
Using original string:StringIndexOutOfBoundsException: index 64,length 64
– Andreas
Nov 19 '18 at 17:08
@Andreas Oops! Fixed.
– Sweeper
Nov 19 '18 at 17:09
add a comment |
There is not deleteCharAt
method in String
, so I suppose you meant StringBuilder
?
You can just reverse the direction of the for loop, so that it starts from the end of the string:
String str = "11111111811111118";
StringBuilder builder = new StringBuilder(str);
System.out.println(str + " " + str.length());
for (int i = str.length() - 1; i >= 0; i--) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
builder.deleteCharAt(i);
}
}
System.out.println(builder+ " " + builder.length());
By deleting chars from the end of the string, the indices of the chars to be removed no longer changes as you move along the string.
There is not deleteCharAt
method in String
, so I suppose you meant StringBuilder
?
You can just reverse the direction of the for loop, so that it starts from the end of the string:
String str = "11111111811111118";
StringBuilder builder = new StringBuilder(str);
System.out.println(str + " " + str.length());
for (int i = str.length() - 1; i >= 0; i--) {
// Every 8th element should be discarded
if (i > 7 && i % 8 == 0) {
builder.deleteCharAt(i);
}
}
System.out.println(builder+ " " + builder.length());
By deleting chars from the end of the string, the indices of the chars to be removed no longer changes as you move along the string.
edited Nov 19 '18 at 17:08
answered Nov 19 '18 at 17:05


Sweeper
64.4k1071139
64.4k1071139
Using original string:StringIndexOutOfBoundsException: index 64,length 64
– Andreas
Nov 19 '18 at 17:08
@Andreas Oops! Fixed.
– Sweeper
Nov 19 '18 at 17:09
add a comment |
Using original string:StringIndexOutOfBoundsException: index 64,length 64
– Andreas
Nov 19 '18 at 17:08
@Andreas Oops! Fixed.
– Sweeper
Nov 19 '18 at 17:09
Using original string:
StringIndexOutOfBoundsException: index 64,length 64
– Andreas
Nov 19 '18 at 17:08
Using original string:
StringIndexOutOfBoundsException: index 64,length 64
– Andreas
Nov 19 '18 at 17:08
@Andreas Oops! Fixed.
– Sweeper
Nov 19 '18 at 17:09
@Andreas Oops! Fixed.
– Sweeper
Nov 19 '18 at 17:09
add a comment |
Why don't you use regex and achieve it in two lines of code like this,
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
String replacedStr = str.replaceAll("([01]{7})[01]", "$1");
System.out.println(str.toString() + " " + str.length());
System.out.println(replacedStr.toString() + " " + replacedStr.length());
}
This gives perfectly correct output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
Alternatively, you can follow this traditional solution like you attempted.
Strings in java are immutable. So instead you should create a StringBuilder object and keep copying every character, except 8th character.
For correctly counting every 8th character, initialize your for loop index run from 1 rather than 0, like in this code, which will eradicate every 8th character effectively where you wanted to do if (i%8==0)
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder sb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 1; i <= str.length(); i++) {
// Every 8th element should be discarded
if (i % 8 == 0) {
// str.deleteCharAt(i);
} else {
sb.append(str.charAt(i-1));
}
}
System.out.println(sb.toString() + " " + sb.length());
}
And this gives following output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
You can verify here where only every 8th character is gone in this output.
But the second output should have a length of 56
– Bab
Nov 19 '18 at 17:07
add a comment |
Why don't you use regex and achieve it in two lines of code like this,
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
String replacedStr = str.replaceAll("([01]{7})[01]", "$1");
System.out.println(str.toString() + " " + str.length());
System.out.println(replacedStr.toString() + " " + replacedStr.length());
}
This gives perfectly correct output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
Alternatively, you can follow this traditional solution like you attempted.
Strings in java are immutable. So instead you should create a StringBuilder object and keep copying every character, except 8th character.
For correctly counting every 8th character, initialize your for loop index run from 1 rather than 0, like in this code, which will eradicate every 8th character effectively where you wanted to do if (i%8==0)
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder sb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 1; i <= str.length(); i++) {
// Every 8th element should be discarded
if (i % 8 == 0) {
// str.deleteCharAt(i);
} else {
sb.append(str.charAt(i-1));
}
}
System.out.println(sb.toString() + " " + sb.length());
}
And this gives following output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
You can verify here where only every 8th character is gone in this output.
But the second output should have a length of 56
– Bab
Nov 19 '18 at 17:07
add a comment |
Why don't you use regex and achieve it in two lines of code like this,
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
String replacedStr = str.replaceAll("([01]{7})[01]", "$1");
System.out.println(str.toString() + " " + str.length());
System.out.println(replacedStr.toString() + " " + replacedStr.length());
}
This gives perfectly correct output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
Alternatively, you can follow this traditional solution like you attempted.
Strings in java are immutable. So instead you should create a StringBuilder object and keep copying every character, except 8th character.
For correctly counting every 8th character, initialize your for loop index run from 1 rather than 0, like in this code, which will eradicate every 8th character effectively where you wanted to do if (i%8==0)
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder sb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 1; i <= str.length(); i++) {
// Every 8th element should be discarded
if (i % 8 == 0) {
// str.deleteCharAt(i);
} else {
sb.append(str.charAt(i-1));
}
}
System.out.println(sb.toString() + " " + sb.length());
}
And this gives following output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
You can verify here where only every 8th character is gone in this output.
Why don't you use regex and achieve it in two lines of code like this,
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
String replacedStr = str.replaceAll("([01]{7})[01]", "$1");
System.out.println(str.toString() + " " + str.length());
System.out.println(replacedStr.toString() + " " + replacedStr.length());
}
This gives perfectly correct output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
Alternatively, you can follow this traditional solution like you attempted.
Strings in java are immutable. So instead you should create a StringBuilder object and keep copying every character, except 8th character.
For correctly counting every 8th character, initialize your for loop index run from 1 rather than 0, like in this code, which will eradicate every 8th character effectively where you wanted to do if (i%8==0)
public static void main(String args) {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder sb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 1; i <= str.length(); i++) {
// Every 8th element should be discarded
if (i % 8 == 0) {
// str.deleteCharAt(i);
} else {
sb.append(str.charAt(i-1));
}
}
System.out.println(sb.toString() + " " + sb.length());
}
And this gives following output,
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
You can verify here where only every 8th character is gone in this output.
edited Nov 19 '18 at 17:43
answered Nov 19 '18 at 17:06
Pushpesh Kumar Rajwanshi
5,4222827
5,4222827
But the second output should have a length of 56
– Bab
Nov 19 '18 at 17:07
add a comment |
But the second output should have a length of 56
– Bab
Nov 19 '18 at 17:07
But the second output should have a length of 56
– Bab
Nov 19 '18 at 17:07
But the second output should have a length of 56
– Bab
Nov 19 '18 at 17:07
add a comment |
The problem is that Strings are starting with 0. Therefore the 8th element has the index 7 and has to be removed as well, which you don't do in your loop. I'd write it like that (but noting that this might not be the most elegant solution):
public static void main(String args)
{
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
int idx = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
idx++;
if (idx == 8) {
idx = 0;
continue;
}
sb.append(str.charAt(i));
}
System.out.println(sb.toString() + " " + sb.length());
}
Outputs:
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
add a comment |
The problem is that Strings are starting with 0. Therefore the 8th element has the index 7 and has to be removed as well, which you don't do in your loop. I'd write it like that (but noting that this might not be the most elegant solution):
public static void main(String args)
{
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
int idx = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
idx++;
if (idx == 8) {
idx = 0;
continue;
}
sb.append(str.charAt(i));
}
System.out.println(sb.toString() + " " + sb.length());
}
Outputs:
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
add a comment |
The problem is that Strings are starting with 0. Therefore the 8th element has the index 7 and has to be removed as well, which you don't do in your loop. I'd write it like that (but noting that this might not be the most elegant solution):
public static void main(String args)
{
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
int idx = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
idx++;
if (idx == 8) {
idx = 0;
continue;
}
sb.append(str.charAt(i));
}
System.out.println(sb.toString() + " " + sb.length());
}
Outputs:
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
The problem is that Strings are starting with 0. Therefore the 8th element has the index 7 and has to be removed as well, which you don't do in your loop. I'd write it like that (but noting that this might not be the most elegant solution):
public static void main(String args)
{
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
int idx = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
idx++;
if (idx == 8) {
idx = 0;
continue;
}
sb.append(str.charAt(i));
}
System.out.println(sb.toString() + " " + sb.length());
}
Outputs:
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
answered Nov 19 '18 at 17:10


maio290
1,888414
1,888414
add a comment |
add a comment |
Assuming that the string does not contain the char with ASCII value 0, convert the string to a char array and change every 8th char with the char with ASCII value 0, then reconstruct the string and replace all chars with ASCII value 0 with "":
String str = "0123456701234567012345670123456701234567012345670123456701234567";
System.out.println("initial = " + str);
char array = str.toCharArray();
for (int i = 7; i < array.length; i = i + 8) {
array[i] = 0;
}
str = String.valueOf(array).replace(String.valueOf(Character.toChars(0)), "");
System.out.println("final = " + str);
will print:
initial = 0123456701234567012345670123456701234567012345670123456701234567
final = 01234560123456012345601234560123456012345601234560123456
add a comment |
Assuming that the string does not contain the char with ASCII value 0, convert the string to a char array and change every 8th char with the char with ASCII value 0, then reconstruct the string and replace all chars with ASCII value 0 with "":
String str = "0123456701234567012345670123456701234567012345670123456701234567";
System.out.println("initial = " + str);
char array = str.toCharArray();
for (int i = 7; i < array.length; i = i + 8) {
array[i] = 0;
}
str = String.valueOf(array).replace(String.valueOf(Character.toChars(0)), "");
System.out.println("final = " + str);
will print:
initial = 0123456701234567012345670123456701234567012345670123456701234567
final = 01234560123456012345601234560123456012345601234560123456
add a comment |
Assuming that the string does not contain the char with ASCII value 0, convert the string to a char array and change every 8th char with the char with ASCII value 0, then reconstruct the string and replace all chars with ASCII value 0 with "":
String str = "0123456701234567012345670123456701234567012345670123456701234567";
System.out.println("initial = " + str);
char array = str.toCharArray();
for (int i = 7; i < array.length; i = i + 8) {
array[i] = 0;
}
str = String.valueOf(array).replace(String.valueOf(Character.toChars(0)), "");
System.out.println("final = " + str);
will print:
initial = 0123456701234567012345670123456701234567012345670123456701234567
final = 01234560123456012345601234560123456012345601234560123456
Assuming that the string does not contain the char with ASCII value 0, convert the string to a char array and change every 8th char with the char with ASCII value 0, then reconstruct the string and replace all chars with ASCII value 0 with "":
String str = "0123456701234567012345670123456701234567012345670123456701234567";
System.out.println("initial = " + str);
char array = str.toCharArray();
for (int i = 7; i < array.length; i = i + 8) {
array[i] = 0;
}
str = String.valueOf(array).replace(String.valueOf(Character.toChars(0)), "");
System.out.println("final = " + str);
will print:
initial = 0123456701234567012345670123456701234567012345670123456701234567
final = 01234560123456012345601234560123456012345601234560123456
answered Nov 19 '18 at 17:32
forpas
9,1221421
9,1221421
add a comment |
add a comment |
Your code won't compile as there is no deleteCharAt()
of the String class. To solve the issue create a new StringBuilder to hold the new values and iterate over str
:
public static void main(String args) throws IOException {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder newSb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
if (i == 0 || ((i + 1) % 8 != 0)) {
newSb.append(str.charAt(i));
}
}
System.out.println(newSb+ " " + newSb.length());
}
Here we skip every 8th index and only append the remaining chars to n. This outputs :
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
add a comment |
Your code won't compile as there is no deleteCharAt()
of the String class. To solve the issue create a new StringBuilder to hold the new values and iterate over str
:
public static void main(String args) throws IOException {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder newSb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
if (i == 0 || ((i + 1) % 8 != 0)) {
newSb.append(str.charAt(i));
}
}
System.out.println(newSb+ " " + newSb.length());
}
Here we skip every 8th index and only append the remaining chars to n. This outputs :
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
add a comment |
Your code won't compile as there is no deleteCharAt()
of the String class. To solve the issue create a new StringBuilder to hold the new values and iterate over str
:
public static void main(String args) throws IOException {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder newSb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
if (i == 0 || ((i + 1) % 8 != 0)) {
newSb.append(str.charAt(i));
}
}
System.out.println(newSb+ " " + newSb.length());
}
Here we skip every 8th index and only append the remaining chars to n. This outputs :
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
Your code won't compile as there is no deleteCharAt()
of the String class. To solve the issue create a new StringBuilder to hold the new values and iterate over str
:
public static void main(String args) throws IOException {
String str = "1100110001011011000000000000000000000000000000000000000000000000";
StringBuilder newSb = new StringBuilder();
System.out.println(str + " " + str.length());
for (int i = 0; i < str.length(); i++) {
if (i == 0 || ((i + 1) % 8 != 0)) {
newSb.append(str.charAt(i));
}
}
System.out.println(newSb+ " " + newSb.length());
}
Here we skip every 8th index and only append the remaining chars to n. This outputs :
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
edited Nov 19 '18 at 17:38
answered Nov 19 '18 at 17:03


Nicholas K
5,81951031
5,81951031
add a comment |
add a comment |
An alternative way is using substring()
method.
substring(int beginIndex, int endIndex) Returns a new string that is a
substring of this string.
In every turn add 7 chars of the string to the new string and skip the 8th element of the string: sb.append(str.substring(start, start+7));
In first turn:
str.substring(0, 7) -> "1100110"
start += 8; -> start = 8;
In second turn:
str.substring(8, 15) -> "0101101"
start += 8; -> start = 23;
...
So the 8th element/the element has the index 7 ("0")
has been skipped.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
int length = str.length();
int start = 0;
StringBuilder sb = new StringBuilder();
while((start+7)<length) {
sb.append(str.substring(start, start+7));
start += 8;
}
if(start<length) {
sb.append(str.substring(start, length));
}
System.out.println(sb + " " + sb.length());
System.out.println(str + " " + str.length());
Output:
11001100101101000000000000000000000000000000000000000000 56
1100110001011011000000000000000000000000000000000000000000000000 64
add a comment |
An alternative way is using substring()
method.
substring(int beginIndex, int endIndex) Returns a new string that is a
substring of this string.
In every turn add 7 chars of the string to the new string and skip the 8th element of the string: sb.append(str.substring(start, start+7));
In first turn:
str.substring(0, 7) -> "1100110"
start += 8; -> start = 8;
In second turn:
str.substring(8, 15) -> "0101101"
start += 8; -> start = 23;
...
So the 8th element/the element has the index 7 ("0")
has been skipped.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
int length = str.length();
int start = 0;
StringBuilder sb = new StringBuilder();
while((start+7)<length) {
sb.append(str.substring(start, start+7));
start += 8;
}
if(start<length) {
sb.append(str.substring(start, length));
}
System.out.println(sb + " " + sb.length());
System.out.println(str + " " + str.length());
Output:
11001100101101000000000000000000000000000000000000000000 56
1100110001011011000000000000000000000000000000000000000000000000 64
add a comment |
An alternative way is using substring()
method.
substring(int beginIndex, int endIndex) Returns a new string that is a
substring of this string.
In every turn add 7 chars of the string to the new string and skip the 8th element of the string: sb.append(str.substring(start, start+7));
In first turn:
str.substring(0, 7) -> "1100110"
start += 8; -> start = 8;
In second turn:
str.substring(8, 15) -> "0101101"
start += 8; -> start = 23;
...
So the 8th element/the element has the index 7 ("0")
has been skipped.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
int length = str.length();
int start = 0;
StringBuilder sb = new StringBuilder();
while((start+7)<length) {
sb.append(str.substring(start, start+7));
start += 8;
}
if(start<length) {
sb.append(str.substring(start, length));
}
System.out.println(sb + " " + sb.length());
System.out.println(str + " " + str.length());
Output:
11001100101101000000000000000000000000000000000000000000 56
1100110001011011000000000000000000000000000000000000000000000000 64
An alternative way is using substring()
method.
substring(int beginIndex, int endIndex) Returns a new string that is a
substring of this string.
In every turn add 7 chars of the string to the new string and skip the 8th element of the string: sb.append(str.substring(start, start+7));
In first turn:
str.substring(0, 7) -> "1100110"
start += 8; -> start = 8;
In second turn:
str.substring(8, 15) -> "0101101"
start += 8; -> start = 23;
...
So the 8th element/the element has the index 7 ("0")
has been skipped.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
int length = str.length();
int start = 0;
StringBuilder sb = new StringBuilder();
while((start+7)<length) {
sb.append(str.substring(start, start+7));
start += 8;
}
if(start<length) {
sb.append(str.substring(start, length));
}
System.out.println(sb + " " + sb.length());
System.out.println(str + " " + str.length());
Output:
11001100101101000000000000000000000000000000000000000000 56
1100110001011011000000000000000000000000000000000000000000000000 64
edited Nov 19 '18 at 17:52
answered Nov 19 '18 at 17:27


Hülya
464119
464119
add a comment |
add a comment |
String
doesn't have a deleteCharAt()
method. If it did, it would return the update string, since String
is immutablem so code would have had to be str = str.deleteCharAt(i);
.
You could use StringBuilder
instead, since it does have a deleteCharAt()
method.
To delete every 8th character, start at the end. That way index values are unaffected by already deleted characters, which is your current problem.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
StringBuilder buf = new StringBuilder(str);
for (int i = (buf.length() - 1) / 8 * 8; i >= 0; i -= 8)
buf.deleteCharAt(i);
str = buf.toString();
System.out.println(str + " " + str.length());
Output
1100110001011011000000000000000000000000000000000000000000000000 64
10011001011011000000000000000000000000000000000000000000 56
UPDATE
The above code deletes the 1st, 9th, 17th, ... character, i.e. characters at index 0, 8, 16, ..., which is in accordance with "remove every 8th char" and "check if i % 8 == 0" mentioned in the question.
If code should delete the 8th, 16th, 24th, ... character, i.e. characters at index 7, 15, 23, ..., then change initialization of i
as follows:
for (int i = (buf.length() - 8) & ~7 | 7; i >= 0; i -= 8)
buf.deleteCharAt(i);
Output
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
1
You removed first character but OP wants to remove every 8th character.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:43
@PushpeshKumarRajwanshi I am removing "every 8th" character, starting with the 1st character, which is what the checki % 8 == 0
does. OP didn't specify that it should start with 8th character, i.e.i % 8 == 7
.
– Andreas
Nov 19 '18 at 17:48
OP hasn't mentioned anywhere that he wants to remove starting from the very first character in his post. Every 8th character means, you skip the first 7 character then remove the next character, and continue like that. In your output string, you have clearly removed the very first digit, which I think you should correct. Andi % 8 == 0
selects the very first character which is not what OP wants.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:53
add a comment |
String
doesn't have a deleteCharAt()
method. If it did, it would return the update string, since String
is immutablem so code would have had to be str = str.deleteCharAt(i);
.
You could use StringBuilder
instead, since it does have a deleteCharAt()
method.
To delete every 8th character, start at the end. That way index values are unaffected by already deleted characters, which is your current problem.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
StringBuilder buf = new StringBuilder(str);
for (int i = (buf.length() - 1) / 8 * 8; i >= 0; i -= 8)
buf.deleteCharAt(i);
str = buf.toString();
System.out.println(str + " " + str.length());
Output
1100110001011011000000000000000000000000000000000000000000000000 64
10011001011011000000000000000000000000000000000000000000 56
UPDATE
The above code deletes the 1st, 9th, 17th, ... character, i.e. characters at index 0, 8, 16, ..., which is in accordance with "remove every 8th char" and "check if i % 8 == 0" mentioned in the question.
If code should delete the 8th, 16th, 24th, ... character, i.e. characters at index 7, 15, 23, ..., then change initialization of i
as follows:
for (int i = (buf.length() - 8) & ~7 | 7; i >= 0; i -= 8)
buf.deleteCharAt(i);
Output
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
1
You removed first character but OP wants to remove every 8th character.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:43
@PushpeshKumarRajwanshi I am removing "every 8th" character, starting with the 1st character, which is what the checki % 8 == 0
does. OP didn't specify that it should start with 8th character, i.e.i % 8 == 7
.
– Andreas
Nov 19 '18 at 17:48
OP hasn't mentioned anywhere that he wants to remove starting from the very first character in his post. Every 8th character means, you skip the first 7 character then remove the next character, and continue like that. In your output string, you have clearly removed the very first digit, which I think you should correct. Andi % 8 == 0
selects the very first character which is not what OP wants.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:53
add a comment |
String
doesn't have a deleteCharAt()
method. If it did, it would return the update string, since String
is immutablem so code would have had to be str = str.deleteCharAt(i);
.
You could use StringBuilder
instead, since it does have a deleteCharAt()
method.
To delete every 8th character, start at the end. That way index values are unaffected by already deleted characters, which is your current problem.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
StringBuilder buf = new StringBuilder(str);
for (int i = (buf.length() - 1) / 8 * 8; i >= 0; i -= 8)
buf.deleteCharAt(i);
str = buf.toString();
System.out.println(str + " " + str.length());
Output
1100110001011011000000000000000000000000000000000000000000000000 64
10011001011011000000000000000000000000000000000000000000 56
UPDATE
The above code deletes the 1st, 9th, 17th, ... character, i.e. characters at index 0, 8, 16, ..., which is in accordance with "remove every 8th char" and "check if i % 8 == 0" mentioned in the question.
If code should delete the 8th, 16th, 24th, ... character, i.e. characters at index 7, 15, 23, ..., then change initialization of i
as follows:
for (int i = (buf.length() - 8) & ~7 | 7; i >= 0; i -= 8)
buf.deleteCharAt(i);
Output
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
String
doesn't have a deleteCharAt()
method. If it did, it would return the update string, since String
is immutablem so code would have had to be str = str.deleteCharAt(i);
.
You could use StringBuilder
instead, since it does have a deleteCharAt()
method.
To delete every 8th character, start at the end. That way index values are unaffected by already deleted characters, which is your current problem.
String str = "1100110001011011000000000000000000000000000000000000000000000000";
System.out.println(str + " " + str.length());
StringBuilder buf = new StringBuilder(str);
for (int i = (buf.length() - 1) / 8 * 8; i >= 0; i -= 8)
buf.deleteCharAt(i);
str = buf.toString();
System.out.println(str + " " + str.length());
Output
1100110001011011000000000000000000000000000000000000000000000000 64
10011001011011000000000000000000000000000000000000000000 56
UPDATE
The above code deletes the 1st, 9th, 17th, ... character, i.e. characters at index 0, 8, 16, ..., which is in accordance with "remove every 8th char" and "check if i % 8 == 0" mentioned in the question.
If code should delete the 8th, 16th, 24th, ... character, i.e. characters at index 7, 15, 23, ..., then change initialization of i
as follows:
for (int i = (buf.length() - 8) & ~7 | 7; i >= 0; i -= 8)
buf.deleteCharAt(i);
Output
1100110001011011000000000000000000000000000000000000000000000000 64
11001100101101000000000000000000000000000000000000000000 56
edited Nov 19 '18 at 17:57
answered Nov 19 '18 at 17:07


Andreas
75.4k460122
75.4k460122
1
You removed first character but OP wants to remove every 8th character.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:43
@PushpeshKumarRajwanshi I am removing "every 8th" character, starting with the 1st character, which is what the checki % 8 == 0
does. OP didn't specify that it should start with 8th character, i.e.i % 8 == 7
.
– Andreas
Nov 19 '18 at 17:48
OP hasn't mentioned anywhere that he wants to remove starting from the very first character in his post. Every 8th character means, you skip the first 7 character then remove the next character, and continue like that. In your output string, you have clearly removed the very first digit, which I think you should correct. Andi % 8 == 0
selects the very first character which is not what OP wants.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:53
add a comment |
1
You removed first character but OP wants to remove every 8th character.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:43
@PushpeshKumarRajwanshi I am removing "every 8th" character, starting with the 1st character, which is what the checki % 8 == 0
does. OP didn't specify that it should start with 8th character, i.e.i % 8 == 7
.
– Andreas
Nov 19 '18 at 17:48
OP hasn't mentioned anywhere that he wants to remove starting from the very first character in his post. Every 8th character means, you skip the first 7 character then remove the next character, and continue like that. In your output string, you have clearly removed the very first digit, which I think you should correct. Andi % 8 == 0
selects the very first character which is not what OP wants.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:53
1
1
You removed first character but OP wants to remove every 8th character.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:43
You removed first character but OP wants to remove every 8th character.
– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:43
@PushpeshKumarRajwanshi I am removing "every 8th" character, starting with the 1st character, which is what the check
i % 8 == 0
does. OP didn't specify that it should start with 8th character, i.e. i % 8 == 7
.– Andreas
Nov 19 '18 at 17:48
@PushpeshKumarRajwanshi I am removing "every 8th" character, starting with the 1st character, which is what the check
i % 8 == 0
does. OP didn't specify that it should start with 8th character, i.e. i % 8 == 7
.– Andreas
Nov 19 '18 at 17:48
OP hasn't mentioned anywhere that he wants to remove starting from the very first character in his post. Every 8th character means, you skip the first 7 character then remove the next character, and continue like that. In your output string, you have clearly removed the very first digit, which I think you should correct. And
i % 8 == 0
selects the very first character which is not what OP wants.– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:53
OP hasn't mentioned anywhere that he wants to remove starting from the very first character in his post. Every 8th character means, you skip the first 7 character then remove the next character, and continue like that. In your output string, you have clearly removed the very first digit, which I think you should correct. And
i % 8 == 0
selects the very first character which is not what OP wants.– Pushpesh Kumar Rajwanshi
Nov 19 '18 at 17:53
add a comment |
Since StringBuilder::deleteCharAt
changes the size of the underlying sequence, you need to process the target string in reverse order.
This solution is based on streams.
// create target string
String s = Stream.generate(() -> IntStream.range(0, 10))
.limit(10)
.map(stream -> stream.mapToObj(Objects::toString).collect(Collectors.joining()))
.collect(Collectors.joining());
StringBuilder sb = new StringBuilder(s);
// delete first element or not?
boolean removeFirst = false;
IntStream.range(removeFirst ? 0 : 1, s.length())
.boxed()
.sorted(Collections.reverseOrder()) // reverse number stream
.filter(i -> i % 8 == 0) // only keep multiples of 8
.forEach(sb::deleteCharAt);
System.out.println(s);
System.out.println(sb.toString());
This is the output it produces
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456790123457890123567890134567891234567901234578901235678901345678912345679012345789
The first element missing is the 8, then 6 (16), then the 4 (24), etc.
add a comment |
Since StringBuilder::deleteCharAt
changes the size of the underlying sequence, you need to process the target string in reverse order.
This solution is based on streams.
// create target string
String s = Stream.generate(() -> IntStream.range(0, 10))
.limit(10)
.map(stream -> stream.mapToObj(Objects::toString).collect(Collectors.joining()))
.collect(Collectors.joining());
StringBuilder sb = new StringBuilder(s);
// delete first element or not?
boolean removeFirst = false;
IntStream.range(removeFirst ? 0 : 1, s.length())
.boxed()
.sorted(Collections.reverseOrder()) // reverse number stream
.filter(i -> i % 8 == 0) // only keep multiples of 8
.forEach(sb::deleteCharAt);
System.out.println(s);
System.out.println(sb.toString());
This is the output it produces
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456790123457890123567890134567891234567901234578901235678901345678912345679012345789
The first element missing is the 8, then 6 (16), then the 4 (24), etc.
add a comment |
Since StringBuilder::deleteCharAt
changes the size of the underlying sequence, you need to process the target string in reverse order.
This solution is based on streams.
// create target string
String s = Stream.generate(() -> IntStream.range(0, 10))
.limit(10)
.map(stream -> stream.mapToObj(Objects::toString).collect(Collectors.joining()))
.collect(Collectors.joining());
StringBuilder sb = new StringBuilder(s);
// delete first element or not?
boolean removeFirst = false;
IntStream.range(removeFirst ? 0 : 1, s.length())
.boxed()
.sorted(Collections.reverseOrder()) // reverse number stream
.filter(i -> i % 8 == 0) // only keep multiples of 8
.forEach(sb::deleteCharAt);
System.out.println(s);
System.out.println(sb.toString());
This is the output it produces
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456790123457890123567890134567891234567901234578901235678901345678912345679012345789
The first element missing is the 8, then 6 (16), then the 4 (24), etc.
Since StringBuilder::deleteCharAt
changes the size of the underlying sequence, you need to process the target string in reverse order.
This solution is based on streams.
// create target string
String s = Stream.generate(() -> IntStream.range(0, 10))
.limit(10)
.map(stream -> stream.mapToObj(Objects::toString).collect(Collectors.joining()))
.collect(Collectors.joining());
StringBuilder sb = new StringBuilder(s);
// delete first element or not?
boolean removeFirst = false;
IntStream.range(removeFirst ? 0 : 1, s.length())
.boxed()
.sorted(Collections.reverseOrder()) // reverse number stream
.filter(i -> i % 8 == 0) // only keep multiples of 8
.forEach(sb::deleteCharAt);
System.out.println(s);
System.out.println(sb.toString());
This is the output it produces
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456790123457890123567890134567891234567901234578901235678901345678912345679012345789
The first element missing is the 8, then 6 (16), then the 4 (24), etc.
edited Nov 19 '18 at 18:06
answered Nov 19 '18 at 17:58


mike
3,16032553
3,16032553
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379398%2fremove-every-8th-char-from-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can't modify the size of an array while iterating through it. Make a copy, modify the copy, and when you're done you can forget the original
– emsimpson92
Nov 19 '18 at 17:01
2
Why not create a new string where you add the chars if they are mod 0 through 7, and ignore the 8th?
– Compass
Nov 19 '18 at 17:01
Note that deleting elements from an array would mean you'd have to shift the other elements left by one place and handle the indices accordingly (e.g. index 15 would become 14 when you remove the element at index 7 etc.). Because of that it's often easier to either make a copy and just skip elements or if it needs to be inplace do it back-to-front.
– Thomas
Nov 19 '18 at 17:04
2
You could also use a regex:
String shortened = str.replaceAll( "(.{7}).", "$1" );
This will replace each sequence of 8 characters with the first 7 thus effectively skipping every 8th.– Thomas
Nov 19 '18 at 17:06
Why are you using a String of 64 binary digits when you could just use a
long
?– DodgyCodeException
Nov 19 '18 at 17:48