Skip to content
Related Articles

Related Articles

Improve Article

CollationElementIterator setOffset() method in Java with Examples

  • Last Updated : 13 Aug, 2021
Geek Week

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 redundent
        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-
 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :