Different Ways to Print First K Characters of the String in Java - GeeksforGeeks

30 Aug, 2020
Given a string str and a positive integer k, the task is to write a Java program to print the first k characters of the string. If the length of the string is less than k then print the string as it is.
Examples:
Input: str = “GeeksForGeeks”, k = 5
Output: Geeks
Explanation: The first k characters of the given string is ‘Geeks’.
Input: str = “Geeks”, k = 6
Output: Geeks
Explanation: The length of the given string is less than k. Therefore, we print the string as it is.
Get the string and a positive integer k to print the first k characters of the string.Check if the string is null or empty then return null.Check if the string length is greater than k then get the first k characters of the string using str.substring(0, k).If the string length is less than k then return the string as it is.Now, print the first k characters of the string.
Get the string and a positive integer k to print the first k characters of the string.
Check if the string is null or empty then return null.
Check if the string length is greater than k then get the first k characters of the string using str.substring(0, k).
If the string length is less than k then return the string as it is.
Now, print the first k characters of the string.
Below is the implementation of the above approach:
Java
// Java program to print first k// characters of the string  class GFG {          // Function to print first k    // characters of the string    public static String       firstKCharacters(String str, int k)    {                  // Check if the string is empty        // or null then return null        if (str == null || str.isEmpty())            return null;                  // Check if the string length        // is greater than k, then        // get the first k characters         // of the string, otherwise        // return the string as it is        if (str.length() > k)            return str.substring(0, k);                  else            return str;    }          // Driver Code    public static void main(String args[])    {        // Given String str        String str = "GeeksForGeeks";                  // Given a positive integer k        int k = 5;                  // Print the first k characters        // of the string        System.out.println(          firstKCharacters(str, k));    }}
Geeks




The idea is to use Math.min() function as an end index of the substring method.Find the minimum value between the length of the string and positive integer k.Get the substring from zero to the minimum of string length and k.Now, print the first k characters of the string.
The idea is to use Math.min() function as an end index of the substring method.
Find the minimum value between the length of the string and positive integer k.
Get the substring from zero to the minimum of string length and k.
Now, print the first k characters of the string.
Below is the implementation of the above approach:
Java
// Java program to print first k// characters of the string  class GFG {      // Function to print first k    // characters of the string    public static String firstKCharacters(String str, int k)    {        // Check if the string is        // null or empty then        // return null        if (str == null || str.isEmpty())            return null;          // Return the first k characters        // of the string if the string        // length is less than k, otherwise        // return the string as it is        return str.substring(0, Math.min(str.length(), k));    }      // Driver Code    public static void main(String args[])    {        // Given String str        String str = "GeeksForGeeks";          // Given a positive integer k        int k = 5;          // Print the first k characters        // of the string        System.out.println(firstKCharacters(str, k));    }}
Geeks




Java-String-Programs
strings
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
Singleton Class in Java
Set in Java
Queue Interface In Java
Convert a String to Character array in Java
Initializing a List in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class