0

I am trying to create a text file with Group Separator, I couldn't find any syntax on how to do it. For example, if I have two strings I need to create something like below into a text file through Java.

String a="HREC";
String b="ZZ";

and final output should appear with group separators as shown in below image

Output expected

HRECZZ

Is there anyway to do it?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 2
    What is the hex value of `GS`? – PM 77-1 Jul 09 '21 at 15:42
  • 3
    @PM77-1 GS (Group Seperator) is 0x1D in ASCII. – Mark Rotteveel Jul 09 '21 at 15:43
  • @MarkRotteveel I do not understand your closure of this Question. This Question has nothing to do with bytes as [discussed in your linked Question](https://stackoverflow.com/questions/28592000/how-to-get-the-bytes-of-a-control-character-in-java), and can be answered without manipulating octets. – Basil Bourque Jul 10 '21 at 20:41
  • @BasilBourque The accepted answers shows how to write control characters to a character stream. – Mark Rotteveel Jul 11 '21 at 06:38
  • @MarkRotteveel But the *question* is not a duplicate. And, the reader must dig through the Answers to discover the part that happens to be a solution here. I appreciate trying to close needless questions as duplicates; I do so often myself. But this is not such a case. – Basil Bourque Jul 11 '21 at 07:52

1 Answers1

2

Unicode code point

Call Character.toString while passing the Unicode code point of that character.

Understand that Unicode is a superset of US-ASCII. All the 128 characters in ASCII make up the first 128 of the 144,697 characters defined in Unicode, at code points 0-127.

For the group separator non-printable character, the decimal integer for its code point is 29.

\u001D

You can write that number 29 using its hexadecimal version 1D as an escaped Unicode code point in a String literal: \u001D.

"dog\u001Dcat"  // A string of seven characters.

Character.toString( 29 )

For frequent use, define a constant.

Calling Character.toString( 29 ) produces a String containing a single character, your group separator.

final String FILE_SEPARATOR = Character.toString( 28 ) ;
final String GROUP_SEPARATOR = Character.toString( 29 ) ;
final String RECORD_SEPARATOR = Character.toString( 30 ) ;
final String UNIT_SEPARATOR = Character.toString( 31 ) ;

Or make a class with statics. Here for brevity we use the two-character abbreviated names defined in ASCII.

public class Ascii 
{
    static final String FS = Character.toString( 28 ) ;
    static final String GS= Character.toString( 29 ) ;
    static final String RS= Character.toString( 30 ) ;
    static final String US= Character.toString( 31 ) ;
}

Usage:

myStringBuilder.append( Ascii.GS ) ;

For those not familiar with these separator characters, see the Question, What are the file/group/record/unit separator control characters and their usage?.


By the way, while researching this I discovered that Unicode also defines a character SYMBOL FOR GROUP SEPARATOR (code point 9,245) for documentation that needs to display something visibly that means the unprintable character.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154