Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent of Python repr()?

Tags:

Is there a Java method that works like Python's repr? For example, assuming the function were named repr,

"foo\n\tbar".repr() 

would return

"foo\n\tbar"

not

foo         bar

as toString does.

like image 851
Ian Dalton Avatar asked Aug 29 '09 03:08

Ian Dalton


People also ask

What is repr () Python?

Python repr() The repr() function returns a printable representation of the given object.

Is repr () a Python built in function?

In Python, __repr__ is a special method used to represent a class's objects as a string. __repr__ is called by the repr() built-in function. You can define your own string representation of your class objects using the __repr__ method. Special methods are a set of predefined methods used to enrich your classes.

What is the difference between repr and str?

repr() compute the “official” string representation of an object (a representation that has all information about the object) and str() is used to compute the “informal” string representation of an object (a representation that is useful for printing the object).

What is the purpose of defining the functions __ str __ and __ repr __ within a class how are the two functions different?

__str__ is used in to show a string representation of your object to be read easily by others. __repr__ is used to show a string representation of the object.


1 Answers

In some projects, I use the following helper function to accomplish something akin to Python's repr for strings:

private static final char CONTROL_LIMIT = ' '; private static final char PRINTABLE_LIMIT = '\u007e'; private static final char[] HEX_DIGITS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };  public static String toPrintableRepresentation(String source) {      if( source == null ) return null;     else {          final StringBuilder sb = new StringBuilder();         final int limit = source.length();         char[] hexbuf = null;          int pointer = 0;          sb.append('"');          while( pointer < limit ) {              int ch = source.charAt(pointer++);              switch( ch ) {              case '\0': sb.append("\\0"); break;             case '\t': sb.append("\\t"); break;             case '\n': sb.append("\\n"); break;             case '\r': sb.append("\\r"); break;             case '\"': sb.append("\\\""); break;             case '\\': sb.append("\\\\"); break;              default:                 if( CONTROL_LIMIT <= ch && ch <= PRINTABLE_LIMIT ) sb.append((char)ch);                 else {                      sb.append("\\u");                      if( hexbuf == null )                          hexbuf = new char[4];                      for( int offs = 4; offs > 0; ) {                          hexbuf[--offs] = HEX_DIGITS[ch & 0xf];                         ch >>>= 4;                      }                      sb.append(hexbuf, 0, 4);                 }             }         }          return sb.append('"').toString();     } } 

Its main advantage over many of the other solutions given here is, that it does not filter only a limited set of non-printable characters (like those replace-based solutions), but simply all non-printable ASCII characters. Some of it could have been written slightly nicer, but it actually does its job...

Note, that like the Python function, this one will surround the string with quotes. If you do not want that, you will have to eliminate the append('"') calls before and after the while loop.

like image 154
Dirk Avatar answered Oct 12 '22 03:10

Dirk