Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:39:53+00:00 2026-06-17T08:39:53+00:00

Does anyone know of a library function to escape and surround a string value

  • 0

Does anyone know of a library function to escape and surround a string value of an object with quotes unless it is null?

For example, the function should process:

  • "hello" as "\"hello\""
  • null as "null"
  • Long.valueOf(2L) as "\"2\""
  • "" as "\"\""
  • "I\'m \"quoted\"" as "\"I\\\'m \\\"quoted\\\""

Clearly this is trivial to implement in Java, but I’m looking for a function in an existing library (e.g. JDK, Commons Lang, Spring …) I’d be surprised if this hasn’t been done before.

For reference, this is for converting objects to JavaScript strings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:39:54+00:00Added an answer on June 17, 2026 at 8:39 am

    Forget the Quoting, really. If you want to write Java to JS, use Jackson:

    import java.util.Date;
    
    import org.codehaus.jackson.map.ObjectMapper;
    
    public class Teste {
    
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
            printAsJs(null);
            printAsJs(5L);
            printAsJs("Hello!");
            printAsJs(new Date());
        }
    
        private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    
        public static void printAsJs(Object o) throws Exception {
            System.out.println(OBJECT_MAPPER.writeValueAsString(o));
        }
    
    }
    

    If you need extra handlers to format as JS, extend ObjectMapper with your desired needs

    Thank you come again

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of a function that can create an lm object given a
Does anyone know of a PHP FTP library/function that is capable of downloading whole
Does anyone know why there is no snwprintf function in the C standard library?
Does anyone know a Library which provides a Thread.sleep() for Java which has an
Does anyone know of a library for RDP that's written in C or Objective-C
Does anyone know of a library that would allow me to manipulate Flex DOM
Does anyone know of a library that works with Ruby 1.9.1 on Rails 3
Does anyone know why a library initialized within dlopen() would initialize a static variable
Does anyone know of a library that enables you to run an application inside
Does anyone know of a good library (or code snippet) for converting a TimeSpan

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.