Strings

Índice da página:

Strings

O JavaScript é bastante poderoso no manuseamento de Strings, fornecendo ao programador uma total flexibilidade no seu manuseamento.
Abaixo apresentamos os métodos disponíveis para manuseamento de strings.

• length (string.length) - retorna o tamanho da string (quantidade de bytes).

Exemplo:

<html>
<head>
<title>String Lenght</title>

<script language="JavaScript" type="text/javascript">
function Tamanho(){
	result = prompt ("Escreva Seu Nome:", "")
   alert("Seu Nome é: "+result+"\nComprimento da String: "+result.length+" Caracteres") } 
</script>

</head>
<body topmargin="0" leftmargin="0">
<form>
<div>Tamanho String:
<input type="button" value="Ver" onclick="Tamanho()"></div><br>
</form>
</body>
</html>
Ver Índice

CharAt

• charAt (string.charAt) - retorna o caracter da posição especificada (inicia em 0)

Exemplo:

<html>
<head>
<title>String Posição</title>

<script language="JavaScript" type="text/javascript">
function Position(){
	result = prompt ("Escreva Seu Nome:", "")
	tamanho = result.length
	pos = prompt ("Escreva Número entre 0 e "+(tamanho-1)+":", "")
   alert("Seu Nome é: "+result+"\nO Caractere "+pos+" corresponde a: "+result.charAt(pos)) } 
</script>

</head>
<body topmargin="0" leftmargin="0">
<form>
<div>Posição String:
<input type="button" value="Ver" onclick="Position()"></div><br>
</form>
</body>
</html>
Ver Índice

IndexOf

• indexOf [string.indexOf("string")] - retorna o número da posição onde começa a primeira "string"

Exemplo:

<html>
<head>
<title>IndexOf</title>

<script language="JavaScript" type="text/javascript">
function Position(){
	result = "Isto é um teste de escrita."
   alert("IndexOf teste = " + result.indexOf("teste")) } 
</script>

</head>
<body topmargin="0" leftmargin="0">
<form>
String = "Isto é um teste de escrita."<br>
String Index = "teste"<br><br>
Index String:
<input type="button" value="Ver" onclick="Position()"></div><br>
</form>
</body>
</html>
Ver Índice

LastIndexOf

• lastIndexOf [string.lastIndexOf("string")] - retorna o número da posição onde começa a última "string"

Exemplo:

<html>
<head>
<title>LastIndexOf</title>

<script language="JavaScript" type="text/javascript">
function Position(){
	result = "Este teste é um bom exemplo de teste."
   alert("lastIndexOf teste = " + result.lastIndexOf("teste")) } 
</script>

</head>
<body topmargin="0" leftmargin="0">
<form>
String = "Este teste é um bom exemplo de teste."<br>
String Index = "teste"<br><br>
Index String:
<input type="button"  value="Ver" onclick="Position()"><br>
</form>
</body>
</html>
Ver Índice

SubString

• substring string.substring(index1, index2) - retorna o conteúdo da string que corresponde ao intervalo especificado. Começando no caracter posicionado em index1 e terminando no caractere imediatamente anterior ao valor especificado em index2.

Exemplo:

<html>
<head>
<title>Substring</title>

<script language="JavaScript" type="text/javascript">
function Position(){
	result = "Esta é uma string de teste."
   alert("Substring(11,17) = " + result.substring(11,17)) } 
</script>

</head>
<body topmargin="0" leftmargin="0">
<form>
String = "Esta é uma string de teste."<br>
Substring = (11,17)<br><br>
Sustring:
<input type="button" class="bot" value="Ver" onclick="Position()"><br>
</form>
</body>
</html>
Ver Índice

UpperCase

• UpperCase string.toUpperCase() - Transforma o conteúdo da string para maiúsculo.

Exemplo:

<html>
<head>
<title>UpperCase</title>

<script language="JavaScript" type="text/javascript">
function Position(){
   result = prompt ("Escreva Seu Nome:", "")
   alert("UpperCase = " + result.toUpperCase()) } 
</script>

</head>
<body topmargin="0" leftmargin="0">
<form>
UpperCase:
<input type="button" value="Ver" onclick="Position()"><br>
</form>
</body>
</html>
Ver Índice

LowerCase

• LowerCase string.toLowerCase() - Transforma o conteúdo da string para minúsculo.

Exemplo:

<html>
<head>
<title>LowerCase</title>

<script language="JavaScript" type="text/javascript">
function Position(){
   result = prompt ("Escreva Seu Nome:", "")
   alert("LowerCase = " + result.toLowerCase()) } 
</script>

</head>
<body topmargin="0" leftmargin="0">
<form>
LowerCase:
<input type="button" class="bot" value="Ver" onclick="Position()"><br>
</form>
</body>
</html>
Ver Índice

Escape

• escape esc=escape("string") - Transforma caracteres especiais para ascii precedido de %.

Exemplo:

<html>
<head>
<title>Escape</title>

<script language="JavaScript" type="text/javascript">
var mt=""
var text=""
function ver(texto) {
    for(n=0; n < texto.length; ++n){
        mt=texto.substring(n,n + 1);
        esc = escape(mt);
        text += mt+"="+esc+"   ";}
    document.form1.Res.value=text;}
</script>

</head>
<body>
<form name="form1">
Caracteres:
<input type="text" value="«ç]&$" name="Texto" readonly style="border:none" size="6">
<input type="button" value="Ver" onclick="ver(Texto.value)"><br><br>
<center>
<input type="text" name="Res" readonly style="border:none" size="50">
</center>
</form>
</body>
</html>
Ver Índice

UnEscape

• unescape esc=unescape("string") - Transforma códigos ascii precedidos de % para caracteres.

Exemplo:

<html>
<head>
<title>UnEscape</title>
<script language="JavaScript" type="text/javascript">
var mt=""
var text=""
function ver(texto) {
    for(n=0; n < 3; ++n){
        mt=texto.substring(4*n,n*4 + 4);
        esc = unescape(mt);
        text += mt+"="+esc+"   ";}
    document.form1.Res.value=text;}
</script>

</head>
<body>
<form name="form1">
<div>Ascii:
<input type="text" value="%5D %26 %24" name="Texto" readonly style="border:none" size="12">
<input type="button" value="Ver" onclick="ver(Texto.value)"></div><br>
<center>
<input type="text" name="Res" readonly style="border:none" size="48">
</center>
</form>
</body>
</html>
Ver Índice