Question on formatting Strings

Discussion in 'Java' started by Joona I Palaste, Oct 19, 2003.

  1. Would there be any existing way in Java 1.4.2 to do this kind of
    conversion?

    Input: A reference to a java.lang.Object (or to any subclass of it)
    Output: If the reference is not null, a concatenated String of the
    following:
    " (i.e. a double quote)
    the result of the object's toString() method
    " (i.e. another double quote)
    If the reference is null, the String null (i.e. a String consisting of
    four characters n, u, l, l, no double quotes).

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "And according to Occam's Toothbrush, we only need to optimise the most frequent
    instructions."
    - Teemu Kerola
    Joona I Palaste, Oct 19, 2003
    #1
    1. Advertising

  2. Joona I Palaste

    Chris Smith Guest

    Joona I Palaste wrote:
    > Would there be any existing way in Java 1.4.2 to do this kind of
    > conversion?
    >
    > Input: A reference to a java.lang.Object (or to any subclass of it)
    > Output: If the reference is not null, a concatenated String of the
    > following:
    > " (i.e. a double quote)
    > the result of the object's toString() method
    > " (i.e. another double quote)
    > If the reference is null, the String null (i.e. a String consisting of
    > four characters n, u, l, l, no double quotes).


    Existing? The code isn't already written, but it should be rather
    trivial to write:

    String out = (in == null) ? "null" : "\"" + in + "\"";

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Oct 19, 2003
    #2
    1. Advertising

  3. Chris Smith <> scribbled the following:
    > Joona I Palaste wrote:
    >> Would there be any existing way in Java 1.4.2 to do this kind of
    >> conversion?
    >>
    >> Input: A reference to a java.lang.Object (or to any subclass of it)
    >> Output: If the reference is not null, a concatenated String of the
    >> following:
    >> " (i.e. a double quote)
    >> the result of the object's toString() method
    >> " (i.e. another double quote)
    >> If the reference is null, the String null (i.e. a String consisting of
    >> four characters n, u, l, l, no double quotes).


    > Existing? The code isn't already written, but it should be rather
    > trivial to write:


    > String out = (in == null) ? "null" : "\"" + in + "\"";


    Yes, I know. Point is, I would like to write this in a veritable
    cornucpia (in Layman: a lot) of places in our application's source code.
    It would be *that* much easier if there was a ready-made method call.
    Thank anyway. Maybe I'll write my own utility method.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    Joona I Palaste, Oct 19, 2003
    #3
  4. Joona I Palaste

    Paul Lutus Guest

    Joona I Palaste wrote:

    < snip >

    >> Existing? The code isn't already written, but it should be rather
    >> trivial to write:

    >
    >> String out = (in == null) ? "null" : "\"" + in + "\"";

    >
    > Yes, I know. Point is, I would like to write this in a veritable
    > cornucpia (in Layman: a lot) of places in our application's source code.
    > It would be *that* much easier if there was a ready-made method call.
    > Thank anyway. Maybe I'll write my own utility method.


    Create a static class that has this function as its only member. Put the
    class in your project classpath. Refer to it at will.

    final public class Gadget {
    public static void doItForMe(Object ob)
    {
    System.out.println(ob == null ? "null" : "\"" + ob + "\"");
    }
    }

    Gadget.doItForMe(null);

    null

    Gadget.doItForMe(new Object());

    "java.lang.Object@14fe5c"

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Oct 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Perecli Manole

    formatting strings

    Perecli Manole, Oct 23, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    548
    Bret Mulvey [MS]
    Oct 24, 2003
  2. Guadala Harry

    Formatting Strings

    Guadala Harry, Oct 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    389
    Guadala Harry
    Oct 28, 2004
  3. Rick

    formatting strings

    Rick, Oct 16, 2003, in forum: C Programming
    Replies:
    10
    Views:
    617
    Irrwahn Grausewitz
    Oct 17, 2003
  4. Steve Holden

    Enumerating formatting strings

    Steve Holden, Apr 18, 2005, in forum: Python
    Replies:
    14
    Views:
    511
    Michael Spencer
    Apr 21, 2005
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    756
    Malcolm
    Jun 24, 2006
Loading...

Share This Page