javascript String functions
// multiline string old school tip trick
// create elemnt div style none or hidden with text
var sMulitilineString = document.getElementById('idHiddenDiv').innerHTML; // get text from hidden div
// ES6 with quotes
const htmlString = 'Say hello to
multi-line
strings!';
(typeof "813") // string
(typeof 813) // number
parseInt("10.33") // returns 10 string to integer
parseInt('2def8'); // 2
parseInt('a2def8'); // NaN
parseFloat("10.33") // returns 10.33 string to float
var test_1 = 'string';
var test_2 = ' example';
var suma = test_1 + test_2;
var length = suma.length; // 14
var le = test_1.length; // 6
charAt(index)
/* Returns the character at the specified index */
var str = new String('Sample');
str.charAt(3)); // p
// startsWith
var str = "HELLO WORLD";
var res = str.charAt(0); // H
// endsWith
var res = str.charAt(str.length - 1); // D
charCodeAt(index)
/* Returns an integer representing the Unicode encoding
of the character at the specified location */
var str = new String('Sample');
str.charCodeAt(3)); // 112
fromCharCode([code1[, code2[, ...[, codeN]]]])
/* Returns a string from a number of Unicode character values */
var e = window.event;
var str = String.fromCharCode(e.keyCode); // keydown etc.
concat([item1[, item2[, . . . [, itemN]]]])
/* Returns a new array consisting of a combination of two or more arrays */
var arr1 = new Array("Hello, ", "Dolly uranove");
var arr2 = new Array("World!", "dort");
var myConcatArray = arr1.concat(arr2);
alert(myConcatArray[3]); // dort
concat([string2[, string3[, . . . [, stringN]]]])
/* Returns a string value containing the concatenation
of two or more supplied strings */
var string1 = new String("Hello, ");
var string2 = new String("World!");
var myConcatString = string1.concat(string2); // Hello, World!
indexOf(subString[, startIndex])
/* Returns the character position where the first occurrence
of a substring occurs within a String object */
lastIndexOf(substring[, startindex])
/* Returns the last occurrence of a substring within a String object */
localeCompare(stringExp)
/* Returns a value indicating whether two strings
are equivalent in the current locale */
var str1 = "1";
var str2 = "2";
var n = str1.localeCompare(str2); // -1
var str1 = "2";
var str2 = "2";
var n = str1.localeCompare(str2); // 0
var str1 = "a";
var str2 = "b";
var n = str1.localeCompare(str2); // -1
var str1 = "b";
var str2 = "a";
var n = str1.localeCompare(str2); // 1
match(rgExp)
/* Executes a search on a string using a regular expression
pattern, and returns an array containing the results of that search */
var rgExp = /some text/g;
replace(rgExp, replaceText)
/* Returns a copy of a string with text replaced
using a regular expression or search string */
search(rgExp)
/* Returns the position of the first substring
match in a regular expression search */
n // A newline character
. // Any character except a newline
r // A carriage return character
t // A tab character
b // A word boundary (the start or end of a word)
B // Anything but a word boundary
d // Any digit (same as [0-9])
D // Anything but a digit (same as [^0-9])
s // Single whitespace (space, tab, newline, etc.)
S // Single nonwhitespace
w // A "word character” (same as [A-Za-z0-9_])
W // A "nonword character” (same as [^A-Za-z0-9_])
slice(start, [end])
/* Returns a section of an array or string */
var myString = new String("This is a test");
var mySlice = myString.slice(2,6); // is i // pos: 2 to 5 // 0==T, 1==h, 2==i, 3==s, 4==" ", 5==i
mySlice = myString.slice(-3); // est // end of string
mySlice = myString.slice(0); // This is a test
split([separator[, limit]])
/* Returns the array of strings that results
when a string is separated into substrings */
var str = "one two three";
var words = str.split(" ");
var len = words.length; // 3
substring(start, end)
/* Returns the substring at the specified location
within a String object */
var inpTxt = "Hello there";
alert(inpTxt.substring(6, 10)); // ther
substr(start [, length ])
// Returns a substring beginning at a specified
// location and having a specified length
var s = "The love in Spain.";
ss = s.substr(12, 5); // Spain
tolocaleLowerCase()
/* Returns a string where all alphabetic characters
have been converted to lowercase, taking into account
the host environment's current locale */
toUpperCase()
/* Returns a string where all alphabetic characters
have been converted to uppercase */
var myString = "My text";
myString.anchor //places an HTML anchor with a NAME attribute around specified text in the object
myString.big
myString.blink
myString.bold
myString.concat
myString.fixed
myString.fontcolor
myString.fontsize
myString.charAt
myString.charCodeAt
myString.indexOf
myString.italics
myString.lastindexOf
myString.length
myString.link
myString.match
myString.replace
myString.search
myString.slice
myString.small
myString.split
myString.strike
myString.sub //places HTML SUB tags around text in a String object
myString.substr
myString.substring
myString.sup
myString.toLowerCase
myString.toString
myString.toUpperCase
myString.valueOf
var str = "58";
var res = str.valueOf();
var type = (typeof res); // string
var str = 58;
var res = str.valueOf();
var type = (typeof res); // number
77LW NO topic_id
AD
Další témata ....(Topics)
Ke konverzi diakritiky slouží v PHP mnoho funkcí, ale výsledky jsou různé.
Někdy je lepší si udělat vlastní funkci.
Níže uvedená funkce zamění v řetězci paznaky, za jejich čitelný ekvivalent:
Někdy je lepší si udělat vlastní funkci.
Níže uvedená funkce zamění v řetězci paznaky, za jejich čitelný ekvivalent:
/**
* konverze diakritiky - okhelp.cz
*/
function diakritika($vstup) {
$arMala = array('á'=> 'á', 'ÄŤ'=> 'č', 'ÄŹ'=> 'ď', 'Ă©'=> 'é', 'Ä›'=> 'ě',
'Ă'=> 'i', 'Ĺ'=> 'ň', 'Ăł'=> 'ó', 'Ĺ™'=> 'ř', 'š'=> 'š', 'ĹĄ'=> 'ť', 'Ăş'=> 'ú', 'ĹŻ'=> 'ů', 'Ă˝'=> 'ý', 'Ĺľ'=> 'ž' );
$arVelka = array('Ă'=> 'Á', 'Ä©'=> 'Č', 'Ĭ'=> 'Ď', 'É'=> 'É', 'ı'=> 'Ě',
'Ă«'=> 'Í', 'Ň'=> 'Ň', 'Ă"'=> 'Ó', 'Ĺ'=> 'Ř', 'Ĺ '=> 'Š', 'Ť'=> 'Ť', 'ñ'=> 'Ú', 'ĹŽ'=> 'Ů', 'ö'=> 'Ý', 'Ĺ˝'=> 'Ž' );
foreach ($arMala as $key=>$value) {
$vstup = str_replace($key, $value, $vstup);
}
foreach ($arVelka as $key=>$value) {
$vstup = str_replace($key, $value, $vstup);
}
return $vstup;
}
mb_internal_encoding("UTF-8");
$s = 'áčď';
$novy = diakritika($s); // $novy == ačď
/*******************
* převede kůň na kun
* máte možnosti buď si napíšete funkci sami něci jako diakritikaKlasicka($vstup)
* nebo použijete $word = iconv("utf-8", "us-ascii//TRANSLIT", $word);
*ale výsledek otestujte, ne vždy je u iconv uspokojivý
*/
$word = "kůň";
$word = iconv("utf-8", "us-ascii//TRANSLIT", $word);
function diakritikaKlasicka($vstup) {
$arMala = array('á'=> 'a', 'č'=> 'c', 'ď'=> 'd', 'é'=> 'e', 'ě'=> 'e',
'í'=> 'i', 'ň'=> 'n', 'ó'=> 'o', 'ř'=> 'r', 'š'=> 's', 'ť'=> 't', 'ú'=> 'u',
'ů'=> 'u', 'ý'=> 'y', 'ž'=> 'z' );
$arVelka = array('Á'=> 'A', 'Č'=> 'C', 'Ď'=> 'D', 'É'=> 'E', 'Ě'=> 'E',
'Í'=> 'I', 'Ň'=> 'N', 'Ó'=> 'O', 'Ř'=> 'R', 'Š'=> 'S', 'Ť'=> 'T',
'Ú'=> 'U', 'Ů'=> 'U', 'Ý'=> 'Y', 'Ž'=> 'Z' );
foreach ($arMala as $key=>$value) {
$vstup = str_replace($key, $value, $vstup);
}
foreach ($arVelka as $key=>$value) {
$vstup = str_replace($key, $value, $vstup);
}
return $vstup;
}
Soubory header, footer a další se nacházejí ve složce příslušného stylu například:
Můžeme v nich nastavit ikonu fóra, nebo údaje do patičky popřípadě atd.
mojeforum/styles/prosilver/template/overall_header.html
mojeforum/styles/prosilver/template/viewtopic_body.html
mojeforum/styles/prosilver/template/overall_footer.html
/mojeforum/cache/tpl_prosilver_overall_footer.html.php
/mojeforum/cache/tpl_prosilver_overall_header.html.php
Můžeme v nich nastavit ikonu fóra, nebo údaje do patičky popřípadě atd.
htaccess rady a zkušenosti
Testování kódu htaccess ZDEPříklady na jakpsatweb
Dokumentace na apache.org
Testování regexů
Komentar nesmi byt na stejnem radku, jako kod. Zde jsou komentare, na konci radku (vedle) prikazu, jen kvuli snadnejsimu pochopeni kodu.
RewriteCond string (regex1) # tento radek nebude fungovat, koment musi byt na samostatnem radku, nejlepe nad provadenym kodem.
# tak je to spravne, komentar na samostatnem radku a nejlepe nad ukolem
RewriteCond string (regex1)
RewriteRule dosáhne jen na regex předchozího RewriteCond!
RewriteCond string (regex1) # nedosahne
RewriteCond string (regex2)
RewriteRule (regex) %1 # v %1 bude regex2
Číslo za % udává, kolikátá skupina předchozího regexu v RewriteCond bude použita. %0 vezme vše ze všech skupin.
Pokud skupina neexistuje, tak se vypíše jako %N , kde N je číslo skupiny, která nebyla nalezena.
$1 číslo za $ udává kolikátá skupina regexu bude použita, ale platí jen pro RewriteRule z jeho vlastního regexu. $0 vezme všchny skupiny (group) regexu z RewriteRule.
RewriteCond může být několikrát za sebou, ale jakmile se jedna z těchto podmínek nesplní, tak se nesplní ani následující RewriteRule!
RewriteCond # podmínka nesplněna
RewriteCond # splněna
RewriteRule # se nevykoná, protože jedna z podmínek nebyla splněna
RewriteRule přísluší vždy k předchozímu - předchozí skupině RewriteCond. Následující RewriteRule už je samostatné viz příklad:
RewriteCond
RewriteRule # tohle reaguje na předchozí podmínku a vykoná se, když je podmínka splněna
RewriteRule # tohle už je samostatné a nebude brát ohled na podmínku - podmínky v předchozích řádcích.
Využití regexu, který bude vytvořen až v následujícím RewriteRule. Regex Back-Reference Availability
Date: 27.02.2020 - 11:11
Jak naformátovat číslo například 1000 aby výstup byl 1 000.
How formatting Numbers in javascript
// in us return 1,000.00 in czech cz return 1 000,00
var newNumber = format_Of_Number ('cz', 1000.00); // cz is czech format - us is us format of number
function format_Of_Number (stat, num) {
stat = stat.toLowerCase();
nStr = this + '';
var x = nStr.split('.');
var x1 = x[0];
var x2;
if(stat=='cz')
x2 = x.length > 1 ? ',' + x[1] : ''; // nahradime tecku carkou
else if(stat=='us')
x2 = x.length > 1 ? '.' + x[1] : ''; // us format
var rgx = /(d+)(d{3})/;
while (rgx.test(x1)) {
if(stat=='cz')
x1 = x1.replace(rgx, '$1' + ' ' + '$2'); // nahradime mezerou cesky format 1 000,00
else if(stat=='us')
x1 = x1.replace(rgx, '$1' + ',' + '$2'); // us format 1,000.00
}
return x1 + x2;
}
php bývá vyčítano, že proměnné jsou typově nejednoznačné. To je sice pravda, ale php má funkci, která vrátí typ proměnné v dané části kodu.
$var = 10;
// funkce gettype vrací retezec s nazvem typu
// tak, jak jej vydi prekladac pri linkovani kodu
$s = gettype($var); // $s == 'integer'
$var = 'some text';
$s = gettype($var); // $s == 'string'
$var = 10.1;
$s = gettype($var); // $s == 'double'
Editace: 8.2.2021 - 22:24
Počet článků v kategorii: 77
Url:javascript-string-functions