Java CharBuffer get substring

Description

Java CharBuffer get substring


import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class Main {
  public static void main(String[] argv) throws Exception {
    ByteBuffer buf = ByteBuffer.allocate(100);

    CharBuffer cbuf = buf.asCharBuffer();

    cbuf.put("demo2s.com");

    int start = 2; // start is relative to cbuf's current position
    int end = 5;/*  w w w.j  ava2s.c om*/
    
    cbuf.rewind();
    CharSequence sub = cbuf.subSequence(start, end); 

    System.out.println(cbuf.capacity());
    System.out.println(sub);
  }
}



PreviousNext

Related