Source Code Cross Referenced for StringUtil.java in  » Portal » liferay-portal-4.4.2 » com » liferay » portal » kernel » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » liferay portal 4.4.2 » com.liferay.portal.kernel.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /**
0002:         * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
0003:         *
0004:         * Permission is hereby granted, free of charge, to any person obtaining a copy
0005:         * of this software and associated documentation files (the "Software"), to deal
0006:         * in the Software without restriction, including without limitation the rights
0007:         * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0008:         * copies of the Software, and to permit persons to whom the Software is
0009:         * furnished to do so, subject to the following conditions:
0010:         *
0011:         * The above copyright notice and this permission notice shall be included in
0012:         * all copies or substantial portions of the Software.
0013:         *
0014:         * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015:         * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016:         * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0017:         * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0018:         * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0019:         * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0020:         * SOFTWARE.
0021:         */package com.liferay.portal.kernel.util;
0022:
0023:        import com.liferay.portal.kernel.log.Log;
0024:        import com.liferay.portal.kernel.log.LogFactoryUtil;
0025:
0026:        import java.io.BufferedReader;
0027:        import java.io.IOException;
0028:        import java.io.InputStream;
0029:        import java.io.InputStreamReader;
0030:        import java.io.StringReader;
0031:
0032:        import java.net.URL;
0033:
0034:        import java.util.ArrayList;
0035:        import java.util.Enumeration;
0036:        import java.util.List;
0037:        import java.util.Map;
0038:        import java.util.StringTokenizer;
0039:
0040:        /**
0041:         * <a href="StringUtil.java.html"><b><i>View Source</i></b></a>
0042:         *
0043:         * @author Brian Wing Shun Chan
0044:         *
0045:         */
0046:        public class StringUtil {
0047:
0048:            public static String add(String s, String add) {
0049:                return add(s, add, StringPool.COMMA);
0050:            }
0051:
0052:            public static String add(String s, String add, String delimiter) {
0053:                return add(s, add, delimiter, false);
0054:            }
0055:
0056:            public static String add(String s, String add, String delimiter,
0057:                    boolean allowDuplicates) {
0058:
0059:                if ((add == null) || (delimiter == null)) {
0060:                    return null;
0061:                }
0062:
0063:                if (s == null) {
0064:                    s = StringPool.BLANK;
0065:                }
0066:
0067:                if (allowDuplicates || !contains(s, add, delimiter)) {
0068:                    StringMaker sm = new StringMaker();
0069:
0070:                    sm.append(s);
0071:
0072:                    if (Validator.isNull(s) || s.endsWith(delimiter)) {
0073:                        sm.append(add);
0074:                        sm.append(delimiter);
0075:                    } else {
0076:                        sm.append(delimiter);
0077:                        sm.append(add);
0078:                        sm.append(delimiter);
0079:                    }
0080:
0081:                    s = sm.toString();
0082:                }
0083:
0084:                return s;
0085:            }
0086:
0087:            public static String bytesToHexString(byte[] bytes) {
0088:                StringMaker sm = new StringMaker(bytes.length * 2);
0089:
0090:                for (int i = 0; i < bytes.length; i++) {
0091:                    String hex = Integer.toHexString(
0092:                            0x0100 + (bytes[i] & 0x00FF)).substring(1);
0093:
0094:                    if (hex.length() < 2) {
0095:                        sm.append("0");
0096:                    }
0097:
0098:                    sm.append(hex);
0099:                }
0100:
0101:                return sm.toString();
0102:            }
0103:
0104:            public static boolean contains(String s, String text) {
0105:                return contains(s, text, StringPool.COMMA);
0106:            }
0107:
0108:            public static boolean contains(String s, String text,
0109:                    String delimiter) {
0110:                if ((s == null) || (text == null) || (delimiter == null)) {
0111:                    return false;
0112:                }
0113:
0114:                StringMaker sm = null;
0115:
0116:                if (!s.endsWith(delimiter)) {
0117:                    sm = new StringMaker();
0118:
0119:                    sm.append(s);
0120:                    sm.append(delimiter);
0121:
0122:                    s = sm.toString();
0123:                }
0124:
0125:                sm = new StringMaker();
0126:
0127:                sm.append(delimiter);
0128:                sm.append(text);
0129:                sm.append(delimiter);
0130:
0131:                String dtd = sm.toString();
0132:
0133:                int pos = s.indexOf(dtd);
0134:
0135:                if (pos == -1) {
0136:                    sm = new StringMaker();
0137:
0138:                    sm.append(text);
0139:                    sm.append(delimiter);
0140:
0141:                    String td = sm.toString();
0142:
0143:                    if (s.startsWith(td)) {
0144:                        return true;
0145:                    }
0146:
0147:                    return false;
0148:                }
0149:
0150:                return true;
0151:            }
0152:
0153:            public static int count(String s, String text) {
0154:                if ((s == null) || (text == null)) {
0155:                    return 0;
0156:                }
0157:
0158:                int count = 0;
0159:
0160:                int pos = s.indexOf(text);
0161:
0162:                while (pos != -1) {
0163:                    pos = s.indexOf(text, pos + text.length());
0164:
0165:                    count++;
0166:                }
0167:
0168:                return count;
0169:            }
0170:
0171:            public static boolean endsWith(String s, char end) {
0172:                return endsWith(s, (new Character(end)).toString());
0173:            }
0174:
0175:            public static boolean endsWith(String s, String end) {
0176:                if ((s == null) || (end == null)) {
0177:                    return false;
0178:                }
0179:
0180:                if (end.length() > s.length()) {
0181:                    return false;
0182:                }
0183:
0184:                String temp = s
0185:                        .substring(s.length() - end.length(), s.length());
0186:
0187:                if (temp.equalsIgnoreCase(end)) {
0188:                    return true;
0189:                } else {
0190:                    return false;
0191:                }
0192:            }
0193:
0194:            public static String extractChars(String s) {
0195:                if (s == null) {
0196:                    return StringPool.BLANK;
0197:                }
0198:
0199:                StringMaker sm = new StringMaker();
0200:
0201:                char[] c = s.toCharArray();
0202:
0203:                for (int i = 0; i < c.length; i++) {
0204:                    if (Validator.isChar(c[i])) {
0205:                        sm.append(c[i]);
0206:                    }
0207:                }
0208:
0209:                return sm.toString();
0210:            }
0211:
0212:            public static String extractDigits(String s) {
0213:                if (s == null) {
0214:                    return StringPool.BLANK;
0215:                }
0216:
0217:                StringMaker sm = new StringMaker();
0218:
0219:                char[] c = s.toCharArray();
0220:
0221:                for (int i = 0; i < c.length; i++) {
0222:                    if (Validator.isDigit(c[i])) {
0223:                        sm.append(c[i]);
0224:                    }
0225:                }
0226:
0227:                return sm.toString();
0228:            }
0229:
0230:            public static String extractFirst(String s, String delimiter) {
0231:                if (s == null) {
0232:                    return null;
0233:                } else {
0234:                    String[] array = split(s, delimiter);
0235:
0236:                    if (array.length > 0) {
0237:                        return array[0];
0238:                    } else {
0239:                        return null;
0240:                    }
0241:                }
0242:            }
0243:
0244:            public static String extractLast(String s, String delimiter) {
0245:                if (s == null) {
0246:                    return null;
0247:                } else {
0248:                    String[] array = split(s, delimiter);
0249:
0250:                    if (array.length > 0) {
0251:                        return array[array.length - 1];
0252:                    } else {
0253:                        return null;
0254:                    }
0255:                }
0256:            }
0257:
0258:            public static String highlight(String s, String keywords) {
0259:                return highlight(s, keywords, "<span class=\"highlight\">",
0260:                        "</span>");
0261:            }
0262:
0263:            public static String highlight(String s, String keywords,
0264:                    String highlight1, String highlight2) {
0265:
0266:                if (s == null) {
0267:                    return null;
0268:                }
0269:
0270:                // The problem with using a regexp is that it searches the text in a
0271:                // case insenstive manner but doens't replace the text in a case
0272:                // insenstive manner. So the search results actually get messed up. The
0273:                // best way is to actually parse the results.
0274:
0275:                //return s.replaceAll(
0276:                //	"(?i)" + keywords, highlight1 + keywords + highlight2);
0277:
0278:                StringMaker sm = new StringMaker(StringPool.SPACE);
0279:
0280:                StringTokenizer st = new StringTokenizer(s);
0281:
0282:                while (st.hasMoreTokens()) {
0283:                    String token = st.nextToken();
0284:
0285:                    if (token.equalsIgnoreCase(keywords)) {
0286:                        sm.append(highlight1);
0287:                        sm.append(token);
0288:                        sm.append(highlight2);
0289:                    } else {
0290:                        sm.append(token);
0291:                    }
0292:
0293:                    if (st.hasMoreTokens()) {
0294:                        sm.append(StringPool.SPACE);
0295:                    }
0296:                }
0297:
0298:                return sm.toString();
0299:            }
0300:
0301:            public static String lowerCase(String s) {
0302:                if (s == null) {
0303:                    return null;
0304:                } else {
0305:                    return s.toLowerCase();
0306:                }
0307:            }
0308:
0309:            public static String merge(boolean[] array) {
0310:                return merge(array, StringPool.COMMA);
0311:            }
0312:
0313:            public static String merge(boolean[] array, String delimiter) {
0314:                if (array == null) {
0315:                    return null;
0316:                }
0317:
0318:                StringMaker sm = new StringMaker();
0319:
0320:                for (int i = 0; i < array.length; i++) {
0321:                    sm.append(String.valueOf(array[i]).trim());
0322:
0323:                    if ((i + 1) != array.length) {
0324:                        sm.append(delimiter);
0325:                    }
0326:                }
0327:
0328:                return sm.toString();
0329:            }
0330:
0331:            public static String merge(int[] array) {
0332:                return merge(array, StringPool.COMMA);
0333:            }
0334:
0335:            public static String merge(int[] array, String delimiter) {
0336:                if (array == null) {
0337:                    return null;
0338:                }
0339:
0340:                StringMaker sm = new StringMaker();
0341:
0342:                for (int i = 0; i < array.length; i++) {
0343:                    sm.append(String.valueOf(array[i]).trim());
0344:
0345:                    if ((i + 1) != array.length) {
0346:                        sm.append(delimiter);
0347:                    }
0348:                }
0349:
0350:                return sm.toString();
0351:            }
0352:
0353:            public static String merge(long[] array) {
0354:                return merge(array, StringPool.COMMA);
0355:            }
0356:
0357:            public static String merge(long[] array, String delimiter) {
0358:                if (array == null) {
0359:                    return null;
0360:                }
0361:
0362:                StringMaker sm = new StringMaker();
0363:
0364:                for (int i = 0; i < array.length; i++) {
0365:                    sm.append(String.valueOf(array[i]).trim());
0366:
0367:                    if ((i + 1) != array.length) {
0368:                        sm.append(delimiter);
0369:                    }
0370:                }
0371:
0372:                return sm.toString();
0373:            }
0374:
0375:            public static String merge(short[] array) {
0376:                return merge(array, StringPool.COMMA);
0377:            }
0378:
0379:            public static String merge(short[] array, String delimiter) {
0380:                if (array == null) {
0381:                    return null;
0382:                }
0383:
0384:                StringMaker sm = new StringMaker();
0385:
0386:                for (int i = 0; i < array.length; i++) {
0387:                    sm.append(String.valueOf(array[i]).trim());
0388:
0389:                    if ((i + 1) != array.length) {
0390:                        sm.append(delimiter);
0391:                    }
0392:                }
0393:
0394:                return sm.toString();
0395:            }
0396:
0397:            public static String merge(List list) {
0398:                return merge(list, StringPool.COMMA);
0399:            }
0400:
0401:            public static String merge(List list, String delimiter) {
0402:                return merge((Object[]) list.toArray(new Object[list.size()]),
0403:                        delimiter);
0404:            }
0405:
0406:            public static String merge(Object[] array) {
0407:                return merge(array, StringPool.COMMA);
0408:            }
0409:
0410:            public static String merge(Object[] array, String delimiter) {
0411:                if (array == null) {
0412:                    return null;
0413:                }
0414:
0415:                StringMaker sm = new StringMaker();
0416:
0417:                for (int i = 0; i < array.length; i++) {
0418:                    sm.append(String.valueOf(array[i]).trim());
0419:
0420:                    if ((i + 1) != array.length) {
0421:                        sm.append(delimiter);
0422:                    }
0423:                }
0424:
0425:                return sm.toString();
0426:            }
0427:
0428:            public static String randomize(String s) {
0429:                return Randomizer.getInstance().randomize(s);
0430:            }
0431:
0432:            public static String read(ClassLoader classLoader, String name)
0433:                    throws IOException {
0434:
0435:                return read(classLoader, name, false);
0436:            }
0437:
0438:            public static String read(ClassLoader classLoader, String name,
0439:                    boolean all) throws IOException {
0440:
0441:                if (all) {
0442:                    StringMaker sm = new StringMaker();
0443:
0444:                    Enumeration enu = classLoader.getResources(name);
0445:
0446:                    while (enu.hasMoreElements()) {
0447:                        URL url = (URL) enu.nextElement();
0448:
0449:                        InputStream is = url.openStream();
0450:
0451:                        String s = read(is);
0452:
0453:                        if (s != null) {
0454:                            sm.append(s);
0455:                            sm.append(StringPool.NEW_LINE);
0456:                        }
0457:
0458:                        is.close();
0459:                    }
0460:
0461:                    return sm.toString().trim();
0462:                } else {
0463:                    InputStream is = classLoader.getResourceAsStream(name);
0464:
0465:                    String s = read(is);
0466:
0467:                    is.close();
0468:
0469:                    return s;
0470:                }
0471:            }
0472:
0473:            public static String read(InputStream is) throws IOException {
0474:                StringMaker sm = new StringMaker();
0475:
0476:                BufferedReader br = new BufferedReader(
0477:                        new InputStreamReader(is));
0478:
0479:                String line = null;
0480:
0481:                while ((line = br.readLine()) != null) {
0482:                    sm.append(line).append('\n');
0483:                }
0484:
0485:                br.close();
0486:
0487:                return sm.toString().trim();
0488:            }
0489:
0490:            public static String remove(String s, String remove) {
0491:                return remove(s, remove, StringPool.COMMA);
0492:            }
0493:
0494:            public static String remove(String s, String remove,
0495:                    String delimiter) {
0496:                if ((s == null) || (remove == null) || (delimiter == null)) {
0497:                    return null;
0498:                }
0499:
0500:                if (Validator.isNotNull(s) && !s.endsWith(delimiter)) {
0501:                    s += delimiter;
0502:                }
0503:
0504:                StringMaker sm = new StringMaker();
0505:
0506:                sm.append(delimiter);
0507:                sm.append(remove);
0508:                sm.append(delimiter);
0509:
0510:                String drd = sm.toString();
0511:
0512:                sm = new StringMaker();
0513:
0514:                sm.append(remove);
0515:                sm.append(delimiter);
0516:
0517:                String rd = sm.toString();
0518:
0519:                while (contains(s, remove, delimiter)) {
0520:                    int pos = s.indexOf(drd);
0521:
0522:                    if (pos == -1) {
0523:                        if (s.startsWith(rd)) {
0524:                            int x = remove.length() + delimiter.length();
0525:                            int y = s.length();
0526:
0527:                            s = s.substring(x, y);
0528:                        }
0529:                    } else {
0530:                        int x = pos + remove.length() + delimiter.length();
0531:                        int y = s.length();
0532:
0533:                        sm = new StringMaker();
0534:
0535:                        sm.append(s.substring(0, pos));
0536:                        sm.append(s.substring(x, y));
0537:
0538:                        s = sm.toString();
0539:                    }
0540:                }
0541:
0542:                return s;
0543:            }
0544:
0545:            public static String replace(String s, char oldSub, char newSub) {
0546:                return replace(s, oldSub, new Character(newSub).toString());
0547:            }
0548:
0549:            public static String replace(String s, char oldSub, String newSub) {
0550:                if ((s == null) || (newSub == null)) {
0551:                    return null;
0552:                }
0553:
0554:                StringMaker sm = new StringMaker();
0555:
0556:                char[] c = s.toCharArray();
0557:
0558:                for (int i = 0; i < c.length; i++) {
0559:                    if (c[i] == oldSub) {
0560:                        sm.append(newSub);
0561:                    } else {
0562:                        sm.append(c[i]);
0563:                    }
0564:                }
0565:
0566:                return sm.toString();
0567:            }
0568:
0569:            public static String replace(String s, String oldSub, String newSub) {
0570:                if ((s == null) || (oldSub == null) || (newSub == null)) {
0571:                    return null;
0572:                }
0573:
0574:                int y = s.indexOf(oldSub);
0575:
0576:                if (y >= 0) {
0577:                    StringMaker sm = new StringMaker();
0578:
0579:                    int length = oldSub.length();
0580:                    int x = 0;
0581:
0582:                    while (x <= y) {
0583:                        sm.append(s.substring(x, y));
0584:                        sm.append(newSub);
0585:                        x = y + length;
0586:                        y = s.indexOf(oldSub, x);
0587:                    }
0588:
0589:                    sm.append(s.substring(x));
0590:
0591:                    return sm.toString();
0592:                } else {
0593:                    return s;
0594:                }
0595:            }
0596:
0597:            public static String replace(String s, String[] oldSubs,
0598:                    String[] newSubs) {
0599:                if ((s == null) || (oldSubs == null) || (newSubs == null)) {
0600:                    return null;
0601:                }
0602:
0603:                if (oldSubs.length != newSubs.length) {
0604:                    return s;
0605:                }
0606:
0607:                for (int i = 0; i < oldSubs.length; i++) {
0608:                    s = replace(s, oldSubs[i], newSubs[i]);
0609:                }
0610:
0611:                return s;
0612:            }
0613:
0614:            /**
0615:             * Returns a string with replaced values. This method will replace all text
0616:             * in the given string, between the beginning and ending delimiter, with new
0617:             * values found in the given map. For example, if the string contained the
0618:             * text <code>[$HELLO$]</code>, and the beginning delimiter was
0619:             * <code>[$]</code>, and the ending delimiter was <code>$]</code>, and the
0620:             * values map had a key of <code>HELLO</code> that mapped to
0621:             * <code>WORLD</code>, then the replaced string will contain the text
0622:             * <code>[$WORLD$]</code>.
0623:             *
0624:             * @param		s the original string
0625:             * @param		begin the beginning delimiter
0626:             * @param		end the ending delimiter
0627:             * @param		values a map of old and new values
0628:             * @return		a string with replaced values
0629:             */
0630:            public static String replaceValues(String s, String begin,
0631:                    String end, Map values) {
0632:
0633:                if ((s == null) || (begin == null) || (end == null)
0634:                        || (values == null) || (values.size() == 0)) {
0635:
0636:                    return s;
0637:                }
0638:
0639:                StringMaker sm = new StringMaker(s.length());
0640:
0641:                int pos = 0;
0642:
0643:                while (true) {
0644:                    int x = s.indexOf(begin, pos);
0645:                    int y = s.indexOf(end, x + begin.length());
0646:
0647:                    if ((x == -1) || (y == -1)) {
0648:                        sm.append(s.substring(pos, s.length()));
0649:
0650:                        break;
0651:                    } else {
0652:                        sm.append(s.substring(pos, x + begin.length()));
0653:
0654:                        String oldValue = s.substring(x + begin.length(), y);
0655:
0656:                        String newValue = (String) values.get(oldValue);
0657:
0658:                        if (newValue == null) {
0659:                            newValue = oldValue;
0660:                        }
0661:
0662:                        sm.append(newValue);
0663:
0664:                        pos = y;
0665:                    }
0666:                }
0667:
0668:                return sm.toString();
0669:            }
0670:
0671:            public static String reverse(String s) {
0672:                if (s == null) {
0673:                    return null;
0674:                }
0675:
0676:                char[] c = s.toCharArray();
0677:                char[] reverse = new char[c.length];
0678:
0679:                for (int i = 0; i < c.length; i++) {
0680:                    reverse[i] = c[c.length - i - 1];
0681:                }
0682:
0683:                return new String(reverse);
0684:            }
0685:
0686:            public static String safePath(String path) {
0687:                return StringUtil.replace(path, StringPool.DOUBLE_SLASH,
0688:                        StringPool.SLASH);
0689:            }
0690:
0691:            public static String shorten(String s) {
0692:                return shorten(s, 20);
0693:            }
0694:
0695:            public static String shorten(String s, int length) {
0696:                return shorten(s, length, "...");
0697:            }
0698:
0699:            public static String shorten(String s, String suffix) {
0700:                return shorten(s, 20, suffix);
0701:            }
0702:
0703:            public static String shorten(String s, int length, String suffix) {
0704:                if (s == null || suffix == null) {
0705:                    return null;
0706:                }
0707:
0708:                if (s.length() > length) {
0709:                    for (int j = length; j >= 0; j--) {
0710:                        if (Character.isWhitespace(s.charAt(j))) {
0711:                            length = j;
0712:
0713:                            break;
0714:                        }
0715:                    }
0716:
0717:                    StringMaker sm = new StringMaker();
0718:
0719:                    sm.append(s.substring(0, length));
0720:                    sm.append(suffix);
0721:
0722:                    s = sm.toString();
0723:                }
0724:
0725:                return s;
0726:            }
0727:
0728:            public static String[] split(String s) {
0729:                return split(s, StringPool.COMMA);
0730:            }
0731:
0732:            public static String[] split(String s, String delimiter) {
0733:                if (s == null || delimiter == null) {
0734:                    return new String[0];
0735:                }
0736:
0737:                s = s.trim();
0738:
0739:                if (!s.endsWith(delimiter)) {
0740:                    StringMaker sm = new StringMaker();
0741:
0742:                    sm.append(s);
0743:                    sm.append(delimiter);
0744:
0745:                    s = sm.toString();
0746:                }
0747:
0748:                if (s.equals(delimiter)) {
0749:                    return new String[0];
0750:                }
0751:
0752:                List nodeValues = new ArrayList();
0753:
0754:                if (delimiter.equals("\n") || delimiter.equals("\r")) {
0755:                    try {
0756:                        BufferedReader br = new BufferedReader(
0757:                                new StringReader(s));
0758:
0759:                        String line = null;
0760:
0761:                        while ((line = br.readLine()) != null) {
0762:                            nodeValues.add(line);
0763:                        }
0764:
0765:                        br.close();
0766:                    } catch (IOException ioe) {
0767:                        _log.error(ioe.getMessage());
0768:                    }
0769:                } else {
0770:                    int offset = 0;
0771:                    int pos = s.indexOf(delimiter, offset);
0772:
0773:                    while (pos != -1) {
0774:                        nodeValues.add(new String(s.substring(offset, pos)));
0775:
0776:                        offset = pos + delimiter.length();
0777:                        pos = s.indexOf(delimiter, offset);
0778:                    }
0779:                }
0780:
0781:                return (String[]) nodeValues.toArray(new String[nodeValues
0782:                        .size()]);
0783:            }
0784:
0785:            public static boolean[] split(String s, boolean x) {
0786:                return split(s, StringPool.COMMA, x);
0787:            }
0788:
0789:            public static boolean[] split(String s, String delimiter, boolean x) {
0790:                String[] array = split(s, delimiter);
0791:                boolean[] newArray = new boolean[array.length];
0792:
0793:                for (int i = 0; i < array.length; i++) {
0794:                    boolean value = x;
0795:
0796:                    try {
0797:                        value = Boolean.valueOf(array[i]).booleanValue();
0798:                    } catch (Exception e) {
0799:                    }
0800:
0801:                    newArray[i] = value;
0802:                }
0803:
0804:                return newArray;
0805:            }
0806:
0807:            public static double[] split(String s, double x) {
0808:                return split(s, StringPool.COMMA, x);
0809:            }
0810:
0811:            public static double[] split(String s, String delimiter, double x) {
0812:                String[] array = split(s, delimiter);
0813:                double[] newArray = new double[array.length];
0814:
0815:                for (int i = 0; i < array.length; i++) {
0816:                    double value = x;
0817:
0818:                    try {
0819:                        value = Double.parseDouble(array[i]);
0820:                    } catch (Exception e) {
0821:                    }
0822:
0823:                    newArray[i] = value;
0824:                }
0825:
0826:                return newArray;
0827:            }
0828:
0829:            public static float[] split(String s, float x) {
0830:                return split(s, StringPool.COMMA, x);
0831:            }
0832:
0833:            public static float[] split(String s, String delimiter, float x) {
0834:                String[] array = split(s, delimiter);
0835:                float[] newArray = new float[array.length];
0836:
0837:                for (int i = 0; i < array.length; i++) {
0838:                    float value = x;
0839:
0840:                    try {
0841:                        value = Float.parseFloat(array[i]);
0842:                    } catch (Exception e) {
0843:                    }
0844:
0845:                    newArray[i] = value;
0846:                }
0847:
0848:                return newArray;
0849:            }
0850:
0851:            public static int[] split(String s, int x) {
0852:                return split(s, StringPool.COMMA, x);
0853:            }
0854:
0855:            public static int[] split(String s, String delimiter, int x) {
0856:                String[] array = split(s, delimiter);
0857:                int[] newArray = new int[array.length];
0858:
0859:                for (int i = 0; i < array.length; i++) {
0860:                    int value = x;
0861:
0862:                    try {
0863:                        value = Integer.parseInt(array[i]);
0864:                    } catch (Exception e) {
0865:                    }
0866:
0867:                    newArray[i] = value;
0868:                }
0869:
0870:                return newArray;
0871:            }
0872:
0873:            public static long[] split(String s, long x) {
0874:                return split(s, StringPool.COMMA, x);
0875:            }
0876:
0877:            public static long[] split(String s, String delimiter, long x) {
0878:                String[] array = split(s, delimiter);
0879:                long[] newArray = new long[array.length];
0880:
0881:                for (int i = 0; i < array.length; i++) {
0882:                    long value = x;
0883:
0884:                    try {
0885:                        value = Long.parseLong(array[i]);
0886:                    } catch (Exception e) {
0887:                    }
0888:
0889:                    newArray[i] = value;
0890:                }
0891:
0892:                return newArray;
0893:            }
0894:
0895:            public static short[] split(String s, short x) {
0896:                return split(s, StringPool.COMMA, x);
0897:            }
0898:
0899:            public static short[] split(String s, String delimiter, short x) {
0900:                String[] array = split(s, delimiter);
0901:                short[] newArray = new short[array.length];
0902:
0903:                for (int i = 0; i < array.length; i++) {
0904:                    short value = x;
0905:
0906:                    try {
0907:                        value = Short.parseShort(array[i]);
0908:                    } catch (Exception e) {
0909:                    }
0910:
0911:                    newArray[i] = value;
0912:                }
0913:
0914:                return newArray;
0915:            }
0916:
0917:            public static boolean startsWith(String s, char begin) {
0918:                return startsWith(s, (new Character(begin)).toString());
0919:            }
0920:
0921:            public static boolean startsWith(String s, String start) {
0922:                if ((s == null) || (start == null)) {
0923:                    return false;
0924:                }
0925:
0926:                if (start.length() > s.length()) {
0927:                    return false;
0928:                }
0929:
0930:                String temp = s.substring(0, start.length());
0931:
0932:                if (temp.equalsIgnoreCase(start)) {
0933:                    return true;
0934:                } else {
0935:                    return false;
0936:                }
0937:            }
0938:
0939:            public static String stripBetween(String s, String begin, String end) {
0940:                if ((s == null) || (begin == null) || (end == null)) {
0941:                    return s;
0942:                }
0943:
0944:                StringMaker sm = new StringMaker(s.length());
0945:
0946:                int pos = 0;
0947:
0948:                while (true) {
0949:                    int x = s.indexOf(begin, pos);
0950:                    int y = s.indexOf(end, x + begin.length());
0951:
0952:                    if ((x == -1) || (y == -1)) {
0953:                        sm.append(s.substring(pos, s.length()));
0954:
0955:                        break;
0956:                    } else {
0957:                        sm.append(s.substring(pos, x));
0958:
0959:                        pos = y + end.length();
0960:                    }
0961:                }
0962:
0963:                return sm.toString();
0964:            }
0965:
0966:            public static String trim(String s) {
0967:                return trim(s, null);
0968:            }
0969:
0970:            public static String trim(String s, char c) {
0971:                return trim(s, new char[] { c });
0972:            }
0973:
0974:            public static String trim(String s, char[] exceptions) {
0975:                if (s == null) {
0976:                    return null;
0977:                }
0978:
0979:                char[] charArray = s.toCharArray();
0980:
0981:                int len = charArray.length;
0982:
0983:                int x = 0;
0984:                int y = charArray.length;
0985:
0986:                for (int i = 0; i < len; i++) {
0987:                    char c = charArray[i];
0988:
0989:                    if (_isTrimable(c, exceptions)) {
0990:                        x = i + 1;
0991:                    } else {
0992:                        break;
0993:                    }
0994:                }
0995:
0996:                for (int i = len - 1; i >= 0; i--) {
0997:                    char c = charArray[i];
0998:
0999:                    if (_isTrimable(c, exceptions)) {
1000:                        y = i;
1001:                    } else {
1002:                        break;
1003:                    }
1004:                }
1005:
1006:                if ((x != 0) || (y != len)) {
1007:                    return s.substring(x, y);
1008:                } else {
1009:                    return s;
1010:                }
1011:            }
1012:
1013:            public static String trimLeading(String s) {
1014:                return trimLeading(s, null);
1015:            }
1016:
1017:            public static String trimLeading(String s, char c) {
1018:                return trimLeading(s, new char[] { c });
1019:            }
1020:
1021:            public static String trimLeading(String s, char[] exceptions) {
1022:                if (s == null) {
1023:                    return null;
1024:                }
1025:
1026:                char[] charArray = s.toCharArray();
1027:
1028:                int len = charArray.length;
1029:
1030:                int x = 0;
1031:                int y = charArray.length;
1032:
1033:                for (int i = 0; i < len; i++) {
1034:                    char c = charArray[i];
1035:
1036:                    if (_isTrimable(c, exceptions)) {
1037:                        x = i + 1;
1038:                    } else {
1039:                        break;
1040:                    }
1041:                }
1042:
1043:                if ((x != 0) || (y != len)) {
1044:                    return s.substring(x, y);
1045:                } else {
1046:                    return s;
1047:                }
1048:            }
1049:
1050:            public static String trimTrailing(String s) {
1051:                return trimTrailing(s, null);
1052:            }
1053:
1054:            public static String trimTrailing(String s, char c) {
1055:                return trimTrailing(s, new char[] { c });
1056:            }
1057:
1058:            public static String trimTrailing(String s, char[] exceptions) {
1059:                if (s == null) {
1060:                    return null;
1061:                }
1062:
1063:                char[] charArray = s.toCharArray();
1064:
1065:                int len = charArray.length;
1066:
1067:                int x = 0;
1068:                int y = charArray.length;
1069:
1070:                for (int i = len - 1; i >= 0; i--) {
1071:                    char c = charArray[i];
1072:
1073:                    if (_isTrimable(c, exceptions)) {
1074:                        y = i;
1075:                    } else {
1076:                        break;
1077:                    }
1078:                }
1079:
1080:                if ((x != 0) || (y != len)) {
1081:                    return s.substring(x, y);
1082:                } else {
1083:                    return s;
1084:                }
1085:            }
1086:
1087:            public static String upperCase(String s) {
1088:                if (s == null) {
1089:                    return null;
1090:                } else {
1091:                    return s.toUpperCase();
1092:                }
1093:            }
1094:
1095:            public static String upperCaseFirstLetter(String s) {
1096:                char[] chars = s.toCharArray();
1097:
1098:                if (chars[0] >= 97 && chars[0] <= 122) {
1099:                    chars[0] = (char) ((int) chars[0] - 32);
1100:                }
1101:
1102:                return new String(chars);
1103:            }
1104:
1105:            public static String wrap(String text) {
1106:                return wrap(text, 80, "\n");
1107:            }
1108:
1109:            public static String wrap(String text, int width,
1110:                    String lineSeparator) {
1111:                if (text == null) {
1112:                    return null;
1113:                }
1114:
1115:                StringMaker sm = new StringMaker();
1116:
1117:                try {
1118:                    BufferedReader br = new BufferedReader(new StringReader(
1119:                            text));
1120:
1121:                    String s = StringPool.BLANK;
1122:
1123:                    while ((s = br.readLine()) != null) {
1124:                        if (s.length() == 0) {
1125:                            sm.append(lineSeparator);
1126:                        } else {
1127:                            String[] tokens = s.split(StringPool.SPACE);
1128:                            boolean firstWord = true;
1129:                            int curLineLength = 0;
1130:
1131:                            for (int i = 0; i < tokens.length; i++) {
1132:                                if (!firstWord) {
1133:                                    sm.append(StringPool.SPACE);
1134:                                    curLineLength++;
1135:                                }
1136:
1137:                                if (firstWord) {
1138:                                    sm.append(lineSeparator);
1139:                                }
1140:
1141:                                sm.append(tokens[i]);
1142:
1143:                                curLineLength += tokens[i].length();
1144:
1145:                                if (curLineLength >= width) {
1146:                                    firstWord = true;
1147:                                    curLineLength = 0;
1148:                                } else {
1149:                                    firstWord = false;
1150:                                }
1151:                            }
1152:                        }
1153:                    }
1154:                } catch (IOException ioe) {
1155:                    _log.error(ioe.getMessage());
1156:                }
1157:
1158:                return sm.toString();
1159:            }
1160:
1161:            private static boolean _isTrimable(char c, char[] exceptions) {
1162:                if ((exceptions != null) && (exceptions.length > 0)) {
1163:                    for (int i = 0; i < exceptions.length; i++) {
1164:                        if (c == exceptions[i]) {
1165:                            return false;
1166:                        }
1167:                    }
1168:                }
1169:
1170:                return Character.isWhitespace(c);
1171:            }
1172:
1173:            private static Log _log = LogFactoryUtil.getLog(StringUtil.class);
1174:
1175:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.