com.reandroid.utils.StringsUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ARSCLib Show documentation
Show all versions of ARSCLib Show documentation
Android binary resources read/write library
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.utils;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
public class StringsUtil {
public static int compare(String[] strings1, String[] strings2){
if(strings1 == strings2){
return 0;
}
if(strings1 == null){
return 1;
}
if(strings2 == null){
return -1;
}
int length1 = strings1.length;
int length2 = strings2.length;
if(length1 == 0 && length2 == 0){
return 0;
}
if(length1 == 0){
return 1;
}
if(length2 == 0){
return -1;
}
int length = length1;
if(length > length2){
length = length2;
}
for(int i = 0; i < length; i++){
String s1 = strings1[i];
String s2 = strings2[i];
int c = compareStrings(s1, s2);
if(c != 0){
return c;
}
}
return Integer.compare(length1, length2);
}
public static String[] split(String text, char search){
return split(text, search, true);
}
public static String[] split(String text, char search, boolean skipConsecutive) {
if(text == null || text.length() == 0){
return new String[0];
}
int count = countChar(text, search, skipConsecutive);
if(count == 0){
return new String[]{text};
}
String[] results = new String[count + 1];
int index = 0;
StringBuilder builder = new StringBuilder();
char[] chars = text.toCharArray();
boolean previousMatch = false;
for(char ch : chars){
if(ch == search){
if(!previousMatch || !skipConsecutive){
previousMatch = true;
results[index] = builder.toString();
builder = new StringBuilder();
index++;
}
}else {
previousMatch = false;
builder.append(ch);
}
}
if(index < results.length){
results[index] = builder.toString();
}
return results;
}
public static int countChar(String text, char search, boolean skipConsecutive) {
if(text == null || text.length() == 0){
return 0;
}
char[] chars = text.toCharArray();
int result = 0;
boolean previousMatch = false;
for(char ch : chars){
if(ch == search){
if(!previousMatch || !skipConsecutive){
result ++;
previousMatch = true;
}
}else {
previousMatch = false;
}
}
return result;
}
public static String toString(Collection> collection){
return toString(collection, MAX_STRING_APPEND);
}
public static String toString(Collection> collection, int max){
if(collection == null){
return "null";
}
if(max < 0){
max = collection.size();
}
StringBuilder builder = new StringBuilder();
builder.append("size=");
builder.append(collection.size());
builder.append(" [");
int count = 0;
Iterator> iterator = collection.iterator();
while (iterator.hasNext() && count < max){
if(count != 0){
builder.append(", ");
}
builder.append(iterator.next());
count ++;
}
if(count < collection.size()){
builder.append(" ... ");
}
builder.append(']');
return builder.toString();
}
public static String toString(Object[] elements){
return toString(elements, MAX_STRING_APPEND);
}
public static String toString(Object[] elements, int max){
if(elements == null){
return "null";
}
StringBuilder builder = new StringBuilder();
builder.append("length=");
builder.append(elements.length);
builder.append(" [");
if(max < 0 || max > elements.length){
max = elements.length;
}
int count;
for (count = 0; count < max; count++){
if(count != 0){
builder.append(", ");
}
builder.append(elements[count]);
}
if(count < elements.length){
builder.append(" ... ");
}
builder.append(']');
return builder.toString();
}
public static String emptyToNull(String text){
if(isEmpty(text)){
return null;
}
return text;
}
public static boolean isEmpty(String text){
return text == null || text.length() == 0;
}
public static boolean isWhiteSpace(String text){
return text == null || text.trim().length() == 0;
}
public static String toUpperCase(String str){
if(str == null || str.length() == 0){
return str;
}
char[] chars = str.toCharArray();
boolean changed = false;
for(int i = 0; i < chars.length; i++){
char ch = chars[i];
char lower = toUpperCase(ch);
if(ch == lower){
continue;
}
chars[i] = lower;
changed = true;
}
if(!changed){
return str;
}
return new String(chars);
}
public static char toUpperCase(char ch){
if(ch > 'z' || ch < 'a'){
return ch;
}
int i = ch - 'a';
return (char) (i + 'A');
}
public static String toLowercase(String str){
char[] chars = str.toCharArray();
boolean changed = false;
for(int i = 0; i < chars.length; i++){
char ch = chars[i];
char lower = toLowercase(ch);
if(ch == lower){
continue;
}
chars[i] = lower;
changed = true;
}
if(!changed){
return str;
}
return new String(chars);
}
public static char toLowercase(char ch){
if(ch > 'Z' || ch < 'A'){
return ch;
}
int i = ch - 'A';
return (char) (i + 'a');
}
public static void toStringSort(List> itemList){
if(itemList == null || itemList.size() < 2){
return;
}
Comparator
© 2015 - 2025 Weber Informatics LLC | Privacy Policy