简体   繁体   中英

How to delete every 12th character from a string in java

Lets say I have a string like this:

String str = "~asdfl;kjx,~rgadfaeg,dsafnewgfljka;ldfjsfa;dlkjfa;lvjvbnaber;fwelfjadfafa"

int character = 12

What I want to do is delete every 12th character in the string, so i would delete the 12 index, then the 24th, then the 36th, etc until the string is over.

Which index I delete (every 12th, or every 2nd) has to equal the character variable I have, since that variable changes.

I tried doing this with regex:

System.out.println(s.replaceAll(".(.)", "$12"));

But it didnt work. any help?

Sometimes, a simple for loop is all you need:

public class Test {
    public static void main(String[] args) {
        String str = "~asdfl;kjx,~rgadfaeg,dsafnewgfljka;ldfjsfa;dlkjfa;lvjvbnaber;fwelfjadfafa";
        int character = 12;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if ((i + 1) % character != 0) {
                sb.append(str.charAt(i));
            }
        }
        String result = sb.toString();

        System.out.println(result);
    }
}

If you insist on using regular expressions, you can interpolate the character variable into the expression as follows:

public class Test {
    public static void main(String[] args) {
        String str = "~asdfl;kjx,~rgadfaeg,dsafnewgfljka;ldfjsfa;dlkjfa;lvjvbnaber;fwelfjadfafa";
        int character = 12;

        System.out.println(str.replaceAll("(.{" + (character - 1) + "}).", "$1"));
    }
}

To delete every 12th character using regex, use this pattern:

(.{11}).

And then replace with just the captured $1 .

Sample Java code:

String str = "~asdfl;kjx,~rgadfaeg,dsafnewgfljka;ldfjsfa;dlkjfa;lvjvbnaber;fwelfjadfafa";
String output = str.replaceAll("(.{11}).", "$1");
System.out.println(output);

This prints:

~asdfl;kjx,rgadfaeg,dsfnewgfljka;dfjsfa;dlkja;lvjvbnabe;fwelfjadfaa

Edit:

To do a regex replacement of some fixed width, use:

String str = "~asdfl;kjx,~rgadfaeg,dsafnewgfljka;ldfjsfa;dlkjfa;lvjvbnaber;fwelfjadfafa";
int width = 11;
String output = str.replaceAll("(.{" + width + "}).", "$1");
System.out.println(output);

Avoid char

The char type in Java is legacy, essentially broken. As a 16-bit value, a char is incapable of representing most characters.

Code points

Instead, use code point integers.

Make an array of each character's code point.

int[] codePointsArray = input.codePoints().toArray() ;

Make a list of that array.

List< Integer > codePoints = List.of( codePointsArray ) ;

Alternatively:

List< Integer > codePoints = input.codePoints().boxed().toList() ;

Make an IntStream of the indexes we need to access each element of that list. Use each index to pull out a code point, and filter by the nth element. Collect into a StringBuilder .

String result =
    IntStream
    .range( 0 , codePoints.size() )
    .filter( n -> n % 12 != 0 )
    .mapToObj( codePoints :: get )
    .collect( StringBuilder :: new , StringBuilder :: appendCodePoint , StringBuilder :: append )
    .toString()
;

That is untested code, but should be close to what you need.

My code here is based on what I saw on this similar Question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM