CharSequence interface in java

java.lang.CharSequence is a interface in java programming language. Inside of this interface exists 6 public methods. Declaration of this interface as follows.

public interface java.lang.CharSequence {
  public abstract int length();
  public abstract char charAt(int);
  public abstract java.lang.CharSequence subSequence(int, int);
  public abstract java.lang.String toString();
  public java.util.stream.IntStream chars();
  public java.util.stream.IntStream codePoints();
  private java.util.Spliterator$OfInt lambda$codePoints$1();
  private java.util.Spliterator$OfInt lambda$chars$0();
}

CharSequence public method

There are following useful methods which is define the inside of java.lang.CharSequence java interface.

MethodDescription
char charAt(int index) It returns the value at the specified index.
default IntStream chars() It returns a stream of zero-extending the char values from this sequence.
default IntStream codePoints() It returns a stream of code point values from this sequence.
int length() It returns the length of this character sequence.
CharSequence subSequence(int start,int end) It returns a that is a subsequence of this sequence.
String toString() It returns a string containing the characters in this sequence in the same order as this sequence.

This reference is belong to javadoc

Implementing Classes

There are following java class are implements this interface functionality.

Such as CharBuffer, Segment, String, StringBuffer, StringBuilder. Here given few example.

// Using of String Class
// Useful packages
import java.lang.CharSequence;
import java.lang.String;
class Example
{
	public static void main(String[] args)
	{
		CharSequence x = new String("ABCDE");
		// Interface method  
		System.out.println(x.toString());
		System.out.println(x.charAt(0));
		System.out.println(x.length());
		System.out.println(x.subSequence(0, 2));
		// IntStream of Unicode code points from this sequence
		System.out.println(x.codePoints());
		// IntStream of char values from this sequence
		System.out.println(x.chars());
	}
}
CharSequence using String class
ABCDE
A
5
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@3e3abc88
// Using of javax.swing.text.Segment
// Useful packages
import java.lang.CharSequence;
import javax.swing.text.Segment;
class Example
{
	public static void main(String[] args)
	{
		char[] arr = {
			'A' , 'B' , 'C' , 'D' , 'E'
		};
		CharSequence x = new Segment(arr, 0, 3);
		// Interface method  
		System.out.println(x.toString());
		System.out.println(x.charAt(0));
		System.out.println(x.length());
		System.out.println(x.subSequence(0, 2));
		// IntStream of Unicode code points from this sequence
		System.out.println(x.codePoints());
		// IntStream of char values from this sequence
		System.out.println(x.chars());
	}
}
CharSequence using Segment class
ABC
A
3
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@3e3abc88
// Using of StringBuffer Class
// Useful packages
import java.lang.CharSequence;
import java.lang.String;
class Example
{
	public static void main(String[] args)
	{
		CharSequence x = new StringBuffer("ABCDE");
		// Interface method  
		System.out.println(x.toString());
		System.out.println(x.charAt(0));
		System.out.println(x.length());
		System.out.println(x.subSequence(0, 2));
		// IntStream of Unicode code points from this sequence
		System.out.println(x.codePoints());
		// IntStream of char values from this sequence
		System.out.println(x.chars());
	}
}
CharSequence using StringBuffer class
ABCDE
A
5
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@3e3abc88
// Using of StringBuilder Class
// Useful packages
import java.lang.CharSequence;
import java.lang.StringBuilder;
class Example
{
	public static void main(String[] args)
	{
		CharSequence x = new StringBuilder("ABCDE");
		// Interface method  
		System.out.println(x.toString());
		System.out.println(x.charAt(0));
		System.out.println(x.length());
		System.out.println(x.subSequence(0, 2));
		// IntStream of Unicode code points from this sequence
		System.out.println(x.codePoints());
		// IntStream of char values from this sequence
		System.out.println(x.chars());
	}
}
CharSequence using StringBuilder class
ABCDE
A
5
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@3e3abc88
// Using of CharBuffer
import java.nio.CharBuffer;
public class Example
{
	public static void main(String[] args)
	{
		try
		{
			CharBuffer x = CharBuffer.allocate(5);
			x.append("ABC");
			x.append('D');
			x.append('E');
			x.rewind();
            // Test the method
			System.out.println(x.toString());
			System.out.println(x.charAt(0));
			System.out.println(x.length());
			System.out.println(x.subSequence(0, 2));
			// IntStream of Unicode code points from this sequence
			System.out.println(x.codePoints());
			// IntStream of char values from this sequence
			System.out.println(x.chars());
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}
CharSequence using CharBuffer class
ABCDE
A
5
AB
java.util.stream.IntPipeline$Head@816f27d
java.util.stream.IntPipeline$Head@6ce253f1


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved