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

Last change on this file since 187 was 175, checked in by sherbold, 13 years ago
  • code documentation and formatting
File size: 1.1 KB
RevLine 
[1]1package de.ugoe.cs.util;
2
[175]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 */
[1]11final public class StringTools {
12
[175]13        /**
14         * <p>
15         * Simplifies use of operation system specific line separators.
16         * </p>
17         */
[1]18        public final static String ENDLINE = System.getProperty("line.separator");
[175]19
[1]20        /**
21         * <p>
[175]22         * Replaces all occurrences of {@literal &, <, >, ', and "} with their
23         * respective XML entities {@literal &amp;, &lt;, &gt;, &apos;, and &quot;}
[1]24         * without destroying already existing entities.
25         * </p>
26         *
27         * @param str
[175]28         *            String where the XML entities are to be replaced
29         * @return new String, where the XML entities are used instead of the
[1]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.