Need help for function

I have a string of 200 characters (max) which is divided into several different zones, for example in position 101 on 30 characters I have the name of the article, in position 16 on 2 characters the theme (it is also necessary to respect the framing on the right or on the left in the zone as well as the characteristics of completion) etc… I have to update or insert values in this string at different positions. I had a java function that I was using in Talend Open Studio but I don’t know how to translate that into js and especially into N8N

Thank you for your suggestions

here is my java code :

public static String xpositionne(String chaine,Integer debut,Integer longueur,String variable,Boolean cadredroite,String caraComp){
        String variableNew="";
        Integer variableLength=0;
        String chaineL="";
        String chaineR="";
        Integer chaineLength=0;
        variableLength=variable.length();
        System.out.println("chaine :"+chaine);
        System.out.println("variable : "+variable);
        System.out.println("variablelenght : "+variableLength);
        chaineLength=chaine.length();
        variableNew=variable;
        if(longueur<variableLength){
            variableNew=variable.substring(0,longueur-1);
        }
        if(longueur>variableLength){
            if(cadredroite){
                for(int i=1;i<=(longueur-variableLength);i++){
                    variableNew=caraComp+variableNew;
                }
            }
            if(cadredroite==false){
                for(int i=1;i<=(longueur-variableLength);i++){
                    variableNew+=caraComp;
                }
            }
        }
        chaineL=chaine.substring(0,debut-1);
        chaineR=chaine.substring((debut-1)+longueur,chaineLength);
        return (chaineL+variableNew+chaineR);
    }

Hey @fporta, so you have a string with a length of up to 200 characters and want to get something like the 30 characters starting from position 101 in a name field? This sounds like a job for .substring().

You can use this method in a Set node like so:

Hope this helps!

This is not quite what I want, I want to be able to insert/update a value at a certain position in a string. My initial idea was to use slice() or subtring() to get the part of the string before the area to be inserted, then the part after the area to be inserted and then concatenate the 3 parts: area before insertion area to be inserted area after insertion

Hey @fporta,

What about using the replace() function which can be used with a static value or regex?

finally I chose to use slice() in a function with parameter

thanks for your help

1 Like