receta olla madrileño ingredientes facil español cocido javascript string split

javascript - olla - cortar parte de una cuerda



cocido madrileño receta (8)

Algunas otras alternativas más modernas son:

  1. Dividir y unirse

    function cutFromString(oldStr, fullStr) { return fullStr.split(oldStr).join(''''); } cutFromString(''there '', ''Hello there world!''); // "Hello world!"

    Adaptado del ejemplo de MDN.

  2. String.replace() , que utiliza String.replace() regulares. Esto significa que puede ser más flexible con la sensibilidad a las mayúsculas.

    function cutFromString(oldStrRegex, fullStr) { return fullStr.replace(oldStrRegex, ''''); } cutFromString(/there /i , ''Hello THERE world!''); // "Hello world!"

Di, tengo una cuerda

"hello is it me you''re looking for"

Quiero cortar parte de esta cadena y devolver la nueva cadena, algo así como

s = string.cut(0,3);

s ahora sería igual a:

"lo is it me you''re looking for"

EDIT : Puede no ser de 0 a 3. Podría ser de 5 a 7.

s = string.cut(5,7);

volvería

"hellos it me you''re looking for"


Intenta lo siguiente:

var str="hello is it me you''re looking for"; document.write(str.substring(3)+"<br />");

Puedes consultar este enlace



Solo como referencia para cualquier persona que busque una función similar, tengo una implementación String.prototype.bisect que divide una cadena de 3 maneras utilizando un delimitador de expresiones regulares / expresiones regulares y devuelve el delimitador-coincidencia anterior y parte de la cadena ... .

/* Splits a string 3-ways along delimiter. Delimiter can be a regex or a string. Returns an array with [before,delimiter,after] */ String.prototype.bisect = function( delimiter){ var i,m,l=1; if(typeof delimiter == ''string'') i = this.indexOf(delimiter); if(delimiter.exec){ m = this.match(delimiter); i = m.index; l = m[0].length } if(!i) i = this.length/2; var res=[],temp; if(temp = this.substring(0,i)) res.push(temp); if(temp = this.substr(i,l)) res.push(temp); if(temp = this.substring(i+l)) res.push(temp); if(res.length == 3) return res; return null; }; /* though one could achieve similar and more optimal results for above with: */ "my string to split and get the before after splitting on and once".split(/and(.+)/,2) // outputs => ["my string to split ", " get the before after splitting on and once"]

Como se indica aquí: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/String/split

Si separador es una expresión regular que contiene paréntesis de captura, cada vez que el separador coincida con los resultados (incluidos los resultados no definidos) de los paréntesis de captura se empalman en la matriz de salida. Sin embargo, no todos los navegadores soportan esta capacidad.


Utilizar

substring

función

Devuelve un subconjunto de una cadena entre un índice y otro, o hasta el final de la cadena.

substring(indexA, [indexB]);

índiceA

An integer between 0 and one less than the length of the string.

indexB (opcional) Un entero entre 0 y la longitud de la cadena.

subcadena extrae caracteres desde indexA hasta pero sin incluir indexB. En particular:

* If indexA equals indexB, substring returns an empty string. * If indexB is omitted, substring extracts characters to the end of the string. * If either argument is less than 0 or is NaN, it is treated as if it were 0. * If either argument is greater than stringName.length, it is treated as if it were stringName.length.

Si indexA es más grande que indexB, entonces el efecto de la subcadena es como si los dos argumentos se intercambiaran; por ejemplo, str.substring (1, 0) == str.substring (0, 1).


Ya casi estás ahí. Lo que quieres es:

http://www.w3schools.com/jsref/jsref_substr.asp

Entonces, en tu ejemplo:

Var string = "hello is it me you''re looking for"; s = string.substr(3);

Como solo proporcionar un inicio (el primer argumento) toma de ese índice hasta el final de la cadena.

Actualización, ¿qué tal algo como:

function cut(str, cutStart, cutEnd){ return str.substr(0,cutStart) + str.substr(cutEnd+1); }


string.substring () es lo que quieres.


s = string.cut(5,7);

Preferiría hacerlo como una función separada, pero si realmente quieres poder llamarlo directamente en una cadena desde el prototipo:

String.prototype.cut= function(i0, i1) { return this.substring(0, i0)+this.substring(i1); }