Java Java String Format toJavaString(String text)

Here you can find the source of toJavaString(String text)

Description

to Java String

License

Open Source License

Parameter

Parameter Description
text a parameter

Return

a Java string

Declaration

public static String toJavaString(String text) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2014 IBM Corp.//  w  ww. ja v a 2 s  .  co  m
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution. 
 *
 * The Eclipse Public License is available at 
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at 
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 *******************************************************************************/

public class Main {
    /**
     * @param text
     * @return a Java string
     */
    public static String toJavaString(String text) {

        String string = text;
        if (string != null) {
            string = string.replaceAll("\n", "\\\\n");
            string = string.replaceAll("\r", "\\\\r");
            string = string.replaceAll("\"", "\\\\\"");
        }

        StringBuilder sb = new StringBuilder();
        sb.append("\"");
        sb.append(string);
        sb.append("\"");

        return sb.toString();
    }
}

Related

  1. toJavaSecs(long secs)
  2. toJavaSourceType(String type)
  3. toJavaStaticIdentifier(String string)
  4. toJavaString(final String s)
  5. toJavaString(String buf)
  6. toJavaTime(double time)
  7. toJavaTypeIdentifier(String string)
  8. toJavaTypeName(String edmTypeName)
  9. toJavaTypes(Class clazz, String value)