Java String to convertString(String label)

Here you can find the source of convertString(String label)

Description

Escape all occurences of newline chars '\n', quotes \", etc.

License

Open Source License

Declaration

public static final String convertString(String label) 

Method Source Code

//package com.java2s;
/*//from  ww w  .j a  v  a 2s.c  o  m
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /**
     * Escape all occurences of newline chars '\n', quotes \", etc.
     */
    public static final String convertString(String label) {
        char[] ch = label.toCharArray();
        StringBuffer buf = new StringBuffer();

        for (int i = 0; i < ch.length; i++) {
            switch (ch[i]) {
            case '\n':
                buf.append("\\n");
                break;
            case '\r':
                buf.append("\\r");
                break;
            case '\"':
                buf.append("\\\"");
                break;
            case '\'':
                buf.append("\\'");
                break;
            case '\\':
                buf.append("\\\\");
                break;
            default:
                buf.append(ch[i]);
                break;
            }
        }

        return buf.toString();
    }
}

Related

  1. convertString(char firstCharacter, String value)
  2. convertString(Object obj, String nullTo)
  3. convertString(String s)
  4. convertString(String s, Class cls)
  5. convertString(String strValue)
  6. convertString(String value, Class type)