Skip to Main Content

How do I call a string from my main method in a different class.

2801625Dec 9 2014 — edited Jan 21 2015

Please forgive me if I'm not using the correct terminology when discussing this topic, I'm a novice java programmmer.

The following code is located in my main class called InjectionFix. I have another class where I need to call or initiate the string in the code below.

How can I achieve this. I tried to create an Object of the class but I cant do this because my other class doesnt contain a main method.How can I

get the other class to initiate the code below which is loacted in my main class.

public static String escapeDN(String name) {

  StringBuilder sb = new StringBuilder();

  // space or # character at the beginning of a string

  if ((name.length() > 0) &&

        ((name.charAt(0) == ' ') ||

             (name.charAt(0) == '#'))) {

            sb.append('\\'); // add the leading backslash if needed

        }

       

       

       

        for (int i = 0; i < name.length(); i++) {

            char curChar = name.charAt(i);

            switch (curChar) {

                case ',':    // Case 1

                    sb.append("\\,");

                    break;

                case '+': // Case 2

                    sb.append("\\+");

                    break;

                case '"': // Case 3

                    sb.append("\\\"");

                    break;

                case '\\':   // Case 4

                    sb.append("\\\\");

                    break;

                case '<': // Case 5

                    sb.append("\\<");

                    break;

                case '>': // Case 6

                    sb.append("\\>");

                    break;

                case ';': // Case 7

                    sb.append("\\;");

                    break;

                case '\n': // Case 8

                    sb.append("\\\n");

                    break;

                case '\r': // Case 9

                    sb.append("\\\r");

                    break;

                case '=': // Case 10

                    sb.append("\\=");

                    break;

                case '/': // Case 11

                    sb.append("\\/");

                    break;

                default:

                    sb.append(curChar);

            }

        }

        // space character at the end of a string

        if ((name.length() > 1) && (name.charAt(name.length() - 1) == ' ')) {

            sb.insert(sb.length() - 1, '\\'); // add the trailing backslash if needed

        }

        return sb.toString();

    }

Comments
Post Details
Added on Dec 9 2014
11 comments
2,462 views