2017-03-10 14:53:19 +01:00
|
|
|
/* Copyright (C) 2017 Carsten Pfeiffer, João Paulo Barraca
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2017-01-24 02:44:30 +01:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
2017-03-02 00:27:54 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
|
2017-01-24 02:44:30 +01:00
|
|
|
public class StringUtils {
|
|
|
|
|
|
|
|
public static String truncate(String s, int maxLength){
|
2017-03-02 00:27:54 +01:00
|
|
|
if (s == null) {
|
|
|
|
return "";
|
|
|
|
}
|
2017-01-24 02:44:30 +01:00
|
|
|
|
2017-03-02 00:27:54 +01:00
|
|
|
int length = Math.min(s.length(), maxLength);
|
|
|
|
if(length < 0) {
|
2017-01-24 02:44:30 +01:00
|
|
|
return "";
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|
2017-01-24 02:44:30 +01:00
|
|
|
|
|
|
|
return s.substring(0, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String pad(String s, int length){
|
|
|
|
return pad(s, length, ' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String pad(String s, int length, char padChar){
|
2017-03-02 00:27:54 +01:00
|
|
|
while(s.length() < length) {
|
2017-01-24 02:44:30 +01:00
|
|
|
s += padChar;
|
2017-03-02 00:27:54 +01:00
|
|
|
}
|
2017-01-24 02:44:30 +01:00
|
|
|
return s;
|
|
|
|
}
|
2017-03-02 00:27:54 +01:00
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public static String formatSender(String sender, Context context) {
|
|
|
|
if (sender == null || sender.length() == 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return context.getString(R.string.StringUtils_sender, sender);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
public static String getFirstOf(String first, String second) {
|
|
|
|
if (first != null && first.length() > 0) {
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
if (second != null) {
|
|
|
|
return second;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2017-01-24 02:44:30 +01:00
|
|
|
}
|