Home > Software engineering >  Javascripts string comparison and update
Javascripts string comparison and update

Time:10-27

I'm trying to implement this function that receives 2 strings as parameters, this function needs to compare the first and the second string, taking/saving the differences, then it should add to the first one the differences. I'm gonna explain me better:

I have var str1 = "abbccc" and var str2 = "aabbbbd", now what I need to obtain is str3 = aabbbbcccd.

How can I do this? I excuse myself for my bad English, but I really need to know how to do this.

UPDATE:

The first string presents like this:

const BaseModel = require("./baseModel");
const MTYPE = require("../lib/types");
const TOOLS = require("../lib/toolkit");
const CONSTANTS = require("../lib/constants");

/**
 * @description
 * ### CRM ###
   Model per la gestione dei soggetti: clienti/fornitori/destinazioni
 */

const xdKeyCliente = "clienti";
const xdKeyLead = "leads";
const xdKeyFornitore = "fornitori";
const xdKeyAgente = "agenti";
const xdKeyDestinazione = "destinazioni";
const xdKeyContatto = "contatti";
const xdKeyRivenditore = "rivenditori";
const xdKeyGruppiUtente = "gruppi_utente";
const xdKeyUtente = "utenti";
const xdKeyProspect = "prospects";

class SoggettiModel extends BaseModel {


    constructor(baseObject, callbackNotification) {

        var prop = {
            fields: {
                // #region CAMPI DB
                uuid: {
                    type: MTYPE.UUID,
                    allowNull: false,
                    primaryKey: true,
                    max: 16,
                    default: 'DEFAULT',
                },
            table: 'soggetti',
            odbcKey: 'DEFAULT',
            entity: "soggetti",
            principalFields: ["uuid", "ragsoc", "pfNome", "pfCognome", "indir", "local", "cap", "prov", "stato", "telefono", "email", "cellulare", "iTipo", "iTipoDescrizione", "iStatoDescrizione", "iProvinciaDescrizione", "iRuoloDescrizione", "iProvinciaCodice", "iNominativo", "iGestore", "iLinkedId", "iLinkedDescrizione", "iLinkedTipo", "obsoleto"],
            callbackNotification: callbackNotification,
        }

        super(prop, baseObject);
    }


    /**
     * Metodo per estrarre il soggetto di fatturazione.
     * Se � una destinazione ti ritorna il cliente, se � una filiale ritorna il gestore, se � un cliente ritorna il cliente stesso
     * @param {uuid} uuid
     * @param {any} isCliente: posso passare 0
     */
    async getClienteIfDestinazione(uuid, isCliente = "") {
        let cliente = "";

        let result = await this.executeFindByAttributes([{ "C": "uuid", "V": uuid }, { "OL": "AND" },
            { "P": "(" },
            { "C": "tipo", "V": "C" },
            { "OL": "OR" },
            { "P": "(" },
            { "C": "tipo", "O": "IN", "V": TOOLS.set_as_column("('D', 'I')") },
            { "OL": "AND" },
            { "C": "linktotype", "V": "C" },
            { "P": ")" },
            { "OL": "OR" },
            { "P": "(" },
            { "C": TOOLS.set_as_column(this.normString(isCliente)), "V": "0" },
            { "OL": "AND" },
            { "C": "tipo", "V": "L" },
            { "P": ")" },
            { "P": ")" },
        ], [], [], [TOOLS.set_as_column("CASE WHEN "   this.normString(isCliente)   " = '0' AND tipo = 'L' THEN uuid WHEN tipo = 'C' and id_gestore is null THEN uuid WHEN tipo = 'C' AND id_gestore IS NOT NULL THEN id_gestore WHEN tipo in ('D', 'I') THEN linktoid ELSE NULL END AS cliente")]);
        if (result.length > 0) {
            cliente = result[0]["cliente"];
        }

        if (cliente == null) {
            cliente = "";
        }

        return cliente;
    }


    /**
     * Ritorna l'xdkey per gli allegati data la tipologia del soggetto (C: cliente, L: lead, F: fornitore)
     * @param {string} tipo
     */
    getXdKey(tipo) {
        let xdKey = "";
        if (tipo == "C" || tipo == "I") {
            xdKey = xdKeyCliente;
        }
        else if (tipo == "L") {
            xdKey = xdKeyLead;
        }
        else if (tipo == "F") {
            xdKey = xdKeyFornitore;
        }
        else if (tipo == "A") {
            xdKey = xdKeyAgente;
        }
        else if (tipo == "D" || tipo=="M") {
            xdKey = xdKeyDestinazione;
        }
        else if (tipo == "N") {
            xdKey = xdKeyContatto;
        }
        else if (tipo == "R") {
            xdKey = xdKeyRivenditore;
        }
        else if (tipo == "G") {
            entity = xdKeyGruppiUtente;
        }
        else if (tipo == "U") {
            entity = xdKeyUtente;
        }
        else if (tipo == "K") {
            entity = xdKeyProspect;
        }

        return xdKey
    }


    /**
    * Ritorna l'entit� per gli allegati data la tipologia del soggetto (C: cliente, L: lead, F: fornitore)
    * @param {string} tipo
    */
    getEntity(tipo) {
        let entity = "";
        if (tipo == "C" || tipo == "I") {
            entity = "clienti";
        }
        else if (tipo == "L") {
            entity = "leads";
        }
        else if (tipo == "F") {
            entity = "fornitori";
        }
        else if (tipo == "A") {
            entity = "agenti";
        }
        else if (tipo == "D" || tipo == "M") {
            entity = "destinazioni";
        }
        else if (tipo == "N") {
            entity = "contatti";
        }
        else if (tipo == "R") {
            entity = "rivenditori";
        }
        else if (tipo == "G") {
            entity = "gruppiutenti";
        }
        else if (tipo == "U") {
            entity = "utenti";
        }
        else if (tipo == "K") {
            entity = "prospects";
        }

        return entity
    }

    /**
     * Estrae l'utente dato l'uuid registry
     */
    async getUtenteByUuid() {
        return await this.executeFindByPk(this.registryId);
    }
}

module.exports = SoggettiModel;
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Otherwise my second one is like this:

const BaseModel = require("./baseModel");
const MTYPE = require("../lib/types");

/**
 * @description
 * ###  ###
   Model per la gestione dei soggetti: clienti/fornitori/destinazioni
 */

class SoggettiModel extends BaseModel {

