Javascript Strings


Simple string declaration.
<html> <body> <script>
var astr="hello";
var bstr=new String("Hello world!");
alert("astr = "+astr+", bstr =  "+bstr);

</script> </body></html>






toUpperCase, toLowerCase

<html> <body> <script>
var astr="hello";
var bstr=new String("Hello world!");
document.write(astr.toUpperCase()+"<br>");
document.write(bstr.toLowerCase()+"<br>");
</script> </body></html>




<html><head></head><body>
<script>
var i=0;
var astr=new String('Hello World!');
document.write("Length: ",astr.length,"<br>");
document.write("ValueOf: ",astr.valueOf(),"<br>");
document.write("ToString: ",astr.toString(),"<br>");
document.write("+: ",astr+",New World! ","<br>");
for(i=0;i<astr.length;i++){
document.write("astr[",i,"]= ",astr[i],"<br>");}
</script></body></html>





Upper,Lower,CharAt,IndexOf,LastIndexOf
<html><head></head><body> <script>
var i=0;
var astr=new String('Hello World!');
document.write("UpperCase: ",astr.toUpperCase(),"<br>");
document.write("LowerCase: ",astr.toLowerCase(),"<br>");
document.write("ChatAt: ",astr.charAt(4),"<br>");
document.write("indexOf: ",astr.indexOf('o'),"<br>");
document.write("indexOf2: ",astr.indexOf('o',5),"<br>");
document.write("LastIndexOf: ",astr.lastIndexOf('l'),"<br>");
document.write("IndexOf: ",astr.indexOf('World'),"<br>");
</script></body></html>




Split,Slice,Concat,Substring
<html><head></head><body> <script>
var i=0;
var astr=new String('Hello World!');
document.write("Slice(2,8): ",astr.slice(2,8),"<br>");
document.write("Substring(2,8): ",astr.substring(2,8),"<br>");
document.write("Split: ",astr.split(" "),"<br>");
document.write("Concat: ",astr.concat(" New World."),"<br>");
</script></body></html>



Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου