Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JoinStringMulti (suggestion) #141

Open
jtc-jtc opened this issue Nov 12, 2024 · 2 comments
Open

JoinStringMulti (suggestion) #141

jtc-jtc opened this issue Nov 12, 2024 · 2 comments

Comments

@jtc-jtc
Copy link

jtc-jtc commented Nov 12, 2024

I have been finding the JoinStringMulti very useful in a couple of workflows, but using '\n' delimiter doesn't seem to add a line break at the end of each string as with other similar nodes. is there a workaround to add this function to the node?

CleanShot 2024-11-12 at 10 33 14@2x

@kijai
Copy link
Owner

kijai commented Nov 12, 2024

Can you give an example of a node where it does work?

@jtc-jtc
Copy link
Author

jtc-jtc commented Nov 12, 2024

was-node-suite 'text concatenate' node seems to work correctly using \n but it doesn't have the great input count option as yours does!

I managed to get it working by adding a bit of code to your 'nodes.py' file from an LLM response (sorry I'm not a coder so no idea of its quality). It also adds a section to ignore null inputs which is handy if some of the input strings are bypassed/muted. I will paste the class section below -

class JoinStringMulti:
    @classmethod
    def INPUT_TYPES(cls):
        return {
            "required": {
                "inputcount": ("INT", {"default": 2, "min": 2, "max": 1000, "step": 1}),
                "string_1": ("STRING", {"default": '', "forceInput": True}),
                "string_2": ("STRING", {"default": '', "forceInput": True}),
                "delimiter": ("STRING", {"default": ' ', "multiline": False}),
                "return_list": ("BOOLEAN", {"default": False}),
            },
        }

    RETURN_TYPES = ("STRING",)
    RETURN_NAMES = ("string",)
    FUNCTION = "combine"
    CATEGORY = "KJNodes"
    DESCRIPTION = """
    Creates a single string or a list of strings from multiple input strings.
    You can set how many inputs the node has with **inputcount** and clicking update.
    """

    def combine(self, inputcount, delimiter, **kwargs):
        # Convert "\n" in delimiter to an actual newline character
        if delimiter == "\\n":
            delimiter = "\n"

        # Initialize the combined string with the first non-empty string
        string = kwargs.get("string_1", "")
        return_list = kwargs.get("return_list", False)
        strings = [] if string == "" else [string]  # Initialize list only if first string is non-empty

        # Loop through the remaining strings and add them if they are non-empty
        for c in range(1, inputcount):
            key = f"string_{c + 1}"
            new_string = kwargs.get(key, "")
            if new_string:  # Only add non-empty strings
                if return_list:
                    strings.append(new_string)
                else:
                    string += delimiter + new_string

        # Return the appropriate output based on return_list
        if return_list:
            return (strings,)  # Return the list of non-empty strings
        else:
            return (string,)  # Return the concatenated string

CleanShot 2024-11-12 at 15 36 50@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants