Trang Chủ | Diễn Đàn | Thành Viên (Đăng Ký) | Tìm Kiếm | Tutorial Room
Java - Xử lý chuỗi
Hỏi đáp, trao đổi về lập trình Java.
- Các câu hỏi về JavaScript xin hãy gởi vào box HTML & JavaScript.
- Các câu hỏi về lập trình JSP/Server, J2EE xin hãy gởi vào box JSP/Servlet - J2EE.
Chú ý: Java FAQs
First page Previous page  (Page 1 )   1   Next page Last page
Xử lý chuỗi
Member
Member since
03:02 27-08-2006
Posts: 14
Fantasy Points: 53
question Posted at 01:52 28-03-2009 Move Move Topic   Pin/Unpin Pin Topic   Lock Lock Topic
Mình có vấn đề thế này. Giả sử mình muốn nhập vào 1 URL có dạng http://xxx, làm thế nào để mình có thể kiểm tra 1 chuỗi nhập vào có phải là 1 URL hay ko?
vd: Nhập http://vnn.vn -> đúng.
http://thanhnien.com.vn -> đúng.
else -> sai
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 1,409 time(s). 1 direct repli(es) and 5 indirect repli(es).
Title Poster
question Xử lý chuỗi
 
answer Re: Xử lý chuỗi
NBThanh
Re: Xử lý chuỗi
Forum Moderator
Member since
20:03 17-08-2001
Posts: 17,617
Fantasy Points: 153,010
Rank
answer Posted at 10:43 28-03-2009
Reply to Xử lý chuỗi (vinhsteven)
vinhsteven:
Mình có vấn đề thế này. Giả sử mình muốn nhập vào 1 URL có dạng http://xxx, làm thế nào để mình có thể kiểm tra 1 chuỗi nhập vào có phải là 1 URL hay ko?
vd: Nhập http://vnn.vn -> đúng.
http://thanhnien.com.vn -> đúng.
else -> sai

Như bạn nói: URL có dạng http://xxx
Nếu vậy thì bạn chỉ cần kiểm tra chuỗi có bắt đầu bằng http:// hay không :-)
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 1,407 time(s). 2 direct repli(es) and 3 indirect repli(es).
Title Poster
answer Re: Xử lý chuỗi
 
answer Re: Xử lý chuỗi
lech
answer Re: Xử lý chuỗi
nguyendien
Re: Xử lý chuỗi
Member
Member since
14:21 05-10-2006
Posts: 11
Fantasy Points: 39
answer Posted at 21:37 13-05-2009
Reply to Re: Xử lý chuỗi (NBThanh)
Giống như kiểm tra Email ý , thay đổi 1 chút là được :D
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 1,273 time(s). 0 direct repli(es) and 0 indirect repli(es).
Re: Xử lý chuỗi
Member
Member since
06:45 03-04-2008
Posts: 2
Fantasy Points: 7
Rank
answer Posted at 02:21 01-07-2009
Reply to Re: Xử lý chuỗi (NBThanh)
Dùng Regex mà bắt.
string patten = "^http[s]?(//)|(\).www.";
So khớp từ đầu và phía sau không quan tâm, có thể là http hay https đều được, dấu \ hay // cũng thế.ế.
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 1,131 time(s). 1 direct repli(es) and 2 indirect repli(es).
Title Poster
answer Re: Xử lý chuỗi
 
answer Re: Xử lý chuỗi
gadfly
Re: Xử lý chuỗi
Member
Member since
07:35 03-09-2002
Posts: 256
Fantasy Points: 4,887
Rank
answer Posted at 07:13 02-07-2009
Reply to Re: Xử lý chuỗi (nguyendien)
hay trick 1 chut la load Url url = new Url(string), no mà thảy ra cái exception là biết không phải well-formed rồi :)
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 1,102 time(s). 2 direct repli(es) and 0 indirect repli(es).
Title Poster
answer Re: Xử lý chuỗi
 
answer Re: Xử lý chuỗi
ntan
answer Re: Xử lý chuỗi
postman_flogsta
Re: Xử lý chuỗi
Member
Member since
22:40 20-03-2009
Posts: 37
Fantasy Points: 127
Rank
answer Posted at 22:03 02-07-2009
Reply to Re: Xử lý chuỗi (gadfly)
Cái này cũng đơn giản thôi mà. Bạn dùng hàm cắt bỏ các khoảng trống 2 bên của chuỗi nhập vào, sau đó kiểm tra xem 7 ký tự đầu tiên có phải là http://, hay http:\,... không. Kiểm tra trong chuỗi có dấu . không..
||
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 1,089 time(s). 0 direct repli(es) and 0 indirect repli(es).
Re: Xử lý chuỗi
Member
Member since
17:06 07-07-2009
Posts: 23
Fantasy Points: 82
Rank
answer Posted at 17:22 07-07-2009
Reply to Re: Xử lý chuỗi (gadfly)
@gadfly
-------------------------------------------------------------------
hay trick 1 chut la load Url url = new Url(string), no mà thảy ra cái exception là biết không phải well-formed rồi :)
-------------------------------------------------------------------

To bạn gadfly

Theo cách của bạn thì bị ngắt kết nối Internet có mà vỡ mặt phải không?
Bầu chọn cho bạn đã trả lời với Regular Expression :). Các cách còn lại cũng khá sáng tạo nhưng không dùng lại được.
Ví dụ : kiểm tra có chuỗi http, https sẽ không dùng lại được nếu yêu cầu thay đổi thay vì kiểm tra URL mà kiểm tra một loại khác như email chẳng hạn .Với RE thì thay đổi 1 dòng 1 thôi.

Thân
Reply Reply   Quote Quote   Edit Edit   Delete Delete   Report Report
This post has been viewed 1,042 time(s). 0 direct repli(es) and 0 indirect repli(es).
First page Previous page  (Page 1 )   1   Next page Last page

Permissions: Create Topic: No  |  Reply Topic: No  |  Attach File: No  |  Make Poll: No

Vietnamese Keyboard: AUTO TELEX VNI VIQR VIQR* OFF

Go top || Print page ||

All logos, trademarks and graphics artwork in this site are property of their respective owners.
Opinions expressed in articles within this site are those of their owners and may not reflect the opinion of TXBB.

TXBB: Home - Disclaimer - Help - Contact
Copyright (C) 2000-2006 TXBB. All rights reserved.

TreXanh Bulletin Board v2.0 (Build: #332 Nov 21, 2006)

DEBUG INFORMATION
Execution 0.694s - SQL used 9s - Concurrent process(es) 0