source: trunk/JavaHelperLib/src/de/ugoe/cs/util/StringTools.java @ 226

Last change on this file since 226 was 175, checked in by sherbold, 13 years ago
  • code documentation and formatting
File size: 1.1 KB
Line 
1package de.ugoe.cs.util;
2
3/**
4 * <p>
5 * Helper class that provides methods to simplify working with {@link String}s.
6 * </p>
7 *
8 * @author Steffen Herbold
9 * @version 1.0
10 */
11final public class StringTools {
12
13        /**
14         * <p>
15         * Simplifies use of operation system specific line separators.
16         * </p>
17         */
18        public final static String ENDLINE = System.getProperty("line.separator");
19
20        /**
21         * <p>
22         * Replaces all occurrences of {@literal &, <, >, ', and "} with their
23         * respective XML entities {@literal &amp;, &lt;, &gt;, &apos;, and &quot;}
24         * without destroying already existing entities.
25         * </p>
26         *
27         * @param str
28         *            String where the XML entities are to be replaced
29         * @return new String, where the XML entities are used instead of the
30         *         literals
31         */
32        public static String xmlEntityReplacement(String str) {
33                String result = str;
34                result = result.replaceAll("&(?!(?:lt|gt|apos|quot|amp);)", "&amp;");
35                result = result.replaceAll("<", "&lt;");
36                result = result.replaceAll(">", "&gt;");
37                result = result.replaceAll("'", "&apos;");
38                result = result.replaceAll("\"", "&quot;");
39                return result;
40        }
41}
Note: See TracBrowser for help on using the repository browser.