codec2_talkie/codec2talkie/src/main/java/com/radio/codec2talkie/tools/TextTools.java

67 wiersze
1.8 KiB
Java
Czysty Zwykły widok Historia

2022-07-06 12:15:11 +00:00
package com.radio.codec2talkie.tools;
2022-08-14 16:52:42 +00:00
import android.util.Log;
2022-08-20 12:52:10 +00:00
import java.nio.ByteBuffer;
2022-08-14 16:52:42 +00:00
import java.util.Arrays;
2022-09-03 14:38:29 +00:00
import java.util.Objects;
2022-08-14 16:52:42 +00:00
2022-07-06 12:15:11 +00:00
public class TextTools {
public static String addZeroWidthSpaces(String text) {
return text.replaceAll(".(?!$)", "$0\u200b");
}
2022-07-23 14:23:57 +00:00
public static int countChars(String text, char ch) {
int count = 0;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == ch) {
count++;
}
}
return count;
}
2022-08-14 16:52:42 +00:00
public static String stripNulls(String text) {
int pos = text.indexOf('\0');
if (pos == -1) return text;
return text.substring(0, pos);
}
public static byte[] stripNulls(byte[] data) {
int i = 0;
for (byte b : data) {
if (b == 0) break;
i++;
}
if (i == data.length) return data;
return Arrays.copyOf(data, i);
}
2022-08-20 12:52:10 +00:00
public static String getString(ByteBuffer byteBuffer) {
StringBuilder result = new StringBuilder();
if (byteBuffer.position() > 0) {
byteBuffer.flip();
while (byteBuffer.hasRemaining()) {
char c = (char)byteBuffer.get();
if (c == '\n') {
break;
}
result.append(c);
}
byteBuffer.compact();
}
return result.toString();
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
2022-07-06 12:15:11 +00:00
}