    constructor(baseObject) {
        var prop = {
            fields: {
                // #region CAMPI DB

                newFiled: {
                    type: MTYPE.INTEGER,
                    allowNull: false,
                    primaryKey: true,
                    max: 16,
                    default: 'DEFAULT',
                }
            },
            table: 'soggetti',
            odbcKey: 'DEFAULT',
        }

        super(prop, baseObject);
    }
}

module.exports = SoggettiModel;
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

The string I need to get is something like:

const BaseModel = require("./baseModel");
const MTYPE = require("../lib/types");
const TOOLS = require("../lib/toolkit");
const CONSTANTS = require("../lib/constants");

/**
 * @description
 * ### CRM ###
   Model per la gestione dei soggetti: clienti/fornitori/destinazioni
 */

const xdKeyCliente = "clienti";
const xdKeyLead = "leads";
const xdKeyFornitore = "fornitori";
const xdKeyAgente = "agenti";
const xdKeyDestinazione = "destinazioni";
const xdKeyContatto = "contatti";
const xdKeyRivenditore = "rivenditori";
const xdKeyGruppiUtente = "gruppi_utente";
const xdKeyUtente = "utenti";
const xdKeyProspect = "prospects";

class SoggettiModel extends BaseModel {


    constructor(baseObject, callbackNotification) {

        var prop = {
            fields: {
                // #region CAMPI DB
                uuid: {
                    type: MTYPE.UUID,
                    allowNull: false,
                    primaryKey: true,
                    max: 16,
                    default: 'DEFAULT',
                },
                newFiled: {
                    type: MTYPE.INTEGER,
                    allowNull: false,
                    primaryKey: true,
                    max: 16,
                    default: 'DEFAULT',
                }
            }
            table: 'soggetti',
            odbcKey: 'DEFAULT',
            entity: "soggetti",
            principalFields: ["uuid", "ragsoc", "pfNome", "pfCognome", "indir", "local", "cap", "prov", "stato", "telefono", "email", "cellulare", "iTipo", "iTipoDescrizione", "iStatoDescrizione", "iProvinciaDescrizione", "iRuoloDescrizione", "iProvinciaCodice", "iNominativo", "iGestore", "iLinkedId", "iLinkedDescrizione", "iLinkedTipo", "obsoleto"],
            callbackNotification: callbackNotification,
        }

        super(prop, baseObject);
    }


