01: /* StringFns.java
02:
03: {{IS_NOTE
04: Purpose:
05:
06: Description:
07:
08: History:
09: Thu Mar 31 12:25:57 2005, Created by tomyeh
10: }}IS_NOTE
11:
12: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
13:
14: {{IS_RIGHT
15: This program is distributed under GPL Version 2.0 in the hope that
16: it will be useful, but WITHOUT ANY WARRANTY.
17: }}IS_RIGHT
18: */
19: package org.zkoss.xel.fn;
20:
21: import org.zkoss.lang.Objects;
22:
23: /**
24: * Utilities to manipulate strings in EL.
25: *
26: * @author tomyeh
27: */
28: public class StringFns {
29: /** Catenates two strings.
30: * Note: null is considered as empty.
31: */
32: public static String cat(String s1, String s2) {
33: if (s1 == null)
34: return s2 != null ? s2 : "";
35: return s2 != null ? s1 + s2 : s1;
36: }
37:
38: /** Catenates three strings.
39: * Note: null is considered as empty.
40: */
41: public static String cat3(String s1, String s2, String s3) {
42: return cat(cat(s1, s2), s3);
43: }
44:
45: /** Catenates four strings.
46: * Note: null is considered as empty.
47: */
48: public static String cat4(String s1, String s2, String s3, String s4) {
49: return cat(cat(cat(s1, s2), s3), s4);
50: }
51:
52: /** Catenates four strings.
53: * Note: null is considered as empty.
54: */
55: public static String cat5(String s1, String s2, String s3,
56: String s4, String s5) {
57: return cat(cat(cat(cat(s1, s2), s3), s4), s5);
58: }
59:
60: /** Replaces all occurrances of 'from' in 'src' with 'to'
61: */
62: public static String replace(String src, String from, String to) {
63: if (Objects.equals(from, to))
64: return src;
65:
66: final StringBuffer sb = new StringBuffer(src);
67: if ("\n".equals(from) || "\r\n".equals(from)) {
68: replace0(sb, "\r\n", to);
69: replace0(sb, "\n", to);
70: } else {
71: replace0(sb, from, to);
72: }
73: return sb.toString();
74: }
75:
76: private static void replace0(StringBuffer sb, String from, String to) {
77: final int len = from.length();
78: for (int j = 0; (j = sb.indexOf(from, j)) >= 0;) {
79: sb.replace(j, j += len, to);
80: }
81: }
82: }
|