Untitled
unknown
plain_text
2 years ago
15 kB
17
Indexable
java
/**
* CustomStringLibrary is a library to work with strings in Java without using the built-in string library.
*/
public class CustomStringLibrary {
/**
* Returns the length of the input string.
*
* @param str The input string
* @return The length of the input string
*/
public static int length(String str) {
int count = 0;
for (char c : str.toCharArray()) {
count++;
}
return count;
}
/**
* Concatenates two strings together.
*
* @param str1 The first string
* @param str2 The second string
* @return The concatenated string
*/
public static String concat(String str1, String str2) {
char[] result = new char[length(str1) + length(str2)];
int index = 0;
for (char c : str1.toCharArray()) {
result[index] = c;
index++;
}
for (char c : str2.toCharArray()) {
result[index] = c;
index++;
}
return new String(result);
}
/**
* Returns a substring of the input string.
*
* @param str The input string
* @param start The starting index of the substring (inclusive)
* @param end The ending index of the substring (exclusive)
* @return The substring of the input string
*/
public static String substring(String str, int start, int end) {
if (start < 0 || start >= end || start >= length(str) || end > length(str)) {
throw new IllegalArgumentException("Invalid start and end indices");
}
char[] result = new char[end - start];
int index = 0;
char[] charArray = str.toCharArray();
for (int i = start; i < end; i++) {
result[index] = charArray[i];
index++;
}
return new String(result);
}
/**
* Checks if the input string is empty or not.
*
* @param str The input string
* @return True if the input string is empty, false otherwise
*/
public static boolean isEmpty(String str) {
return length(str) == 0;
}
/**
* Replaces all occurrences of a specified old character with a new character in the input string.
*
* @param str The input string
* @param oldChar The old character to be replaced
* @param newChar The new character to replace the old character with
* @return The replaced string
*/
public static String replace(String str, char oldChar, char newChar) {
char[] result = new char[length(str)];
int index = 0;
for (char c : str.toCharArray()) {
if (c == oldChar) {
result[index] = newChar;
} else {
result[index] = c;
}
index++;
}
return new String(result);
}
/**
* Splits the input string into an array of substrings using the specified delimiter.
*
* @param str The input string
* @param delimiter The delimiter to split the string
* @return An array of substrings
*/
public static String[] split(String str, String delimiter) {
int count = 0;
for (int i = 0; i < length(str) - length(delimiter) + 1; i++) {
if (substring(str, i, i + length(delimiter)).equals(delimiter)) {
count++;
}
}
String[] result = new String[count + 1];
int index = 0;
int startIndex = 0;
int endIndex = 0;
while (endIndex < length(str)) {
if (substring(str, endIndex, endIndex + length(delimiter)).equals(delimiter)) {
result[index] = substring(str, startIndex, endIndex);
index++;
startIndex = endIndex + length(delimiter);
endIndex = startIndex;
} else {
endIndex++;
}
}
result[index] = substring(str, startIndex, endIndex);
return result;
}
/**
* Joins the elements of the input array into a single string using the specified delimiter.
*
* @param arr The input array
* @param delimiter The delimiter to join the elements
* @return The joined string
*/
public static String join(String[] arr, String delimiter) {
if (arr.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder();
sb.append(arr[0]);
for (int i = 1; i < arr.length; i++) {
sb.append(delimiter).append(arr[i]);
}
return sb.toString();
}
/**
* Checks if two strings are equal regardless of the case sensitivity.
*
* @param str1 The first string
* @param str2 The second string
* @return True if the two strings are equal, false otherwise
*/
public static boolean equalsIgnoreCase(String str1, String str2) {
if (length(str1) != length(str2)) {
return false;
}
for (int i = 0; i < length(str1); i++) {
if (tolowercase(str1.charAt(i)) != tolowercase(str2.charAt(i))) {
return false;
}
}
return true;
}
/**
* Returns the index of the first occurrence of a specified character in the input string.
*
* @param str The input string
* @param ch The character to be searched for
* @return The index of the first occurrence of the character, -1 if not found
*/
public static int indexOf(String str, char ch) {
for (int i = 0; i < length(str); i++) {
if (str.charAt(i) == ch) {
return i;
}
}
return -1;
}
/**
* Formats a string using the specified format and arguments.
*
* @param format The format string
* @param arguments The arguments to be substituted in the format string
* @return The formatted string
*/
public static String format(String format, Object... arguments) {
StringBuilder sb = new StringBuilder();
int index = 0;
int argumentIndex = 0;
while (index < length(format)) {
if (format.charAt(index) == '{' && index + 1 < length(format) && format.charAt(index + 1) == '}') {
if (argumentIndex >= arguments.length) {
throw new IllegalArgumentException("Insufficient arguments for format string");
}
sb.append(arguments[argumentIndex]);
argumentIndex++;
index += 2;
} else {
sb.append(format.charAt(index));
index++;
}
}
return sb.toString();
}
/**
* Checks if the input string starts with the specified prefix.
*
* @param str The input string
* @param prefix The prefix to be checked
* @return True if the input string starts with the prefix, false otherwise
*/
public static boolean startsWith(String str, String prefix) {
if (length(prefix) > length(str)) {
return false;
}
for (int i = 0; i < length(prefix); i++) {
if (str.charAt(i) != prefix.charAt(i)) {
return false;
}
}
return true;
}
/**
* Checks if the input string ends with the specified suffix.
*
* @param str The input string
* @param suffix The suffix to be checked
* @return True if the input string ends with the suffix, false otherwise
*/
public static boolean endsWith(String str, String suffix) {
if (length(suffix) > length(str)) {
return false;
}
int strIndex = length(str) - 1;
int suffixIndex = length(suffix) - 1;
while (suffixIndex >= 0) {
if (str.charAt(strIndex) != suffix.charAt(suffixIndex)) {
return false;
}
suffixIndex--;
strIndex--;
}
return true;
}
/**
* Converts the input string to lowercase.
*
* @param str The input string
* @return The lowercase string
*/
public static String toLowercase(String str) {
char[] result = new char[length(str)];
int index = 0;
for (char c : str.toCharArray()) {
if (c >= 'A' && c <= 'Z') {
result[index] = (char) (c - 'A' + 'a');
} else {
result[index] = c;
}
index++;
}
return new String(result);
}
/**
* Converts the input string to uppercase.
*
* @param str The input string
* @return The uppercase string
*/
public static String toUppercase(String str) {
char[] result = new char[length(str)];
int index = 0;
for (char c : str.toCharArray()) {
if (c >= 'a' && c <= 'z') {
result[index] = (char) (c - 'a' + 'A');
} else {
result[index] = c;
}
index++;
}
return new String(result);
}
/**
* Removes leading and trailing whitespaces from the input string.
*
* @param str The input string
* @return The trimmed string
*/
public static String trim(String str) {
int startIndex = 0;
while (startIndex < length(str) && str.charAt(startIndex) == ' ') {
startIndex++;
}
int endIndex = length(str) - 1;
while (endIndex >= 0 && str.charAt(endIndex) == ' ') {
endIndex--;
}
return substring(str, startIndex, endIndex + 1);
}
/**
* Compares two strings lexicographically.
*
* @param str1 The first string
* @param str2 The second string
* @return The value 0 if the two strings are equal; a value less than 0 if the first string is lexicographically less
* than the second string; a value greater than 0 if the first string is lexicographically greater than the second
* string
*/
public static int compareTo(String str1, String str2) {
int len1 = length(str1);
int len2 = length(str2);
int lim = Math.min(len1, len2);
char[] v1 = str1.toCharArray();
char[] v2 = str2.toCharArray();
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
}
Тесты для каждого из методов библиотеки:
java
import org.junit.Assert;
import org.junit.Test;
public class CustomStringLibraryTest {
@Test
public void testLength() {
String str = "Hello, World!";
int expected = 13;
int actual = CustomStringLibrary.length(str);
Assert.assertEquals(expected, actual);
}
@Test
public void testConcat() {
String str1 = "Hello,";
String str2 = " World!";
String expected = "Hello, World!";
String actual = CustomStringLibrary.concat(str1, str2);
Assert.assertEquals(expected, actual);
}
@Test
public void testSubstring() {
String str = "Hello, World!";
String expected = "World";
String actual = CustomStringLibrary.substring(str, 7, 12);
Assert.assertEquals(expected, actual);
}
@Test
public void testIsEmpty() {
String str = "";
boolean expected = true;
boolean actual = CustomStringLibrary.isEmpty(str);
Assert.assertEquals(expected, actual);
}
@Test
public void testReplace() {
String str = "Hello, World!";
char oldChar = 'o';
char newChar = 'x';
String expected = "Hellx, Wxrld!";
String actual = CustomStringLibrary.replace(str, oldChar, newChar);
Assert.assertEquals(expected, actual);
}
@Test
public void testSplit() {
String str = "Hello, World!";
String delimiter = ", ";
String[] expected = {"Hello", "World!"};
String[] actual = CustomStringLibrary.split(str, delimiter);
Assert.assertArrayEquals(expected, actual);
}
@Test
public void testJoin() {
String[] arr = {"Hello", "World!"};
String delimiter = ", ";
String expected = "Hello, World!";
String actual = CustomStringLibrary.join(arr, delimiter);
Assert.assertEquals(expected, actual);
}
@Test
public void testEqualsIgnoreCase() {
String str1 = "Hello, World!";
String str2 = "hello, world!";
boolean expected = true;
boolean actual = CustomStringLibrary.equalsIgnoreCase(str1, str2);
Assert.assertEquals(expected, actual);
}
@Test
public void testIndexOf() {
String str = "Hello, World!";
char ch = 'o';
int expected = 4;
int actual = CustomStringLibrary.indexOf(str, ch);
Assert.assertEquals(expected, actual);
}
@Test
public void testFormat() {
String format = "Hello, {0}!";
String argument = "World";
String expected = "Hello, World!";
String actual = CustomStringLibrary.format(format, argument);
Assert.assertEquals(expected, actual);
}
@Test
public void testStartsWith() {
String str = "Hello, World!";
String prefix = "Hello";
boolean expected = true;
boolean actual = CustomStringLibrary.startsWith(str, prefix);
Assert.assertEquals(expected, actual);
}
@Test
public void testEndsWith() {
String str = "Hello, World!";
String suffix = "World!";
boolean expected = true;
boolean actual = CustomStringLibrary.endsWith(str, suffix);
Assert.assertEquals(expected, actual);
}
@Test
public void testToLowercase() {
String str = "HeLLo, WoRLD!";
String expected = "hello, world!";
String actual = CustomStringLibrary.toLowercase(str);
Assert.assertEquals(expected, actual);
}
@Test
public void testToUppercase() {
String str = "HeLLo, WoRLD!";
String expected = "HELLO, WORLD!";
String actual = CustomStringLibrary.toUppercase(str);
Assert.assertEquals(expected, actual);
}
@Test
public void testTrim() {
String str = " Hello, World! ";
String expected = "Hello, World!";
String actual = CustomStringLibrary.trim(str);
Assert.assertEquals(expected, actual);
}
@Test
public void testCompareTo() {
String str1 = "Hello";
String str2 = "World";
int expected = -15;
int actual = CustomStringLibrary.compareTo(str1, str2);
Assert.assertEquals(expected, actual);
}
}
Editor is loading...
Leave a Comment