What is the difference between the string.slice () method and the string.substring () method?

4

We know that the string.slice() method and the string.substring() method allow you to extract a part of a string and create a new string as a result (without modifying the original string). What differentiates one method from the other since both produce the same result?

    
asked by anonymous 19.11.2018 / 22:55

2 answers

1

I asked the question and from there ran a response too

As stated in the question " o método string.slice() e o método string.substring() permitem extrair uma parte de uma string e criar uma nova string como resultado (sem modificar a string original) "

  However, a useful improvement in string.slice () is that specifying a final index value relative to the end of the main string is easier.

     

Using string.substring() to extract a substring that ends before the end of the string requires some mechanisms, such as the following:

string.substring(4, (string.length-2))
     

Instead, you can assign a negative number to the second parameter of string.slice() to report an offset from the end of the string:

string.slice(4, -2)

String.slice () method

string.slice(indiceInicio [, indiceFim])

Returns: String

  • The second parameter is optional. If omitted and the first parameter is not negative, the returned value will be a string from the initial offset to the end of the main string.
  • If the first parameter is negative and the second one is omitted the value returned will be a string from the end.
  • If the second parameter is also negative and greater than the first parameter, the value returned will be as follows:

      string="os dois parâmetros negativos";
      string.slice(-9, -2)
      do final para o inicio primeiro parâmetro (-9) resulta negativos
      segundo parãmetro (-2) retira da string resultante acima e retorna negativ
    
  • See

string="os dois parametros negativos";
var resultado =string.slice(-9, -2);
console.log(resultado);

Below you can test the various situations for string.slice()

var mainString = "Oftalmotorrinolaringologista"
function showResults() {
	var form = document.forms[0]
	var param1 = parseInt(form.param1.options[form.param1.selectedIndex].value)
	var param2 = parseInt(form.param2.options[form.param2.selectedIndex].value)
	if (!param2) {
		form.result1.value = mainString.slice(param1)
	} else {
		form.result1.value = mainString.slice(param1, param2)
	}
}
<table border=1>
<form>
<tr><th>Metodo String</th><th>Metodo Parametros</th><th>Resultado</th></tr>
<tr>
<td>string.substring()</td><td>
(&nbsp;<select name="param1" onChange="showResults()">
	<option value=0>0
	<option value=1>1
	<option value=2>2
	<option value=3>3
	<option value=5>5
	<option value=10>10
	<option value=30>30
	<option value=-1>-1
	<option value=-3>-3
	<option value=-5>-5
	<option value=-10>-10
</select>,
<select name="param2" onChange="showResults()">
    <option>(Não)
    <option value=1>1
    <option value=3>3
    <option value=5>5
    <option value=10>10
    <option value=30>30
    <option value=-1>-1
    <option value=-3>-3
    <option value=-5>-5
    <option value=-10>-10
</select>&nbsp;) </td>
<td><input type="text" name="result1" value="Oftalmotorrinolaringologista" size=25></td>
</tr>
</form>
</table>

String.substring () method

string.substring(indiceA, indiceB)

Returns: Character string between index values indexA and indexB

  • The parameters of this method are the initial and final index values of the main string, from which the excerpt must be removed. An important item to note is that the snippet reaches it but does not include the character pointed to by the highest index value.

  • It makes no difference what index value in the parameters is greater than the other: the method starts the snippet from the smallest value and continues to (but does not include) the highest value. If the two values are the same, the method returns an empty string; and if you omit the second parameter, the end of the string is considered as the end point.

Below you can test the various situations for string.substring()

var mainString = "Oftalmotorrinolaringologista"
function showResults() {
	var form = document.forms[0]
	var param1 = parseInt(form.param1.options[form.param1.selectedIndex].value)
	var param2 = parseInt(form.param2.options[form.param2.selectedIndex].value)
	if (!param2) {
		form.result1.value = mainString.substring(param1)
	} else {
		form.result1.value = mainString.substring(param1, param2)
	}
}
<table border=1>
<form>
<tr><th>Metodo String</th><th>Metodo Parametros</th><th>Resultado</th></tr>
<tr>
<td>string.substring()</td><td>
(&nbsp;<select name="param1" onChange="showResults()">
	<option value=0>0
	<option value=1>1
	<option value=2>2
	<option value=3>3
	<option value=5>5
	<option value=10>10
	<option value=30>30
</select>,
<select name="param2" onChange="showResults()">
    <option>(Não)
    <option value=1>1
    <option value=3>3
    <option value=5>5
    <option value=10>10
    <option value=30>30
    <option value=-1>-1
    <option value=-5>-5
    <option value=-10>-10
</select>&nbsp;) </td>
<td><input type="text" name="result1" value="Oftalmotorrinolaringologista" size=25></td>
</tr>
</form>
</table>
    
20.11.2018 / 13:51
0

slice() works as substring() with some different behaviors.

Syntax

string.slice (start, stop); string.substring (start, stop);

What they have in common:

If start is equal to stop: returns empty string

If stop is omitted: Extract characters to the end of the string

If one of the arguments is greater than the length of the string, the length of the string will be used instead.

Distinctions from substring () :

If start > stop, then the substring will exchange these 2 arguments. If one of the arguments is negative or is NaN, it will be treated as 0.

Distinctions of slice () :

If start > stop, slice () will NOT change the 2 arguments. If start is negative: set char at the end of the string, just like substr () in Firefox. This behavior is observed in Firefox and IE. If stop is negative: set stop to: string.length - Math.abs (stop) (original value), except limited to 0 (therefore Math.max (0, string.length + stop)) as covered in the ECMA specification.

Font

    
19.11.2018 / 23:27