From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../global_objects/string/substr/index.html | 140 +++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 files/pt-br/web/javascript/reference/global_objects/string/substr/index.html (limited to 'files/pt-br/web/javascript/reference/global_objects/string/substr') diff --git a/files/pt-br/web/javascript/reference/global_objects/string/substr/index.html b/files/pt-br/web/javascript/reference/global_objects/string/substr/index.html new file mode 100644 index 0000000000..c1e45beaef --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/string/substr/index.html @@ -0,0 +1,140 @@ +--- +title: String.prototype.substr() +slug: Web/JavaScript/Reference/Global_Objects/String/substr +tags: + - Descontinuado + - JavaScript + - Prototipo + - Referencia + - String + - metodo + - substr() +translation_of: Web/JavaScript/Reference/Global_Objects/String/substr +--- +
{{JSRef}}
+ +

O método substr() retorna uma parte da string, começando no índice especificado e estendendo-se por um determinado número de caracteres posteriormente.

+ +
{{EmbedInteractiveExample("pages/js/string-substr.html")}}
+ +

Sintaxe

+ +
str.substr(start[, length])
+ +

Parâmetros

+ +
+
start
+
Local para começar a extrair os caracteres.
+
length
+
Opcional. O número de caracteres a serem extraídos.
+
+ +

Valor de retorno

+ +

Uma nova string contendo a seção extraída da string fornecida.

+ +

Descrição

+ +

O substr() extrai caracteres de comprimento de uma str, contando a partir do índice inicial.

+ + + +
+

Nota: No Microsoft JScript, valores negativos no argumento start não são considerados como referência ao final da string.

+
+ + + +

Exemplos

+ +

Usando substr()

+ +
var aString = 'Mozilla';
+
+console.log(aString.substr(0, 1));   // 'M'
+console.log(aString.substr(1, 0));   // ''
+console.log(aString.substr(-1, 1));  // 'a'
+console.log(aString.substr(1, -1));  // ''
+console.log(aString.substr(-3));     // 'lla'
+console.log(aString.substr(1));      // 'ozilla'
+console.log(aString.substr(-20, 2)); // 'Mo'
+console.log(aString.substr(20, 2));  // ''
+ +

Polyfill

+ +

JScript da Microsoft não suporta valores negativos para o índice de start. Se você deseja usar esse recurso, você pode usar o seguinte código de compatibilidade para evitar esse erro:

+ +
// only run when the substr() function is broken
+if ('ab'.substr(-1) != 'b') {
+  /**
+   *  Get the substring of a string
+   *  @param  {integer}  start   where to start the substring
+   *  @param  {integer}  length  how many characters to return
+   *  @return {string}
+   */
+  String.prototype.substr = function(substr) {
+    return function(start, length) {
+      // call the original method
+      return substr.call(this,
+      	// did we get a negative start, calculate how much it is from the beginning of the string
+        // adjust the start parameter for negative value
+        start < 0 ? this.length + start : start,
+        length)
+    }
+  }(String.prototype.substr);
+}
+
+ +

Especificações

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES3')}}{{Spec2('ES3')}}Defined in the (informative) Compatibility Annex B. Implemented in JavaScript 1.0.
{{SpecName('ES5.1', '#sec-B.2.3', 'String.prototype.substr')}}{{Spec2('ES5.1')}}Defined in the (informative) Compatibility Annex B
{{SpecName('ES6', '#sec-string.prototype.substr', 'String.prototype.substr')}}{{Spec2('ES6')}}Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers
{{SpecName('ESDraft', '#sec-string.prototype.substr', 'String.prototype.substr')}}{{Spec2('ESDraft')}}Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers
+ + + + + +

{{Compat("javascript.builtins.String.substr")}}

+ +

Veja também

+ + -- cgit v1.2.3-54-g00ecf