]> git.basschouten.com Git - openhab-addons.git/blob
c48704b0c01fed2ba10e9955d7a4e5f81f943023
[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.magentatv.internal;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
19  * {@link MagentaTVUtil} implements some helper functions.
20  *
21  * @author Markus Michels - Initial contribution
22  */
23 @NonNullByDefault
24 public class MagentaTVUtil {
25     public static String getString(@Nullable String value) {
26         return value != null ? value : "";
27     }
28
29     public static String substringBefore(@Nullable String string, String pattern) {
30         if (string != null) {
31             int pos = string.indexOf(pattern);
32             if (pos > 0) {
33                 return string.substring(0, pos);
34             }
35         }
36         return "";
37     }
38
39     public static String substringBeforeLast(@Nullable String string, String pattern) {
40         if (string != null) {
41             int pos = string.lastIndexOf(pattern);
42             if (pos > 0) {
43                 return string.substring(0, pos);
44             }
45         }
46         return "";
47     }
48
49     public static String substringAfter(@Nullable String string, String pattern) {
50         if (string != null) {
51             int pos = string.indexOf(pattern);
52             if (pos != -1) {
53                 return string.substring(pos + pattern.length());
54             }
55         }
56         return "";
57     }
58
59     public static String substringAfterLast(@Nullable String string, String pattern) {
60         if (string != null) {
61             int pos = string.lastIndexOf(pattern);
62             if (pos != -1) {
63                 return string.substring(pos + pattern.length());
64             }
65         }
66         return "";
67     }
68
69     public static String substringBetween(@Nullable String string, String begin, String end) {
70         if (string != null) {
71             int s = string.indexOf(begin);
72             if (s != -1) {
73                 // The end tag might be included before the start tag, e.g.
74                 // when using "http://" and ":" to get the IP from http://192.168.1.1:8081/xxx
75                 // therefore make it 2 steps
76                 String result = string.substring(s + begin.length());
77                 return substringBefore(result, end);
78             }
79         }
80         return "";
81     }
82 }