Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.StringTokenizer;

public class Main {
    public static String quote(String value) {
        if (value != null) {
            if (value.contains("'")) {
                if (value.contains("\"")) {
                    // We've got something perverse like [["It's back!"]] (ie a mix of single and double quotes!)
                    StringBuilder sb = new StringBuilder("concat(");

                    StringTokenizer st = new StringTokenizer(value, "'\"", true);
                    while (st.hasMoreTokens()) {
                        sb.append(quote(st.nextToken()));

                        if (st.hasMoreTokens()) {
                            sb.append(", ");
                        }
                    }

                    sb.append(")");

                    return sb.toString();
                } else {
                    return '"' + value + '"';
                }

            } else {
                return "'" + value + "'";
            }
        }

        return value;
    }
}