首頁 >Java >java教程 >在Java中列印字串的前K個字元的不同方法

在Java中列印字串的前K個字元的不同方法

WBOY
WBOY轉載
2023-09-18 14:05:08880瀏覽

在Java中列印字串的前K個字元的不同方法

A string is a class in Java that stores a series of characters enclosed within double quotes.Those characters are actually String-type objects. The string class is available in the'. ' package. Suppose we have given a string and a positive integer 'k'. Now, thejob is to print that string's first 'k' characters in Java. Also, check whether the length ofgiven string is less than or not, check whether the length ofgiven string is less than or not, if. the original string.

Java Program to Print First K Characters of the String

Let’s understand the given problem with a few examples −

實例

String st1 = “TutorialsPoint”;
String st2 = “Tutorial”;

假設我們已經有了上述字串,並且給定 k 值為 9。那麼,結果將會是 −

The first K characters of st1: Tutorials
The first K characters of st2: Tutorial

字串1的長度大於9,因此我們印了前9個字元。但是,字串2的長度小於9,因此我們印了整個字串本身

方法一

  • Create a user-defined method along with two parameters of String and integer type.

  • Convert the given string into a character array and store it in ‘chs[]’.

  • We know that strings are immutable in Java therefore we need to create an object ‘new_st’ of the StringBuffer class to store characters of string into a character array

  • 使用if-else區塊檢查字串的長度。如果長度大於k,則將前k個字元追加到字元陣列中,否則列印給定的陣列。

  • 現在,在main()方法中,宣告並初始化字串和一個正整數k。然後,使用字串和k作為參數呼叫使用者定義的方法

範例

以下範例說明如何使用使用者定義的方法來檢索字串的前K個字元。

import java.util.*;
public class Kstring {
   public static void frstChar(String st, int k) {
      char chs[] = st.toCharArray(); // converting into character array
      StringBuffer new_st = new StringBuffer();
      if(st.length() > k) { // checking the length of string
         for(int i = 0; i < k; i++) {
            new_st.append(chs[i]); // appending characters to new string
         }
         System.out.println("The first K characters are: " + new_st.toString()); // printing the new string
      } else {
         System.out.println("K is greater than given String: " + st);
      }
   }
   public static void main(String args[]) {
      String st1 = "TutorialsPoint";
      String st2 = "Tutorial";
      int k = 9;
      System.out.println("The Original String: " + st1);
      System.out.println("The Original String: " + st2);
      
      // calling the method
      frstChar(st1, k);
      frstChar(st2, k);
   }
}

Output

#
The Original String: TutorialsPoint
The Original String: Tutorial
The first K characters are: Tutorials
K is greater than given String: Tutorial

Approach 2

  • 宣告並初始化一個字串和一個正整數‘k’。

  • Now, using the if-else block check whether the length of String is greater than k or not. If it is greater than k, print the same number of characters of string using 'substring()' methoding(wisewise print the whole string.

  • #內建方法 'substring()' 接受兩個整數類型的參數,並列印指定字串中在參數範圍內的字元。

Example

The following example illustrates how we can retrieve the first K characters of a String using an in-built method ‘substring()’.

#
public class Kstring {
   public static void main(String args[]) {
      String st1 = "TutorialsPoint";
      int k = 9;
      System.out.println("The Original String: " + st1);
      
      if (st1.length() > k) { // checking the length of string
         System.out.println("The first K characters are: " + st1.substring(0, k));
      } else {
         System.out.println("The first K characters are: " + st1);
      }
   }
}

Output

#
The Original String: TutorialsPoint
The first K characters are: Tutorials

Conclusion

我們透過定義String並理解Java程式的問題陳述來開始本文。後來,我們討論了兩個範例程序,一個使用使用者定義的方法,另一個使用內建方法來列印給定String的前K個字元。

以上是在Java中列印字串的前K個字元的不同方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除