Método PrintStream append (CharSequence, int, int) en Java con ejemplos

El método append(CharSequence, int, int) de PrintStream Class en Java se usa para agregar una parte específica de la charSequence especificada en la secuencia. Esta charSequence se toma como parámetro. También se toman como parámetros el índice inicial y el índice final a escribir.

Sintaxis:

agregar vacío público (CharSequence charSequence, int índice de inicio, int índice final)

Parámetros: Este método acepta tres parámetros obligatorios:

  • charSequence que es la charSequence que se escribirá en el Stream.
  • índiceInicial que es el índice inicial desde el cual se tomará la parte del carácter.
  • endingIndex, que es el índice final que se escribirá en la secuencia.

Valor devuelto: este método no devuelve ningún valor.

Los siguientes métodos ilustran el funcionamiento del método append(CharSequence, int, int):

Programa 1:

// Java program to demonstrate
// PrintStream append(CharSequence, int, int) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a PrintStream instance
            PrintStream stream
                = new PrintStream(System.out);
  
            // Get the charSequence
            // to be written in the stream
            CharSequence charSequence = "GeeksForGeeks";
  
            // Get the starting index
            int startingIndex = 0;
  
            // Get the length of char
            int endingIndex = 5;
  
            // Write the portion of the charSequence
            // to this stream using append() method
            // This will put the charSequence in the stream
            // till it is printed on the console
            stream.append(charSequence,
                          startingIndex,
                          endingIndex);
  
            stream.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

Geeks

Programa 2:

// Java program to demonstrate
// PrintStream append(CharSequence, int, int) method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            // Create a PrintStream instance
            PrintStream stream
                = new PrintStream(System.out);
  
            // Get the charSequence
            // to be written in the stream
            CharSequence charSequence = "GFG";
  
            // Get the starting index
            int startingIndex = 2;
  
            // Get the length of char
            int endingIndex = 3;
  
            // Write the portion of the charSequence
            // to this stream using append() method
            // This will put the charSequence in the stream
            // till it is printed on the console
            stream.append(charSequence,
                          startingIndex,
                          endingIndex);
  
            stream.flush();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
Producción:

G

Publicación traducida automáticamente

Artículo escrito por Kirti_Mangal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *