String to BitSet - Java java.util

Java examples for java.util:BitSet

Description

String to BitSet

Demo Code


//package com.java2s;
import java.util.BitSet;

public class Main {
    public static void main(String[] argv) throws Exception {
        String s = "java2s.com";
        int length = 2;
        System.out.println(toBitSet(s, length));
    }//from  w  ww .  j  a  va2 s  .com

    public static BitSet toBitSet(String s, int length) {
        int len = s.length();
        BitSet bs = new BitSet(length);
        for (int i = 0; i < len; i++) {
            if (s.charAt(i) == '1')
                bs.set(i);
        }
        return bs;
    }
}

Related Tutorials