Jaka jest różnica między substr i substr?
Jaka jest różnica między
alert("abc".substr(0,2));
I
alert("abc".substring(0,2));
Obie wydają się emitować "ab".
8 answers
Różnica jest w drugim argumencie. Drugi argument substring
jest indeksem do zatrzymania (ale nie include), ale drugi argument substr
jest maksymalną długością do zwrócenia.
Linki?
Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substr
Https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/substring
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-19 11:41:06
substr
(MDN ) przyjmuje parametry jako (from, length)
.substring
(MDN ) przyjmuje parametry jako (from, to)
.
alert("abc".substr(1,2)); // returns "bc"
alert("abc".substring(1,2)); // returns "b"
Możesz pamiętać, że substring
pobiera indeksy, podobnie jak kolejna metoda ekstrakcji łańcuchów, slice .
Rozpoczynając od 0 możesz użyć jednej z metod.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-15 11:31:46
W odpowiedzi yatima2975 pojawia się dodatkowa różnica:]}
substr()
przyjmuje ujemną pozycję początkową jako przesunięcie od końca łańcucha. Nie.
From MDN :
Jeśli start jest ujemny, substr () używa go jako indeksu znaków z koniec sznurka.
Podsumowując różnice funkcjonalne:
substring(begin-offset, end-offset-exclusive)
gdzie begin-offset jest 0
lub większy
substr(begin-offset, length)
gdzie begin-offset może być również negative
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-19 21:56:04
Kolejna wpadka, na którą ostatnio natknąłem się jest taka, że w IE 8 "abcd".substr(-1)
błędnie zwraca "abcd"
, Podczas gdy Firefox 3.6 zwraca "d"
tak jak powinien. slice
działa poprawnie na obu.
Więcej na ten temat można znaleźć tutaj .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-06-01 14:16:40
Główną różnicą jest to, że
Substr () pozwala określić maksymalną długość zwracaną
Substring () pozwala określić indeksy, a drugi argument nie zawiera
Istnieje kilka dodatkowych subtelności między substr() i substr (), takich jak obsługa równych i negatywnych argumentów. Również notatka substring () i slice () są podobne, ale nie zawsze takie same.
//*** length vs indices:
"string".substring(2,4); // "ri" (start, end) indices / second value is NOT inclusive
"string".substr(2,4); // "ring" (start, length) length is the maximum length to return
"string".slice(2,4); // "ri" (start, end) indices / second value is NOT inclusive
//*** watch out for substring swap:
"string".substring(3,2); // "r" (swaps the larger and the smaller number)
"string".substr(3,2); // "in"
"string".slice(3,2); // "" (just returns "")
//*** negative second argument:
"string".substring(2,-4); // "st" (converts negative numbers to 0, then swaps first and second position)
"string".substr(2,-4); // ""
"string".slice(2,-4); // ""
//*** negative first argument:
"string".substring(-3); // "string"
"string".substr(-3); // "ing" (read from end of string)
"string".slice(-3); // "ing"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-17 04:12:34
Różnica jest drugim parametrem. Ich drugie parametry, podczas gdy obie liczby, oczekują dwóch różnych rzeczy:
Przy użyciu podłańcucha drugi parametr jest pierwszym indeksem, który nie zawiera:
var s = "string";
s.substring(1, 3); // would return 'tr'
var s = "another example";
s.substring(3, 7); // would return 'ther'
Podczas używania substr drugim parametrem jest liczba znaków do umieszczenia w podłańcuchu:
var s = "string";
s.substr(1, 3); // would return 'tri'
var s = "another example";
s.substr(3, 7); // would return 'ther ex'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-04 16:16:39
Slice vs Substr vs Substr vs [] Methods
Istnieją korzyści wydajnościowe dla każdej z tych metod javascript. Prosimy o odpowiednie korzystanie z tych funkcji.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-10-21 01:04:19
Duża różnica jest, substr()
jest przestarzałą metodą , która może być nadal używana, ale powinna być używana ostrożnie, ponieważ oczekuje się, że zostaną całkowicie usunięte w przyszłości. Powinieneś pracować nad usunięciem ich użycia z kodu. Oraz substring()
metoda powiodła się i określiła poprzednią.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-20 14:26:51