用String.substring方法,不小心會有越界異常。現實現一個沒拋出越界異常,越界就返回null,不過直接返回的再用其它方法,可能有Null異常。現還實現可以負index的,可能逆向的。
package com.chenlb.util;
public class StringUtil {
/**
* start與end均可負數<br/>
* start < end正向取, start > end逆向取<br/>
* 示例:str="I am chenlb"<br/>
* StringUtil.substring(str, 0, 12) -> null<br/>
* StringUtil.substring(str, 12, 12) -> null<br/>
* StringUtil.substring(str, 12, 13) -> null<br/>
* StringUtil.substring(str, 4, 4) -> ""<br/>
* StringUtil.substring(str, 0, 4) -> "I am"<br/>
* StringUtil.substring(str, -4, -1) -> "enl"<br/>
* StringUtil.substring(str, -2, 4) -> "lbI am"<br/>
* StringUtil.substring(str, 4, 0) -> "ma I"<br/>
* StringUtil.substring(str, -1, -4) -> "lne"<br/>
* StringUtil.substring(str, 1, -4) -> "Iblne"<br/>
* StringUtil.substring(str, 0, -4) -> "blne"<br/>
* StringUtil.substring(str, -4, 0) -> "enlb"<br/>
* @return 越界返回null, start==end返回空
* @author chenlb 2008-6-18 下午12:39:51
*/
public static String substring(String str, int start, int end) {
if(str == null) {
return null;
}
int len = str.length();
if(Math.abs(start) >= len) {
return null;
}
if(Math.abs(end) > len) {
return null;
}
StringBuilder sb = new StringBuilder();
if(end > start) { //正向
substring(sb, str, start, end);
} else if(end == start) {
return "";
} else { //逆向 end < start
substring(sb, str, end, start);
sb.reverse();
}
return sb.toString();
}
private static void substring(StringBuilder sb, String str, int start, int end) {
int len = str.length();
if(start < 0) {
if(end < 0) {
sb.append(str.substring(len+start, len+end));
} else {
sb.append(str.substring(len+start, len));
sb.append(str.substring(0, end));
}
} else {
sb.append(str.substring(start, end));
}
}
}
測試代碼:
public void testSubstring() {
String str = "I am chenlb";
assertEquals(null, StringUtil.substring(str, 0, 12));
assertEquals(null, StringUtil.substring(str, 12, 12));
assertEquals(null, StringUtil.substring(str, 12, 13));
assertEquals("", StringUtil.substring(str, 4, 4));
assertEquals("I am", StringUtil.substring(str, 0, 4));
assertEquals("am", StringUtil.substring(str, 2, 4));
assertEquals("I am chenlb", StringUtil.substring(str, 0, 11));
assertEquals("enl", StringUtil.substring(str, -4, -1));
assertEquals("lbI am", StringUtil.substring(str, -2, 4));
assertEquals("ma I", StringUtil.substring(str, 4, 0));
assertEquals("lne", StringUtil.substring(str, -1, -4));
assertEquals("Iblne", StringUtil.substring(str, 1, -4));
assertEquals("blne", StringUtil.substring(str, 0, -4));
assertEquals("enlb", StringUtil.substring(str, -4, 0));
}
posted @
2008-06-24 13:53 流浪汗 閱讀(552) |
評論 (0) |
編輯 收藏
Windows網絡命令行程序
ipconfig /all 查看配置
ipconfig /renew 刷新配置
ipconfig 管理 DNS 和 DHCP 類別 ID
Ping 測試連接
Arp 解決硬件地址問題
nbtstat 解決 NetBIOS 名稱問題
netstat 顯示連接統計
tracert 跟蹤網絡連接
pathping 測試路由器
posted @
2008-06-24 13:51 流浪汗 閱讀(269) |
評論 (0) |
編輯 收藏
今天運行下程序,報錯說“內存不夠”。在Tomcat可以擴大JVM的內存棧呢?然后看那bin目錄下啟動文件,找到catalina.bat文件的JAVA_OPTS(大概在103行,5.5.X),在再添加一個set JAVA_OPTS參數即可如:
set JAVA_OPTS=%JAVA_OPTS% -Xms100m -Xmx512m
posted @
2008-06-24 13:49 流浪汗 閱讀(371) |
評論 (0) |
編輯 收藏
前段時間學習Linux命令,偶然發現curl命令很有用。這里簡單介紹下。網絡上部分解析是:curl是一個利用URL語法在命令行方式下工作的文件傳輸工具。
它可以取得有規律的url的內容。比如:http://www.example.com/001.html 到 http://www.example.com/100.html ,它有一種表達式可以這些內容下載下來,這功能絕對比迅雷強,迅雷只支持一個變量,curl只你喜歡可任意多。它可繼點續傳,提交表單……
來看下簡單的使用:
1.查看響應的頭
curl -I http://chenlb.javaeye.com
現在正如robbin說的可以看下X-Runtime: 0.47101
2.在學校要代理才可以上javaeye.com。用-x設代理
curl -x proxy.gdut.edu.cn:8080 -I http://chenlb.javaeye.com
3.把返回的內容保存下來,用-o filename參數
curl -o chenlb.html http://chenlb.javaeye.com
4.保存內容時要filename很煩,用一個-O參數來指定用服務器的文件名,這個批量下載很有用。
curl -O http://baike.baidu.com/view/[1-2].htm
批量下載百科的1.htm 2.htm兩個頁面,這功能夠強。
我常用的就是以上四個。
5.很多要referer的,有-e參數可以設置
curl -o me.html -e http://www.javaeye.com http://chenlb.javaeye.com
還有很多很多參數,留給大家去發現,比如:發送數據,提交表單,設置用戶與密碼,用什么協議啊……
posted @
2008-06-24 13:47 流浪汗 閱讀(1509) |
評論 (0) |
編輯 收藏
java命令引入jar時可以-cp參數,但時-cp不能用通配符(多個jar時什么煩要一個個寫,不能*.jar),面通常的jar都在同一目錄,且多于1個。前些日子找到(發現)-Djava.ext.dirs太好。
如:
java -Djava.ext.dirs=lib MyClass
posted @
2008-06-22 23:58 流浪汗 閱讀(5096) |
評論 (0) |
編輯 收藏
javascript xslt 處理xml備忘錄。支持firefox。
參考:
w3school XSLT - 客戶端
http://www.w3school.com.cn/xsl/xsl_client.asp
如何使用Javascript XSLT 處理XML文件
http://java.chinaitlab.com/advance/533787.html
1.xml文件,cdcatalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>
2.xsl文件,cdcatalog.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v2007 (http://www.altova.com) -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/>
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
3.html文件,index.html
<html>
<body>
<script type="text/javascript">
var xml;
var xsl;
if(typeof window.ActiveXObject != 'undefined') {
xml = new ActiveXObject("Microsoft.XMLDOM");
xsl = new ActiveXObject("Microsoft.XMLDOM");
} else if(document.implementation && document.implementation.createDocument) { //mozilla
xml = document.implementation.createDocument("", "", null);
xsl = document.implementation.createDocument("", "", null);
}
// Load XML
xml.async = false;
xml.load("cdcatalog.xml");
// Load XSL
xsl.async = false;
xsl.load("cdcatalog.xsl");
// Transform
if(typeof window.ActiveXObject != 'undefined') {
document.write(xml.transformNode(xsl));
} else if(document.implementation && document.implementation.createDocument) { //mozilla
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
// transformToDocument方式
var result = xsltProcessor.transformToDocument(xml);
var xmls = new XMLSerializer();
document.write(xmls.serializeToString(result));
}
</script>
</body>
</html>
posted @
2008-05-18 19:02 流浪汗 閱讀(801) |
評論 (1) |
編輯 收藏
摘要: 自己實現的優先隊列 PriorityQueue
閱讀全文
posted @
2008-05-08 23:08 流浪汗 閱讀(1011) |
評論 (0) |
編輯 收藏
想到局域網上建一個dns服務器,昨天晚上搞了好久都不成,包括今天也發了好多時間也不能通過.最后找到
秋水小筑之Blog
http://blog.chinaunix.net/u/5302/showart_238337.html
的博客, 幫了大忙,網上的很多文章都試過了都沒有很好的結果.
我安裝的centos是單CD的服務版本.安裝后已經有bind了
1.配置文件在/etc/named.conf
//
// named.conf for Red Hat caching-nameserver
//
options {
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
/*
* If there is a firewall between you and nameservers you want
* to talk to, you might need to uncomment the query-source
* directive below. Previous versions of BIND always asked
* questions using port 53, but BIND 8.1 uses an unprivileged
* port by default.
*/
// query-source address * port 53;
};
//
// a caching only nameserver config
//
controls {
inet 127.0.0.1 allow { localhost; } keys { rndckey; };
};
zone "." IN {
type hint;
file "named.ca";
};
zone "localdomain" IN {
type master;
file "localdomain.zone";
allow-update { none; };
};
zone "localhost" IN {
type master;
file "localhost.zone";
allow-update { none; };
};
zone "chenlb.com" IN {
type master;
file "chenlb.com.zone";
allow-query { any; };
allow-transfer { any; };
allow-update { none; };
};
zone "0.0.127.in-addr.arpa" IN {
type master;
file "named.local";
allow-update { none; };
};
zone "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa" IN {
type master;
file "named.ip6.local";
allow-update { none; };
};
zone "255.in-addr.arpa" IN {
type master;
file "named.broadcast";
allow-update { none; };
};
zone "0.in-addr.arpa" IN {
type master;
file "named.zero";
allow-update { none; };
};
include "/etc/rndc.key";
只要添加一個zone就行,看上面
zone "chenlb.com" IN {
type master;
file "chenlb.com.zone";
allow-query { any; };
allow-transfer { any; };
allow-update { none; };
};
2.在/var/named/chroot/var/named/目錄里建個chenlb.com.zone(上面的file),內容如下:
$TTL 86400
@ IN SOA chenlb.com. root.chenlb.com.(
2008050201 ; Serial
28800 ; Refresh
14400 ; Retry
3600000 ; Expire
86400 ) ; Minimum
IN NS chenlb.com.
IN MX 10 mail.chenlb.com.
@ IN A 192.168.0.60
www IN A 192.168.0.60
ftp IN A 192.168.0.60
mail IN A 192.168.0.60
3.在/var/named目錄下建鏈接
# ch /var/named
# ln -s /var/named/chroot/var/named/chenlb.com.zone chenlb.com.zone
4.啟動named
# /etc/init.d/named start
5.測試前添加nds服務地址
# vi /etc/resolv.conf
在加
nameserver 192.168.0.60
search chenlb.com
說明:192.168.0.60是我本機地址
現在本機下可以ping www.chenlb.com了
要在加的機上可以使用DNS服務,要在防火墻里允許
6.修改/etc/sysconfig/iptables添加下面的
-A RH-Firewall-1-INPUT -p udp -m udp --dport 53 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 53 -j ACCEPT
OK,現在在win里添加dns地址192.168.0.60就在ping www.chenlb.com了. 呵呵
posted @
2008-05-02 20:46 流浪汗 閱讀(1016) |
評論 (0) |
編輯 收藏
昨天安裝好了lighttpd,現在想試下與tomcat的一起工作。把所有的*.jsp讓tomcat去處理。用到lighttpd的代理,lighttpd與tomcat比apache與tomcat要簡單多。開始喜歡上了lighttpd。
修改lighttpd的配置
# vi /usr/local/lighttpd-1.4.19/lighttpd.conf
去掉mod_proxy注釋
server.modules = (
#...
# "mod_proxy",
#...
)
去掉proxy.server注釋
proxy.server = ( ".jsp" =>
( "localhost" =>
(
"host" => "192.168.0.60",
"port" => 8080
)
)
)
重啟lighttpd,然后
http://192.168.0.60/index.jsp就可以看到tomcat的頁面了。
posted @
2008-05-02 00:03 流浪汗 閱讀(2286) |
評論 (0) |
編輯 收藏
近幾日都想玩下服務器。此文是在linux下的lighttpd安裝php。參考 的博客:
http://blog.csdn.net/shined_zhang/archive/2007/10/28/1852349.aspx
1.安裝lighttpd看
http://www.tkk7.com/chenlb/archive/2008/04/30/197617.html
2.安裝mysql看
http://www.tkk7.com/chenlb/archive/2007/03/20/105114.html
3.安裝php,先到
http://www.php.net 下載,我下載的是php-5.2.5
# tar -zxvf php-5.2.5.tar.gz
# cd php-5.2.5
#./configure --prefix=/usr/local/php-5.2.5 --enable-fastcgi --with-mysql=/usr/local/mysql --enable-zend-multibyte --with-config-file-path=/usr/local/php-5.2.5/conf --enable-discard-path --enable-force-cgi-redirect
# make
# make install
#mkdir /usr/local/php-5.2.5/conf
#cp php.ini-dist /usr/local/php-5.2.5/conf/php.ini
我在安裝過程中也出過問題,說我沒有找到xml2。要yum install libxml2*一下。
4.修改lighttpd的配置。 把mod_fastcgi去掉注釋。
# vi /usr/local/lighttpd-1.4.19/lighttpd.conf
server.modules = (
#
# "mod_fastcgi",
#
)
找到fastcgi.server去掉注釋,修改后看起來像。
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"socket" => "/usr/local/lighttpd-1.4.19/php-fastcgi.socket",
"bin-path" => "/usr/local/php-5.2.5/bin/php-cgi"
)
)
)
5.php測試頁面phpinfo.php放到你的www目錄下。
<?php
echo phpinfo();
?>
最后啟動或重啟lighttpd即可。
posted @
2008-05-01 16:48 流浪汗 閱讀(1179) |
評論 (0) |
編輯 收藏