    /**
     * Metodo per estrarre il soggetto di fatturazione.
     * Se � una destinazione ti ritorna il cliente, se � una filiale ritorna il gestore, se � un cliente ritorna il cliente stesso
     * @param {uuid} uuid
     * @param {any} isCliente: posso passare 0
     */
    async getClienteIfDestinazione(uuid, isCliente = "") {
        let cliente = "";

        let result = await this.executeFindByAttributes([{ "C": "uuid", "V": uuid }, { "OL": "AND" },
            { "P": "(" },
            { "C": "tipo", "V": "C" },
            { "OL": "OR" },
            { "P": "(" },
            { "C": "tipo", "O": "IN", "V": TOOLS.set_as_column("('D', 'I')") },
            { "OL": "AND" },
            { "C": "linktotype", "V": "C" },
            { "P": ")" },
            { "OL": "OR" },
            { "P": "(" },
            { "C": TOOLS.set_as_column(this.normString(isCliente)), "V": "0" },
            { "OL": "AND" },
            { "C": "tipo", "V": "L" },
            { "P": ")" },
            { "P": ")" },
        ], [], [], [TOOLS.set_as_column("CASE WHEN "   this.normString(isCliente)   " = '0' AND tipo = 'L' THEN uuid WHEN tipo = 'C' and id_gestore is null THEN uuid WHEN tipo = 'C' AND id_gestore IS NOT NULL THEN id_gestore WHEN tipo in ('D', 'I') THEN linktoid ELSE NULL END AS cliente")]);
        if (result.length > 0) {
            cliente = result[0]["cliente"];
        }

        if (cliente == null) {
            cliente = "";
        }

        return cliente;
    }


    /**
     * Ritorna l'xdkey per gli allegati data la tipologia del soggetto (C: cliente, L: lead, F: fornitore)
     * @param {string} tipo
     */
    getXdKey(tipo) {
        let xdKey = "";
        if (tipo == "C" || tipo == "I") {
            xdKey = xdKeyCliente;
        }
        else if (tipo == "L") {
            xdKey = xdKeyLead;
        }
        else if (tipo == "F") {
            xdKey = xdKeyFornitore;
        }
        else if (tipo == "A") {
            xdKey = xdKeyAgente;
        }
        else if (tipo == "D" || tipo=="M") {
            xdKey = xdKeyDestinazione;
        }
        else if (tipo == "N") {
            xdKey = xdKeyContatto;
        }
        else if (tipo == "R") {
            xdKey = xdKeyRivenditore;
        }
        else if (tipo == "G") {
            entity = xdKeyGruppiUtente;
        }
        else if (tipo == "U") {
            entity = xdKeyUtente;
        }
        else if (tipo == "K") {
            entity = xdKeyProspect;
        }

        return xdKey
    }


    /**
    * Ritorna l'entit� per gli allegati data la tipologia del soggetto (C: cliente, L: lead, F: fornitore)
    * @param {string} tipo
    */
    getEntity(tipo) {
        let entity = "";
        if (tipo == "C" || tipo == "I") {
            entity = "clienti";
        }
        else if (tipo == "L") {
            entity = "leads";
        }
        else if (tipo == "F") {
            entity = "fornitori";
        }
        else if (tipo == "A") {
            entity = "agenti";
        }
        else if (tipo == "D" || tipo == "M") {
            entity = "destinazioni";
        }
        else if (tipo == "N") {
            entity = "contatti";
        }
        else if (tipo == "R") {
            entity = "rivenditori";
        }
        else if (tipo == "G") {
            entity = "gruppiutenti";
        }
        else if (tipo == "U") {
            entity = "utenti";
        }
        else if (tipo == "K") {
            entity = "prospects";
        }

        return entity
    }

    /**
     * Estrae l'utente dato l'uuid registry
     */
    async getUtenteByUuid() {
        return await this.executeFindByPk(this.registryId);
    }
}

module.exports = SoggettiModel;
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

NB: These classes are contained inside a string, I wrote them in snippets to let you understand better.

CodePudding user response:

You could turn your strings into lists using string.split("") and then loop over the lists. If the entries are the same add the entrie from one string, if they are different add both entries to the string. If one string is longer than the over add the remaining entries to your output string I tried building it how I understood the question, there probably is a prettier/better way, but maybe this helps with your problem

  var str1 = "abbccc"
  var str2 = "aabbbbd"
  var out = ""
  var i =0;

  var longer = (str1.length > str2.length ? str1.length : str2.length)
 
 for(i=0;i<longer;i  ){
    str1[i] == str2[i] ? out  = str1[i] : str1[i] != undefined && str2[i] != undefined ? out  = (str1[i]   str2[i]) : ""
    str1[i] == null ? out  = str2[i] : ""
    str2[i] == null ? out  = str1[i] : ""
  }

  out = out.split("").sort().join("").toString() // Only if you need the string to be in alphabetical order
  
  console.log(out)
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

This is a one way you can do...

var str1 = "aaabbbccc";

var str2 = "abc";

var arr_one =str1.split('');

var arr_two = str2.split('');

var arr_all = arr_one.concat(arr_two);

var arr = arr_all.sort();

alert(arr);

CodePudding user response:

var str1 = "abbccc";
var str2 = "aabbbbd";
var str1_and_2 = str1   str2;
var str3 = [...str1_and_2];

console.log( str3.join('').toString() )
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related