#1 Extract Numeric Substrings from a String.
Title: Extract Numeric Substrings from a String in Java
Problem Statement:
Given an input string containing alphanumeric characters, the task is to extract all numeric substrings from the string and return them as a comma-separated list.
Input: abcd1234
Output: 1234
Solution:
We will create a Java method to solve this problem without using any regular expressions or built-in string manipulation methods. Instead, we will use a simple loop to process the input string character by character and extract the numeric substrings.
Java Solution:
public class NumericSubstringExtractor {
public static String extractNumericSubstrings(String input) {
String result = "";
String currentNumericSubstring = "";
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c >= '0' && c <= '9') {
currentNumericSubstring += c;
} else {
if (!currentNumericSubstring.isEmpty()) {
result += currentNumericSubstring + ",";
currentNumericSubstring = "";
}
}
}
if (!currentNumericSubstring.isEmpty()) {
result += currentNumericSubstring + ",";
}
if (!result.isEmpty()) {
result = result.substring(0, result.length() - 1);
}
return result;
}
public static void main(String[] args) {
String input = "abcd1234efgh";
String output = extractNumericSubstrings(input);
System.out.println(output); // Output: 1234
}
}
Explanation:
The provided Java solution follows a step-by-step approach to extract numeric substrings from the input string. Let's break down the logic:
1. We initialize two strings, `result` and `currentNumericSubstring`, to store the final output and the current numeric substring being processed, respectively.
Recommended by LinkedIn
2. We use a `for` loop to iterate through each character of the input string.
3. For each character, we check if it falls within the ASCII range of digits ('0' to '9').
4. If the character is a digit, we append it to the `currentNumericSubstring` to build the numeric substring.
5. When we encounter a non-digit character, it indicates the end of the current numeric substring. We check if `currentNumericSubstring` is not empty (i.e., we have processed some digits). If true, we add the current numeric substring to the `result` string, followed by a comma (',') to separate it from the next numeric substring.
6. After adding the `currentNumericSubstring` to the `result`, we reset `currentNumericSubstring` to an empty string to prepare for processing the next numeric substring.
7. After the loop completes, we check if there is any remaining `currentNumericSubstring`. If so, it indicates the last numeric substring. We add it to the `result` string, followed by a comma (',') if it exists.
8. To ensure a clean output, we check if the `result` string is not empty (i.e., there are numeric substrings). If true, we remove the trailing comma by using the `substring` method.
Conclusion:
By using a simple loop and basic string manipulation, we have successfully extracted numeric substrings from a given input string without relying on any regular expressions or built-in methods in Java. This approach allows us to solve similar problems efficiently while gaining a better understanding of string handling and character manipulation in the Java programming language.