001package com.pi4j.util;
002
003/*
004 * #%L
005 * **********************************************************************
006 * ORGANIZATION  :  Pi4J
007 * PROJECT       :  Pi4J :: Java Library (Core)
008 * FILENAME      :  StringUtil.java
009 *
010 * This file is part of the Pi4J project. More information about
011 * this project can be found here:  https://www.pi4j.com/
012 * **********************************************************************
013 * %%
014 * Copyright (C) 2012 - 2019 Pi4J
015 * %%
016 * This program is free software: you can redistribute it and/or modify
017 * it under the terms of the GNU Lesser General Public License as
018 * published by the Free Software Foundation, either version 3 of the
019 * License, or (at your option) any later version.
020 *
021 * This program is distributed in the hope that it will be useful,
022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024 * GNU General Lesser Public License for more details.
025 *
026 * You should have received a copy of the GNU General Lesser Public
027 * License along with this program.  If not, see
028 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
029 * #L%
030 */
031
032
033public class StringUtil {
034
035    public static final String EMPTY = "";
036    public static final char DEFAULT_PAD_CHAR = ' ';
037
038    public static boolean isNullOrEmpty(String data, boolean trim){
039        if(data == null)
040            return true;
041
042        // trim if requested
043        String test = data;
044        if(trim)
045            test = data.trim();
046
047        return (test.length() <= 0);
048    }
049
050    public static boolean isNullOrEmpty(String data){
051        return isNullOrEmpty(data, false);
052    }
053
054    public static boolean isNotNullOrEmpty(String data){
055        return isNotNullOrEmpty(data, false);
056    }
057
058    public static boolean isNotNullOrEmpty(String data, boolean trim){
059        return !(isNullOrEmpty(data, trim));
060    }
061
062    public static boolean contains(String source, String target)  {
063        return (null != source && null != target && source.contains(target));
064    }
065
066    public static boolean contains(String source, String[] targets)  {
067        if (null != source && null != targets) {
068            for(String target : targets) {
069                if (source.contains(target)) {
070                    return true;
071                }
072            }
073        }
074        return false;
075    }
076
077    public static boolean contains(String[] sources, String target)  {
078        if (null != sources && null != target) {
079            for (String source : sources) {
080                if(contains(source, target))
081                    return true;
082            }
083        }
084        return false;
085    }
086
087    public static boolean contains(String[] sources, String[] targets)  {
088        if (null != sources && null != targets) {
089            for (String source : sources) {
090                if(contains(source, targets))
091                    return true;
092            }
093        }
094        return false;
095    }
096
097    public static String create(int length)  {
098        return create(DEFAULT_PAD_CHAR, length);
099    }
100
101    public static String create(char c, int length)  {
102        StringBuilder sb = new StringBuilder(length);
103        for(int index = 0; index < length; index++)
104            sb.append(c);
105        return sb.toString();
106    }
107
108    public static String create(String s, int length)  {
109        StringBuilder sb = new StringBuilder(length * s.length());
110        for(int index = 0; index < length; index++)
111            sb.append(s);
112        return sb.toString();
113    }
114
115    public static String repeat(char c, int length)  {
116        return create(c, length);
117    }
118
119    public static String padLeft(String data, int length)  {
120        return padLeft(data, DEFAULT_PAD_CHAR, length);
121    }
122
123    public static String padLeft(String data, char pad, int length)  {
124        StringBuilder sb = new StringBuilder(data.length() + length);
125        for(int index = 0; index < length; index++)
126            sb.append(pad);
127        sb.append(data);
128        return sb.toString();
129    }
130
131    public static String padLeft(String data, String pad, int length)  {
132        StringBuilder sb = new StringBuilder(data.length() + (length * pad.length()));
133        for(int index = 0; index < length; index++)
134            sb.append(pad);
135        sb.append(data);
136        return sb.toString();
137    }
138
139    public static String padRight(String data, int length)  {
140        return padRight(data, DEFAULT_PAD_CHAR, length);
141    }
142
143    public static String padRight(String data, char pad, int length)  {
144        StringBuilder sb = new StringBuilder(data.length() + length);
145        sb.append(data);
146        for(int index = 0; index < length; index++)
147            sb.append(pad);
148        return sb.toString();
149    }
150
151    public static String padRight(String data, String pad, int length)  {
152        StringBuilder sb = new StringBuilder(data.length() + (length * pad.length()));
153        sb.append(data);
154        for(int index = 0; index < length; index++)
155            sb.append(pad);
156        return sb.toString();
157    }
158
159    public static String pad(String data, int length)  {
160        return pad(data, DEFAULT_PAD_CHAR, length);
161    }
162
163    public static String pad(String data, char pad, int length)  {
164        return create(pad, length) + data + create(pad, length);
165    }
166
167    public static String pad(String data, String pad, int length)  {
168        return create(pad, length) + data + create(pad, length);
169    }
170
171    public static String padCenter(String data, int length) {
172        return padCenter(data, DEFAULT_PAD_CHAR, length);
173    }
174
175    public static String padCenter(String data, char pad, int length) {
176        if(data.length() < length) {
177            int needed = length - data.length();
178            int padNeeded = needed / 2;
179            StringBuilder result = new StringBuilder();
180            result.append(create(pad, padNeeded));
181            result.append(data);
182            result.append(create(pad, padNeeded));
183            int remaining = length - result.length();
184            result.append(create(pad, remaining));
185            return result.toString();
186        }
187        return data;
188    }
189
190    public static String trimLeft(String data)  {
191        return trimLeft(data, DEFAULT_PAD_CHAR);
192    }
193
194    public static String trimLeft(String data, char trim)  {
195        for(int index = 0; index < data.length(); index++)
196            if(!(data.charAt(index) == trim))
197                return data.substring(index);
198        return EMPTY;
199    }
200
201    public static String trimRight(String data)  {
202        return trimRight(data, DEFAULT_PAD_CHAR);
203    }
204
205    public static String trimRight(String data, char trim)  {
206        int count = 0;
207        for(int index = data.length(); index > 0; index--)
208            if(data.charAt(index-1) == trim)
209                count++;
210            else
211                return data.substring(0, data.length() - count);
212        return EMPTY;
213    }
214
215    public static String trim(String data)  {
216        return trim(data, DEFAULT_PAD_CHAR);
217    }
218
219    public static String trim(String data, char trim)  {
220        String result = trimLeft(data, trim);
221        return trimRight(result, trim);
222    }
223
224    public static String center(String text, int length){
225        String out = String.format("%"+length+"s%s%"+length+"s", "",text,"");
226        float mid = (out.length()/2);
227        float start = mid - (length/2);
228        float end = start + length;
229        return out.substring((int) start, (int) end);
230    }
231
232    public static String concat(String ... data)  {
233        StringBuilder sb = new StringBuilder();
234        for(String d : data){
235            sb.append(d);
236        }
237        return sb.toString();
238    }
239}