Wycinanie ciągu znaków przy N-tym wystąpieniu znaku
To, co chcę zrobić, to wziąć łańcuch znaków, taki jak this.those.that
i uzyskać podłańcuch do lub z n-tego wystąpienia znaku. Tak więc od początku łańcucha do drugiego wystąpienia .
zwróci this.those
. Podobnie, od drugiego wystąpienia .
do końca łańcucha zwraca that
. Przepraszam, jeśli moje pytanie jest mgliste, to nie jest takie łatwe do wyjaśnienia. Ponadto, proszę nie sugerować tworzenia dodatkowych zmiennych, a wynik będzie w łańcuchu, a nie tablicy.
5 answers
Można to zrobić bez tablic, ale wymaga to więcej kodu i jest mniej czytelne.
Ogólnie rzecz biorąc, chcesz użyć tylko tyle kodu, aby wykonać zadanie, a to również zwiększa czytelność. Jeśli okaże się, że to zadanie staje się problemem wydajności (benchmark it), wtedy możesz zdecydować się na rozpoczęcie refaktoryzacji pod kątem wydajności.
var str = 'this.those.that',
delimiter = '.',
start = 1,
tokens = str.split(delimiter).slice(start),
result = tokens.join(delimiter); // those.that
JsFiddle .
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-04-28 09:14:48
Spróbuj tego:
"qwe.fs.xczv.xcv.xcv.x".replace(/([^\.]*\.){3}/, '');
"xcv.xcv.x"
"qwe.fs.xczv.xcv.xcv.x".replace(/([^\.]*\.){**nth**}/, '');
- Gdzie jest nth to ilość wystąpienia do usunięcia.
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-18 11:41:22
Jestem zakłopotany, dlaczego chcesz robić rzeczy wyłącznie z funkcjami string, ale myślę, że możesz zrobić coś takiego:
//str - the string
//c - the character or string to search for
//n - which occurrence
//fromStart - if true, go from beginning to the occurrence; else go from the occurrence to the end of the string
var cut = function (str, c, n, fromStart) {
var strCopy = str.slice(); //make a copy of the string
var index;
while (n > 1) {
index = strCopy.indexOf(c)
strCopy = strCopy.substring(0, index)
n--;
}
if (fromStart) {
return str.substring(0, index);
} else {
return str.substring(index+1, str.length);
}
}
Jednak zdecydowanie opowiadałbym się za czymś w rodzaju znacznie prostszego kodu Alexa.
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-03-31 02:43:02
Na wypadek, gdyby ktoś potrzebował zarówno tego, jak i tych.że " w sposób jak alex opisał w swoim komentarzu , Oto zmodyfikowany kod:
var str = 'this.those.that',
delimiter = '.',
start = 1,
tokens = str.split(delimiter),
result = [tokens.slice(0, start), tokens.slice(start)].map(function(item) {
return item.join(delimiter);
}); // [ 'this', 'those.that' ]
document.body.innerHTML = result;
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-05-23 12:18:01
Jeśli naprawdę chcesz trzymać się metod string, to:
// Return a substring of s upto but not including
// the nth occurence of c
function getNth(s, c, n) {
var idx;
var i = 0;
var newS = '';
do {
idx = s.indexOf(c);
newS += s.substring(0, idx);
s = s.substring(idx+1);
} while (++i < n && (newS += c))
return newS;
}
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-03-31 02:50:21