]> git.basschouten.com Git - openhab-addons.git/blob
f34181cddd968afb0e40cab9f1aaf82dd929609f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.gardena.internal.util;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
19  * Some String operations from commons lang.
20  *
21  * @author Gerhard Riegler - Initial contribution
22  */
23 @NonNullByDefault
24 public class StringUtils {
25     /**
26      * Gets the substring before the first occurrence of a separator.
27      */
28     public static @Nullable String substringBefore(@Nullable String str, String separator) {
29         if (str != null && !str.isEmpty()) {
30             int pos = str.indexOf(separator);
31             return pos == -1 ? str : str.substring(0, pos);
32         } else {
33             return str;
34         }
35     }
36
37     /**
38      * Gets the substring before the last occurrence of a separator.
39      */
40     public static @Nullable String substringBeforeLast(@Nullable String str, String separator) {
41         if (str != null && !str.isEmpty()) {
42             int pos = str.lastIndexOf(separator);
43             return pos == -1 ? str : str.substring(0, pos);
44         } else {
45             return str;
46         }
47     }
48
49     /**
50      * Gets the substring after the last occurrence of a separator.
51      */
52     public static @Nullable String substringAfterLast(@Nullable String str, String separator) {
53         if (str != null && !str.isEmpty()) {
54             int pos = str.lastIndexOf(separator);
55             return pos != -1 && pos != str.length() - separator.length() ? str.substring(pos + separator.length()) : "";
56         } else {
57             return str;
58         }
59     }
60 }