Divide string into several substrings - java

I have a strings that contain only digits. String itself would look like this "0011112222111000" or "1111111000". I'd like to know how can I get an array of substrings which will consist of strings with only one digit.
For example, if I have "00011111122233322211111111110000000" string, I 'd like it to be in string array(string[]) which contains ["000","111111","222","333","222","1111111111","0000000"].
This is what I've tried
for (int i = (innerHierarchy.length()-1); i >= 1; i--) {
Log.e("Point_1", "innerHierarchy " + innerHierarchy.charAt(i));
c = Character.toChars(48 + max);
Log.e("Point_1", "c " + c[0]);
if (innerHierarchy.charAt(i) < c[0] && innerHierarchy.charAt(i - 1) == c[0]) {
Log.e("Point_1", "Start " + string.charAt(i));
o = i;
} else if (innerHierarchy.charAt(i) == c[0] && innerHierarchy.charAt(i - 1) < c[0]) {
Log.e("Point_1", "End " + string.charAt(i));
o1 = i;
string[j] = string.substring(o1,o);
j=j+1;
}
}
But this code won't work if string looks like this "111111000"
Thank you.

I have "00011111122233322211111111110000000" string, I 'd like it to
be in string array(string[]) which contains
["000","111111","222","333","222","1111111111","0000000"]
One approach I can think of right now (O(n)) (might not be the most efficient but would solve your problem) would be traversing the string of numbers i.e. ("00011111122233322211111111110000000" in your case )
and if char at that position under consideration is not same as char at previous position then making string till that part as one string and continuing.
(approach)
considering str= "00011111122233322211111111110000000"
//starting from position 1 (ie from 2nd char which is '0')
//which is same as prev character ( i.e 1st char which is '0')
// continue in traversal
// now char at pos 2 which is again '0'
// keep traversing
// but then char at position 3 is 1
// so stop here and
//make substring till here-1 as one string
//so "000" came as one string
//continue in same manner.
code
import java.util.*;
public class A {
public static void main(String []args){
String str = "00011111122233322211111111110000000";
str+='-'; //appended '-' to get last 0000000 as well into answer
//otherwise it misses last string which i guess was your problem
String one_element ="";
int start=0;
for(int i=1;i<str.length();i++){
if(str.charAt(i)== str.charAt(i-1) )
{
}
else{
one_element = str.substring(start,i);
start = i;
System.out.println(one_element);//add one_element into ArrayList if required.
}
}
}
}
I have printed each element here as string , if you need an array of all those you can simply use an array_list and keep adding one_element in array_list instead of printing.

Related

How to reverse a String after a comma and then print the 1st half of the String Java

For example String grdwe,erwd becomes dwregrdwe
I have most of the code I just have trouble accessing all of ch1 and ch2 in my code after my for loop in my method I think I have to add all the elements to ch1 and ch2 into two separate arrays of characters but I wouldn't know what to initially initialize the array to it only reads 1 element I want to access all elements and then concat them. I'm stumped.
And I'd prefer to avoid Stringbuilder if possible
public class reverseStringAfterAComma{
public void reverseMethod(String word){
char ch1 = ' ';
char ch2 = ' ';
for(int a=0; a<word.length(); a++)
{
if(word.charAt(a)==',')
{
for(int i=word.length()-1; i>a; i--)
{
ch1 = word.charAt(i);
System.out.print(ch1);
}
for (int j=0; j<a; j++)
{
ch2 = word.charAt(j);
System.out.print(ch2);
}
}
}
//System.out.print("\n"+ch1);
//System.out.print("\n"+ch2);
}
public static void main(String []args){
reverseStringAfterAComma rsac = new reverseStringAfterAComma();
String str="grdwe,erwd";
rsac.reverseMethod(str);
}
}
You can use string builder as described here:
First split the string using:
String[] splitString = yourString.split(",");
Then reverse the second part of the string using this:
splitString[1] = new StringBuilder(splitString[1]).reverse().toString();
then append the two sections like so:
String final = splitString[1] + splitString[0];
And if you want to print it just do:
System.out.print(final);
The final code would be:
String[] splitString = yourString.split(",");
splitString[1] = new StringBuilder(splitString[1]).reverse().toString();
String final = splitString[1] + splitString[0];
System.out.print(final);
Then, since you are using stringbuilder all you need to do extra, is import it by putting this at the top of your code:
import java.lang.StringBuilder;
It appears you currently have working code, but are looking to print/save the value outside of the for loops. Just set a variable before you enter the loops, and concatenate the chars in each loop:
String result = "";
for (int a = 0; a < word.length(); a++) {
if (word.charAt(a) == ',') {
for (int i = word.length() - 1; i > a; i--) {
ch1 = word.charAt(i);
result += ch1;
}
for (int j = 0; j < a; j++) {
ch2 = word.charAt(j);
result += ch2;
}
}
}
System.out.println(result);
Demo
Let propose a solution that doesn't use a StringBuilder
You should knoz there is no correct reason not to use that class since this is well tested
The first step would be to split your String on the first comma found (I assumed, in case there is more than one, that the rest are part of the text to reverse). To do that, we can you String.split(String regex, int limit).
The limit is define like this
If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n and the array's last entry will contain all input beyond the last matched delimiter.
If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.
If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
Example :
"foobar".split(",", 2) // {"foobar"}
"foo,bar".split(",", 2) // {"foo", "bar"}
"foo,bar,far".split(",", 2) // {"foo", "bar,far"}
So this could be used at our advantage here :
String text = "Jake, ma I ,dlrow olleh";
String[] splittedText = text.split( ",", 2 ); //will give a maximum of a 2 length array
Know, we just need to reverse the second array if it exists, using the simplest algorithm.
String result;
if ( splittedText.length == 2 ) { //A comma was found
char[] toReverse = splittedText[1].toCharArray(); //get the char array to revese
int start = 0;
int end = toReverse.length - 1;
while ( start < end ) { //iterate until needed
char tmp = toReverse[start];
toReverse[start] = toReverse[end];
toReverse[end] = tmp;
start++; //step forward
end--; //step back
}
result = new String( toReverse ) + splittedText[0];
}
This was the part that should be done with a StringBuilder using
if ( splittedText.length == 2 ){
result = new StringBuilder(splittedText[1]).reverse().toString() + splittedText[0];
}
And if there is only one cell, the result is the same as the original text
else { //No comma found, just take the original text
result = text;
}
Then we just need to print the result
System.out.println( result );
hello world, I am Jake

Removing Consecutive Characters in each Iteration shows Unexpected error

How to remove Consecutive Characters at each Iteration..
Below is the screenshot that explains the question with more details
MySolution
Initially I checked whether there are any Consecutive characters.
If yes,Then,remove all the consecutive characters and when there are no consecutive characters add the remaining characters to another String.
If no Consecutive Characters just simply increment it.
public static void print(){
String s1="aabcccdee"; I have taken a sample test case
String s2="";
for(int i=0;i<s1.length();){
if(s1.charAt(i)==s1.charAt(i+1)){
while(s1.charAt(i)==s1.charAt(i+1)){
i++;
}
for(int j=i+1;j<s1.length();j++){
s2=s2+s1.charAt(j);
}
s1=s2;
}
else
i++;
}
System.out.println(s1);
}
Output Shown
An infinite Loop
Expected Output for the give sample is
bd
Can Anyone guide me how to correct?
You can simply use String::replaceFirts with this regex (.)\1+ which means matche any charater (.) which followed by itself \1 one or more time + with empty.
In case you want to replace first by first you have to check the input, if after each iteration still contain more than one consecutive characters or not, in this case you can use Pattern and Matcher like this :
String[] strings = {"aabcccdee", "abbabba", "abbd "};
for (String str : strings) {
Pattern pattern = Pattern.compile("([a-z])\\1");
// While the input contain more than one consecutive char make a replace
while (pattern.matcher(str).find()) {
// Note : use replaceFirst instead of replaceAll
str = str.replaceFirst("(.)\\1+", "");
}
System.out.println(str);
}
Outputs
aabcccdee -> bd
abbabba -> a
abbd -> ad
Update
I had misread the question. The intent is to also remove the consecutive characters after each replacement. The below code does that.
private static String removeDoubles(String str) {
int s = -1;
for (int i = 1; i < str.length(); i++) {
// If the current character is the same as the previous one,
// remember its start position, but only if it is not set yet
// (its value is -1)
if (str.charAt(i) == str.charAt(i - 1)) {
if (s == -1) {
s = i - 1;
}
}
else if (s != -1) {
// If the current char is not equal to the previous one,
// we have found our end position. Cut the characters away
// from the string.
str = str.substring(0, s) + str.substring(i);
// Reset i. Notice that we don't have to loop from 0 on,
// instead we can start from our last replacement position.
i = s - 1;
// Finally reset our start position
s = -1;
}
}
if (s != -1) {
// Check the last portion
str = str.substring(0, s);
}
return str;
}
Note that this is almost 10 times faster than YCF_L's answer.
Original post
You are almost there, but you don't have to use multiple for loops. You just need one loop, because whether to remove characters from the string only depends on subsequent characters; we don't need to count anything.
Try this:
private static String removeDoubles(String s) {
boolean rem = false;
String n = "";
for (int i = 0; i < s.length() - 1; i++) {
// First, if the current char equals the next char, don't add the
// character to the new string and set 'rem' to true, which is used
// to remove the last character of the sequence of the same
// characters.
if (s.charAt(i) == s.charAt(i + 1)) {
rem = true;
}
// If this is the last character of a sequence of 'doubles', then
// reset 'rem' to false.
else if (rem) {
rem = false;
}
// Else add the current character to the new string
else {
n += s.charAt(i);
}
}
// We haven't checked the last character yet. Let's add it to the string
// if 'rem' is false.
if (!rem) {
n += s.charAt(s.length() - 1);
}
return n;
}
Note that this code is on average more than three times faster than regular expressions.
Try something like this:
public static void print() {
String s1 = "abcccbd"; // I have taken a sample test case
String s2 = "";
while (!s1.equals(s2)) {
s2 = s1;
s1 = s1.replaceAll("(.)\\1+", "");
}
System.out.println(s1);
}
consider this easier to understand code
String s1="aabcccdee";
while (true) {
rvpoint:
for (int x = 0; x < s1.length() -1; x++)
{
char c = s1.charAt(x);
if (c == s1.charAt(x+ 1)) {
s1 = s1.replace(String.valueOf(c), "");
continue rvpoint; // keep looping if a replacement was made
}
}
break; // break out of outer loop, if replacement not found
}
System.out.println(s1);
note
This will only work for the first iteration, put into a method and keep calling until the sizes do not change

Index value wont Increase after comparing two chars

I started this because I was totally bored but because of this error I have been sitting here since so long and finally decided to take this to stackOverFlow. Here is the code Which I wrote.
I was trying to print characters by skipping 1 index. But when there are duplicates I want to print a space which would differentitate words from big string.
Updated Question: Everything fixed except I cant increase I value more than 1. I commented it in below program. Please look at it.
Let me cut the chase and get to the point. I need this output " Vishnu Vardhan" from this String "aVeIwSjHaNgUaaVdAgRjDkHxAmN";
My only requirement is if the string has two same letters it has to print space. So "aVeIwSjHaNgU [aa] VdAgRjDkHxAmN" the aa in the brackets has to be replaced by space.
It has to be dynamic, if any char is repeated it has to print a space and jump to required next char and print it.
Here is the Updated program. Using help from one of the comments.
public class Decrypter {
public static void main(String[] args) {
String Text = "aVeIwSjHaNgUaaVdAgRjDkHxAmN";
char[] convertedText = Text.toCharArray();//converted string to char array
for (int i = 1; i < convertedText.length; i++) { //Looping it to print alternate chars
/* if the character at an index is same as the character at next index then
add a space and increase index value by 2 so I can print the required char*/
if (i + 1 < convertedText.length) {
if (Text.charAt(i) == Text.charAt(i + 1)) {
i++;// Increasing I value by 2 here will give me required output. Everything is perfect now
System.out.printf("%s ", convertedText[i]);
} else {
System.out.printf("%s", convertedText[i]);
i++;
}
}
}
}
}
Current output : VISHNUadgjkxm
Required output: VISHNU VARDHAN
i dont know if converting string to charArray is required but i hope this will do. comment below if you have questions open for revision.
String text = "aVeIwSjjHaNgUkkVarqddlhxn";
//this is the holder of your new processed text.
String newText = "";
//start of counter. it may start in any number depends on requirements.
int x = 0;
//loop while the x is lessthan the length of your string.
while(x < text.length()){
//check if the x + 1 is not equal to the length of your string to avoid StringIndexOutOfBoundsException
if((x+1) != text.length()){
//in this area it will check if the current char is the same on next index
if(text.charAt(x) == text.charAt(x+1)){
// this will concatenate/append the value of char with space
newText += text.charAt(x) +" ";
// this will increase the value of your x by 1 and at the bottom there are also x++ that will add 1 to your x so the total sum of x if (text.charAt(x) == text.charAt(x+1)) are true, will be 2.
x++;
}
}
newText += text.charAt(x);
x++;
}
System.out.println(newText);
output :
aVeIwSj jHaNgUk kVarqd dlhxn
if this is not what you looking for please kindly update your question.
Fixed:
/**
*
* #author Chintu
*/
public class Decrypter {
public static void main(String[] args) {
String Text = "aVeIwSjHaNgUkkVarqdlhxn";
char[] convertedText = Text.toCharArray();//converted string to char array
for (int i = 1; i < convertedText.length; i++) { //Looping it to print alternate chars
/* if the character at an index is same as the character at next index then
add a space and increase index value by 2 so I can print the required char*/
if (i+1 < convertedText.length) {
if (Text.charAt(i) == Text.charAt(i + 1)) {
i++;
System.out.printf("%s ",convertedText[i]);
}
}
System.out.printf("%s", convertedText[i]);
}
}
}
Fixed
public class Decrypter {
public static void main(String[] args) {
String Text = "aVeIwSjHaNgUaaVdAgRjDkHxAmN";
char[] convertedText = Text.toCharArray();//converted string to char array
for (int i = 0; i < convertedText.length; i++) { //Looping it to print alternate chars
/* if the character at an index is same as the character at next index then
add a space and increase index value by 2 so I can print the required char*/
if (i + 1 < convertedText.length) {
if (Text.charAt(i) == Text.charAt(i + 1)) {
i += 2;
System.out.printf(" %s", convertedText[i]);
} else {
i++;
System.out.printf("%s", convertedText[i]);
}
}
}
}
}
output: VISHNU VARDHAN

scramble some letter in ArrayList assignment

This assignment involves reasoning about strings made up of uppercase letters. You will implement several static methods that appear in the same class (not shown). Here are the details.
1. The first method takes a single string parameter and returns a scrambled version of that string. The scrambling process begins at the first letter of the word and continues from left to right. If two consecutive letters consist of an "A" followed by a letter that is not an "A", then the two letters are swapped in the resulting string. Once the letters in two adjacent positions have been swapped, neither of those two positions can be involved in a future swap.
public static String scrambleWord(String word)
The method takes a given word (an empty string or a string containing only upper case letters) and returns a string that contains a scrambled version of the word according to the rules given above. The following table shows several examples of words and their scrambled versions.
Original word After scrambling
"TAN" "TNA"
"ABRACADABRA" "BARCADABARA"
"WHOA" "WHOA"
"AARDVARK" "ARADVRAK"
"EGGS" "EGGS"
"A" "A"
"" ""
the code i used but it dose not work is
public class ScrambleWord {
public static void main(String[] args) {
List<String> strList = new ArrayList<String>();
strList.add("TAN");
strList.add("ABRACADABRA");
strList.add("WHOA");
strList.add("EGGS");
strList.add("A");
strList.add("");
System.out.prentln(MainMethod.scrambleWordMeth(strList));
}
class MainMethod {
public static void scrambleWordMeth(List<String> strList) {
int curr = 0;
String res = "";
while (curr < strList.size()) {
String currentString = strList.get(curr);
for(int i = 0; i < currentString.length(); i++){
if (currentString.charAt(i) == 'A' && !(currentString.charAt(i + 1) == 'A')) {
res = res + currentString.substring(curr + 1, curr + 2);
res = res + 'A';
curr = curr + 2;
}
else {
res = res + currentString.substring(curr, curr + 1);
curr++;
}
}
if (curr < strList.size()) {
res = res + currentString.charAt(curr);
//res=res + strList.substring(curr);
}
}
return res;
}
}
}
Here is template for how to setup the methods such that the algorithm can be worked on in a more clear and isolated manner (note how the task states for "several methods"). This will prevent some issues in the posted code such as the incorrect usage of curr (which did not related to characters at all) in the inner loop. The usage of the array for the letters makes the task itself more logical to focus on without needing to perform slicing and concatenation.
static void scrambleAllWords(List<String> words) {
// Iterate through the list of word applying the scramble
// function and replacing the original item with the result.
for (int i = 0; i < words.size(); i++) {
String scrambled = scrambleWord(words.get(i));
words.set(i, scrambled);
}
}
static String scrambleWord(String word) {
// Get the letters that make up the word
char[] letters = word.toCharArray();
// Perform the algorithm on the letters
// for (int i = 0; i < ..
// Create a new string from the now-scrambled letters
return new String(letters);
}
The algorithm itself is rather simple and can be read as the following pseudo-code, which should be trivial to apply to letters as it is now an array clearly separated from the other cruft.
for i in the range [0, the number of letters in the word - 1)
if the letter at i is an 'A' and the following letter at i+1 is not an 'A' then
swap the letter at i with the letter at i+1 and
skip the next letter by advancing i by 1
(so that the current-now-next 'A' letter cannot be swapped again)
otherwise
do nothing

logically sorting a mixed string of uppercase letters and numbers

I've got a string of uppercase letters and numbers that I must 'logically' sort and store in a field in a database. I've got the update/change/inquire part into the database figured out. I'm struggle with logically sorting this string.
Here goes, I hope I can explain this well.
Given this set of strings
AB1
AB2
AB3
A11
AB10
I need these to alpha sort like so
A11
AB1
AB2
AB3
AB10
in order to achieve this, I believe I need to explode the string. because currently trying to alpha sort yields A11 AB1 AB10 AB2 AB3
EDIT: I need to be able to store an exploded string and a non exploded string to be able to sort with other programs.
Here is how I think they need to be broken up and stored in order to sort alpha
A11 - A 11
AB1 - AB 1
AB2 - AB 2
AB3 - AB 3
AB10 - AB 10
There are some constants. The string will be no larger than 5 positions. It will only contain upper case letters and numbers.
Here is as far as I've gotten with my code. writers block so i'm hoping for some help. I think I need to find if it starts with a letter, then find all the consecutive letters, move those left alight, then go to work on number, finding all the consecutive numbers and move those right aligned. Not sure how something like 'A1B1' would work either...
for(int ii = 0;ii < sectionString.length() && ii< SECTIONSPACES;ii++){
System.out.print(" Was previous a number? " + isPreviousANumber + "\n");
try{
String tmpString = sectionString.substring(ii,ii + 1 );
int positionInCharArray = Integer.parseInt(tmpString);
System.out.printf(" Position " + ii + " is number " + positionInCharArray + "\n");
isPreviousANumber = true;
}catch(Exception e){
System.out.printf(" Position " + ii + " number is not a number " + sectionString.substring(ii,ii) + "\n");
isPreviousANumber = false;
}
}
This remark "Not sure how something like 'A1B1' would work either..." somewhat increases the complexity of the problem. The following should work for all cases.
Method:
Divide the string into tokens. A token is either a letter or a consecutive run of digits. Pad each digits-token to five characters with leading spaces. Concatenate the tokens to make the exploded string.
From a 5 character original, the longest exploded string will be 17 characters.
The resulting exploded strings may be sorted by any program, or by a SQL "ORDERED BY" clause.
Examples:
1A1A1 " 1A 1A 1"
11A11 " 11A 11"
1111A " 1111A"
11111 "11111"
A1 "A 1"
A1B1 "A 1B 1"
A1C "A 1C"
A2 "A 2"
A2B1 "A 2B 1"
A10 "A 10"
A10B1 "A 10B 1"
A11 "A 11"
AA1 "AA 1"
AB1 "AB 1"
AB2 "AB 2"
AB10 "AB 10"
ABC "ABC"
Pseudocode:
// original = "section" string
exploded = ""
prevdigits = false
for ii from 1 to length(original) {
ch = original[ii]
if (ch is a digit) then {
if not prevdigits then {
token = ""
prevdigits = true
}
token = token+ch
} else { // letter
if prevdigits then {
exploded = exploded + spaces(5-length(token)) + token
prevdigits = false
}
exploded = exploded + ch
}
}
-Al.
Here is how I sort it using my radix sort idea:
public static String[] radixSort(String[] strings){
// Pad the strings
for(int i=0; i<strings.length; i++){
strings[i] = String.format("%-5s", strings[i]);
}
// Radix sort them
for (int digit = 0; digit < 5; digit++) {
final int i = digit;
Arrays.sort(strings, new Comparator<String>() {
#Override
public int compare(String o1, String o2) {
return o1.charAt(i) - o2.charAt(i);
}
});
}
// Then trim the whitespaces we used to pad
for (int i = 0; i < strings.length; i++) {
strings[i] = strings[i].trim();
}
return strings;
}
With input
String[] strings = new String[] { "AB1", "AB2", "AB3", "A11", "AB10" };
System.out.println(Arrays.toString(radixSort(strings)));
And output
[A11, AB1, AB2, AB3, AB10]
I am not sure this is the most efficient method but it gets the job done.
you could use another class as special representation for your strings. something like this:
public class AlphaNumericString implements Comparable<AlphaNumericString> {
public final String alphaPart;
public final Long numericPart;
public AlphaNumericString(String string) {
int index = 0;
while (index < string.length() && !Character.isDigit(string.charAt(index))) {
index++;
}
alphaPart = string.substring(0, index);
if (index < string.length()) {
numericPart = new Long(string.substring(index));
} else {
numericPart = null;
}
}
#Override
public int compareTo(AlphaNumericString other) {
int stringCompareResult = alphaPart != null ? alphaPart.compareTo(other.alphaPart) : -1;
if (stringCompareResult == 0) {
return numericPart != null ? numericPart.compareTo(other.numericPart) : -1;
} else {
return stringCompareResult;
}
}
#Override
public String toString() {
return (alphaPart != null ? alphaPart : "") + (numericPart != null ? numericPart : "");
}
}
You can turn your current strings into this class, sort and convert them back as needed
I would complete these strings with spaces to 5 symbols and after that would make Radix Sort . We can compare all symbols as chars.
String[] array = {"A11", "AB1", "AB2", "AB3", "AB10"};
int i, j, length;
for (i = 0; i < array.length; i++) {
length = array[i].length();
for (j = length; j < 5; j++) {
array[i] += " ";
}
}
Arrays.sort(array);
for (int k = 0; k<array.length; k++)
System.out.println(array[k]);
Here is my code. I'm sure it can be streamlined, it was one of those blackout moments where i had a brain child and needed to write. This would not work if the string of numbers was over 5 characters long...
updated:less ugly
private String buildPieceSortNumber(String pieceNumber){
final int INTSPACES = 5;
final String SPACE = " ";
String explodedSection = "";
char[] charArray = pieceNumber.toCharArray();
String ints = "";
for(int i = 0;i < charArray.length;i++){
if(Character.isDigit(charArray[i])){
//add to the int string
ints += charArray[i];
//check if the next character in the array is a number
int nextChar = i + 1;
//make sure we don't go past the end of the string
if(nextChar < charArray.length){
if(!Character.isDigit(charArray[nextChar])){
//end of numbers, take ints string, and add padding up to five positions
while(ints.length() < INTSPACES){
ints = SPACE + ints;
}
//add the int string to the end of the exploded string
explodedSection += ints;
//clear the int string
ints = "";
}
}else{
//end of numbers, take ints string, and add padding up to five positions
while(ints.length() < INTSPACES){
ints = SPACE + ints;
}
//add the int string to the end of the exploded string
explodedSection += ints;
//clear the int string
ints = "";
}
}else{
explodedSection += charArray[i];
}
}
return explodedSection;
Do you really need to sort the data before putting it in the database? Consider letting the database do the work for you.
Suppose you wrote the value straight into the database. Your database might allow you to do something like mine does. In DB2, to get only letters, I would translate all the digits to spaces, and then remove all the spaces. The same concept can apply to getting only digits.
SELECT replace(translate(inp, #spaces, #digits),' ','') as alpha,
int(replace(translate(inp, #spaces, #letters),' ','')) as nbr,
....
While this might be a normalized database approach, you might question performing this calculation every time data is retrieved from the table. So instead, do this when writing the the data into the table
INSERT INTO yourtable ( item, alpha, nbr, ..... )
VALUES (inp,
replace(translate(inp, #spaces, #digits),' ',''),
int(replace(translate(inp, #spaces, #letters),' ','')),
.....
)
To my view, this is simpler logic, less code, easier to test / debug, helping reduce risks of defects, and being easier for someone to maintain. Of course your mileage may vary depending on your database. But this approach seems worth consideration.

Categories

Resources