|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
StringUtil.java | 0% | 0% | 0% | 0% |
|
1 | /* | |
2 | * Copyright (c) 2002-2003 by OpenSymphony | |
3 | * All rights reserved. | |
4 | */ | |
5 | package com.opensymphony.oscache.util; | |
6 | ||
7 | import java.util.ArrayList; | |
8 | import java.util.List; | |
9 | ||
10 | /** | |
11 | * Provides common utility methods for handling strings. | |
12 | * | |
13 | * @author <a href="mailto:chris@swebtec.com">Chris Miller</a> | |
14 | */ | |
15 | public class StringUtil { | |
16 | /** | |
17 | * Splits a string into substrings based on the supplied delimiter | |
18 | * character. Each extracted substring will be trimmed of leading | |
19 | * and trailing whitespace. | |
20 | * | |
21 | * @param str The string to split | |
22 | * @param delimiter The character that delimits the string | |
23 | * @return A string array containing the resultant substrings | |
24 | */ | |
25 | 0 | public static List split(String str, char delimiter) { |
26 | // return no groups if we have an empty string | |
27 | 0 | if ((str == null) || "".equals(str)) { |
28 | 0 | return new ArrayList(); |
29 | } | |
30 | ||
31 | 0 | ArrayList parts = new ArrayList(); |
32 | 0 | int currentIndex; |
33 | 0 | int previousIndex = 0; |
34 | ||
35 | 0 | while ((currentIndex = str.indexOf(delimiter, previousIndex)) > 0) { |
36 | 0 | String part = str.substring(previousIndex, currentIndex).trim(); |
37 | 0 | parts.add(part); |
38 | 0 | previousIndex = currentIndex + 1; |
39 | } | |
40 | ||
41 | 0 | parts.add(str.substring(previousIndex, str.length()).trim()); |
42 | ||
43 | 0 | return parts; |
44 | } | |
45 | } |
|