jsB@nk.com


Free JavaScript Codes Download

Translate this page to English Translate this page to France Translate this page to Vietnamese

Mã JS tách từng ký tự

Nơi hỗ trợ và giải đáp về JavaScript nói chung, cũng như các mã JavaScript có trên jsB@nk.com nói riêng.

Mã JS tách từng ký tự

Postby nguyenthanhphong on Thu Feb 14, 2008 5:19 am

Mình không biết làm sao để viết đoạn mã dùng để tách từng ký tự trong chuỗi ký tự mình nhập vào.
Ví dụ: nhập vào chữ abcd, nhấn nút "Tách"
thì sẽ hiển thị ra là "chữ đầu a chữ b chữ c chữ cuối d"
nguyenthanhphong
 
Posts: 6
Joined: Thu Feb 14, 2008 5:11 am


Postby Cao Phong on Sat Feb 16, 2008 1:04 pm

kiểu String trong JavaScript có phương thức split có thể dùng để tách chuỗi đó bạn
[hide]
Code: Select all
var str = "abcd";
var res = str.split('');
[/hide]
Unlimited hosting: Hostgator, Bluehost, H9, IX
User avatar
Cao Phong
 
Posts: 515
Joined: Wed Aug 29, 2007 3:44 am
Location: Here

Cách dùng

Postby nguyenthanhphong on Sun Feb 17, 2008 2:17 am

Bạn có thể cho mình xin đoạn mã hoàn chỉnh không, mình mới học java thôi :D
Mã theo cấu trúc gồm 1 textbox, 1 button và 1 textarea.
Mình nhập dòng chữ vào textbox, sau đó nhấn nút button thì kết quả sẽ hiện ra ngay textarea.
<body>
<input type="text" name="chu">
<input type="button" value="Tạo" name="B3">
<textarea rows="2" name="ketqua" cols="20"></textarea>

</body>
nguyenthanhphong
 
Posts: 6
Joined: Thu Feb 14, 2008 5:11 am

Postby Cao Phong on Sun Feb 17, 2008 5:54 am

Code mà bạn cần
[hide]
Code: Select all
<script>
function tachchu()
{
   var arr = document.f.chu.value.split('');
   var len = arr.length;
   var res = '';
   for(var i = 0; i < len; i++)
   {
      if(i == 0)
         res += 'Chu dau ' + arr[i] + ', ';
      else if(i == len - 1)
         res += 'Chu cuoi ' + arr[i];
      else
         res += 'Chu ' + arr[i] + ', ';
   }
   return res;
}
</script>
<form name="f">
<input type="text" name="chu">
<input type="button" value="Tạo" name="B3" onclick="document.f.ketqua.value = tachchu();">
<textarea rows="2" name="ketqua" cols="20"></textarea>
</form>
[/hide]
Unlimited hosting: Hostgator, Bluehost, H9, IX
User avatar
Cao Phong
 
Posts: 515
Joined: Wed Aug 29, 2007 3:44 am
Location: Here

Thank !!

Postby nguyenthanhphong on Sun Feb 17, 2008 7:08 am

Cảm ơn nhiều
Mà mình quên mất một trường hợp, nếu là ký tự trắng thì sẽ trở thành chữ trang
Chỉ cho nhập từ a-z và trắng thôi
Giúp cái này luôn nha :D
nguyenthanhphong
 
Posts: 6
Joined: Thu Feb 14, 2008 5:11 am


Postby Cao Phong on Sun Feb 17, 2008 12:55 pm

code này nè bạn

[hide]
Code: Select all
<script>
function tachchu()
{
   var arr = document.f.chu.value.split('');
   var len = arr.length;
   var res = '';
   for(var i = 0; i < len; i++)
   {
      if(i == 0)
         res += 'Chu dau ' + arr[i] + ', ';
      else if(i == len - 1)
         res += 'Chu cuoi ' + arr[i];
      else if(arr[i] == ' ')
         res += 'Chu trang';
      else
         res += 'Chu ' + arr[i] + ', ';
   }
   return res;
}
function testkey()
{
   var keycode;
   var inp = document.f.chu.value;
   if (window.event) keycode = window.event.keyCode;
   else if (e) keycode = e.which;
   if((keycode >= 65 && keycode <= 90) || (keycode >= 97 && keycode <= 122) || keycode == 32)
   {
      return true;
   }
   return false;
}
</script>
<form name="f">
<input type="text" name="chu" onkeypress="return testkey()">
<input type="button" value="Tạo" name="B3" onclick="document.f.ketqua.value = tachchu();">
<textarea rows="2" name="ketqua" cols="20"></textarea>
</form>
[/hide]
Unlimited hosting: Hostgator, Bluehost, H9, IX
User avatar
Cao Phong
 
Posts: 515
Joined: Wed Aug 29, 2007 3:44 am
Location: Here

Tốt

Postby nguyenthanhphong on Mon Feb 18, 2008 4:43 am

Thank nhiều, mà còn thiếu trường hợp là ký tự trắng nằm ở cuối cùng :D
Mà được rồi bạn, có ai mà nhập chữ cuối là trắng đâu mà lo :D
nguyenthanhphong
 
Posts: 6
Joined: Thu Feb 14, 2008 5:11 am

Postby Cao Phong on Mon Feb 18, 2008 11:20 am

các hàm cắt bỏ các kí tự trống bên trái, phải và cả 2 bên của 1 chuỗi:

[hide]
Code: Select all
// Removes leading whitespaces
function LTrim( value ) {
   
   var re = /\s*((\S+\s*)*)/;
   return value.replace(re, "$1");
   
}

// Removes ending whitespaces
function RTrim( value ) {
   
   var re = /((\s*\S+)*)\s*/;
   return value.replace(re, "$1");
   
}

// Removes leading and ending whitespaces
function trim( value ) {
   
   return LTrim(RTrim(value));
   
}

[/hide]
Unlimited hosting: Hostgator, Bluehost, H9, IX
User avatar
Cao Phong
 
Posts: 515
Joined: Wed Aug 29, 2007 3:44 am
Location: Here

Postby nguyenthanhphong on Mon Feb 18, 2008 11:24 am

Cảm ơn, bạn giúp quá nhiều nhỉ.
Mình đã làm xong rồi
nguyenthanhphong
 
Posts: 6
Joined: Thu Feb 14, 2008 5:11 am


Postby nguyenthanhphong on Mon Mar 10, 2008 2:13 pm

Bạn vào http://clubvt.info/taochu xem đi, mình làm theo mã nguồn này đấy :D :lol:
nguyenthanhphong
 
Posts: 6
Joined: Thu Feb 14, 2008 5:11 am

Next

Return to Support Center

Who is online

Users browsing this forum: No registered users and 0 guests

Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web
cron