How to resolve the algorithm Substring/Top and tail step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Substring/Top and tail step by step in the Java programming language

Table of Contents

Problem Statement

The task is to demonstrate how to remove the first and last characters from a string. The solution should demonstrate how to obtain the following results:

If the program uses UTF-8 or UTF-16, it must work on any valid Unicode code point, whether in the Basic Multilingual Plane or above it. The program must reference logical characters (code points), not 8-bit code units for UTF-8 or 16-bit code units for UTF-16. Programs for other encodings (such as 8-bit ASCII, or EUC-JP) are not required to handle all Unicode characters.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Substring/Top and tail step by step in the Java programming language

RM_chars

  • This class uses the substring method to remove characters from a string.
  • In the first three lines, it removes the first character, the first four characters, and the first and last characters, respectively.
  • In the last three lines, it uses the replaceAll method to remove the first character, the last character, and both the first and last characters, respectively.

SubstringTopAndTail

  • This class uses the substring method to remove characters from a string, taking into account the possibility of surrogate pairs.
  • In the first line, it creates a string with a horse emoji at the beginning and end, and the letters "a", "b", and "c" in the middle.
  • In the next three lines, it calculates the size of the first and last characters in the string, taking into account the possibility that they are surrogate pairs.
  • Then, it uses the substring method to remove the first character, the last character, and both the first and last characters, respectively.
  • Finally, it prints the resulting strings to the console.

Source code in the java programming language

public class RM_chars {
  public static void main( String[] args ){
    System.out.println( "knight".substring( 1 ) );
    System.out.println( "socks".substring( 0, 4 ) );
    System.out.println( "brooms".substring( 1, 5 ) );
      // first, do this by selecting a specific substring
      // to exclude the first and last characters
    
    System.out.println( "knight".replaceAll( "^.", "" ) );
    System.out.println( "socks".replaceAll( ".$", "" ) );
    System.out.println( "brooms".replaceAll( "^.|.$", "" ) );
      // then do this using a regular expressions
  }
}


public class SubstringTopAndTail {
  public static void main( String[] args ){
    var s = "\uD83D\uDC0Eabc\uD83D\uDC0E";  // Horse emoji, a, b, c, horse emoji: "🐎abc🐎"

    var sizeOfFirstChar = Character.isSurrogate(s.charAt(0)) ? 2 : 1;
    var sizeOfLastChar = Character.isSurrogate(s.charAt(s.length() - 1)) ? 2 : 1;

    var removeFirst = s.substring(sizeOfFirstChar);
    var removeLast = s.substring(0, s.length() - sizeOfLastChar);
    var removeBoth = s.substring(sizeOfFirstChar, s.length() - sizeOfLastChar);

    System.out.println(removeFirst);
    System.out.println(removeLast);
    System.out.println(removeBoth);
  }
}


  

You may also check:How to resolve the algorithm Distributed programming step by step in the Racket programming language
You may also check:How to resolve the algorithm Longest string challenge step by step in the Julia programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Terminal control/Inverse video step by step in the Z80 Assembly programming language
You may also check:How to resolve the algorithm Long year step by step in the Julia programming language