Open In App

CollationElementIterator setOffset() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The setOffset() method of java.text.CollationElementIterator class is used to set the cursor of the iterator to the particular index passed as parameter.

Syntax: 

public void setOffset(int newOffset)

Parameter: This method takes an integer value newOffset at which point the cursor has to be set.

Return Value: This method has nothing to return. 

Below are the examples to illustrate the setOffset() method:

Example 1:  

Java




// Java program to demonstrate
// setOffset() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // creating and initializing testString
        String test = "Code Geeks 123";
 
        // creating and initializing
        // RuleBasedCollator object
        RuleBasedCollator rbc
            = (RuleBasedCollator)(Collator.getInstance());
 
        // creating and initializing
        // CollationElementIterator
        CollationElementIterator cel
            = rbc.getCollationElementIterator(test);
 
        // setting offset to index 4
        // using setOffset() method
        cel.setOffset(4);
 
        // display the result
        System.out.println("current offset is "
                           + cel.getOffset());
    }
}


Output: 

current offset is 4

 

Example 2: 

Java




// Java program to demonstrate
// setOffset() method
 
import java.text.*;
import java.util.*;
import java.io.*;
 
public class GFG {
    public static void main(String[] argv)
    {
        // creating and initializing testString
        String test = "GeeksForGeeks";
 
        // creating and initializing
        // RuleBasedCollator object
        RuleBasedCollator rbc
            = (RuleBasedCollator)(Collator.getInstance());
 
        // creating and initializing
        // CollationElementIterator
        CollationElementIterator cel
            = rbc.getCollationElementIterator(test);
 
        // after call of setOffset() method
        // all next() method will become redundant
        cel.next();
        cel.next();
        cel.next();
        cel.next();
 
        // setting cursor of iterator to index 0
        // using setOffset() method
        cel.setOffset(0);
 
        // display the result
        System.out.println("current offset is "
                           + cel.getOffset());
    }
}


Output: 

current offset is 0

 

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/CollationElementIterator.html#setOffset-int-
 



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads