How to increment Strings? (Eg. 00, AA, AB, AC, AD, BA, BB, BC)

0
Hi Guys, I am thinking of a way to increment Strings of 2 characters. Example: 00 -> AA -> AB -> AC -> BA   I will have a string attribute under a entity.    I could use enumerations, but if I were to increment it till the max (AA ->..... AZ -> BA ->.....BZ), I will need to create a lot of enums and that's not efficient. Anyone has any ideas? 
asked
2 answers
0

What about a Java Action to generate the next in sequence. Chat GPT is your friend, It probably requires some tweaking.

> Generate a Java function to get the next in a sequence 00, AA, AB, AC, AD, BA, BB, BC, etc

 

public class SequenceGenerator {

    public static void main(String[] args) {
        // Example usage
        System.out.println(getNextInSequence("AD"));  // Output: "AE"
        System.out.println(getNextInSequence("ZZ"));  // Output: "AAA"
    }

    public static String getNextInSequence(String current) {
        // Convert the string into a character array for manipulation
        char[] chars = current.toCharArray();
        
        // Start from the last character and work backwards
        int i = chars.length - 1;
        
        // Iterate and increment the characters as needed
        while (i >= 0) {
            // If the character is 'Z', reset it to 'A' and move left
            if (chars[i] == 'Z') {
                chars[i] = 'A';
                i--;
            } else {
                // Increment the character by 1
                chars[i]++;
                break;
            }
        }
        
        // If all characters were 'Z' and now are 'A', we need to add another character at the beginning
        if (i < 0) {
            // The result will have one more character at the start (e.g., "ZZ" -> "AAA")
            return "A" + new String(chars);
        }
        
        // Return the next sequence
        return new String(chars);
    }
}

 

 

answered
0

you can make a microflow with a parameter number to convert for example 29 to AC

$Label = ''

While $Number > 0:

  $Remainder = mod (number - 1, 26)

  $Letter = substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', Remainder, 1)

  $Label = $Label + $Letter

  $Number = floor(($Number - 1) / 26)

 

This is just a direction, please check the algorithm.

 

if you need it you can also create a microflow to convert AC -> 29.

 

Total microflow IncrementString is then

ConvertNumberToString(ConvertStringToNumber(source)+1))

 

answered