Cappuccino~~~
BlogJava
首頁
新文章
新隨筆
聚合
管理
posts - 3, comments - 7, trackbacks - 0
TestString
public
class
TestString
{
public
static
void
main(String[] args)
{
//
求該字符串包含多少個java子字符串
String s1
=
"
java,java_sunjavaworld,i love java,ha,6iy%$javaw87%^sn2^*java
"
;
int
num
=
gen(s1,
"
java
"
);
System.out.println(
"
s1字符串一共包含
"
+
num
+
"
個子串
"
);
//
求該字符串中包含多少個大寫字符、小寫字符、數字、其他字符
String s2
=
"
hdHoinlG&dd3NZW3OIN2,dsou5WE.!72Hsn__@#m
"
;
fun1(s2);
//
查找方法1
fun2(s2);
//
查找方法2
fun3(s2);
//
查找方法3
}
public
static
int
gen(String str,String key)
{
String s
=
str;
int
count
=
0
;
int
position
=
0
;
while
( (position
=
s.indexOf(key))
!=
-
1
)
{
count
++
;
s
=
s.substring(position
+
key.length());
}
return
count;
}
public
static
void
fun1(String s)
{
int
uCase
=
0
;
int
lCase
=
0
;
int
nCase
=
0
;
int
oCase
=
0
;
for
(
int
i
=
0
;i
<
s.length();i
++
)
{
char
c
=
s.charAt(i);
if
(c
>=
'
A
'
&&
c
<=
'
Z
'
)
{
uCase
++
;
}
else
if
(c
>=
'
a
'
&&
c
<=
'
z
'
)
{
lCase
++
;
}
else
if
(c
>=
'
0
'
&&
c
<=
'
9
'
)
{
nCase
++
;
}
else
{
oCase
++
;
}
}
System.out.println(
"
fun1.大寫字符:
"
+
uCase
+
"
小寫字符:
"
+
lCase
+
"
數字
"
+
nCase
+
"
其他字符:
"
+
oCase
+
"
總計:
"
+
s.length());
}
public
static
void
fun2(String s)
{
int
uCase
=
0
;
int
lCase
=
0
;
int
nCase
=
0
;
int
oCase
=
0
;
String uS
=
"
ABCDEFGHIJKLMNOPQRSTUVWXYZ
"
;
String lS
=
"
abcdefghijklmnopqrstuvwxyz
"
;
String nS
=
"
0123456789
"
;
for
(
int
i
=
0
;i
<
s.length();i
++
)
{
char
c
=
s.charAt(i);
if
(uS.indexOf(c)
!=
-
1
)
{
uCase
++
;
}
else
if
(lS.indexOf(c)
!=
-
1
)
{
lCase
++
;
}
else
if
(nS.indexOf(c)
!=
-
1
)
{
nCase
++
;
}
else
{
oCase
++
;
}
}
System.out.println(
"
fun2.大寫字符:
"
+
uCase
+
"
小寫字符:
"
+
lCase
+
"
數字
"
+
nCase
+
"
其他字符:
"
+
oCase
+
"
總計:
"
+
s.length());
}
public
static
void
fun3(String s)
{
int
uCase
=
0
;
int
lCase
=
0
;
int
nCase
=
0
;
int
oCase
=
0
;
for
(
int
i
=
0
;i
<
s.length();i
++
)
{
char
c
=
s.charAt(i);
if
(Character.isUpperCase(c))
{
uCase
++
;
}
else
if
(Character.isLowerCase(c))
{
lCase
++
;
}
else
if
(Character.isDigit(c))
{
nCase
++
;
}
else
{
oCase
++
;
}
}
System.out.println(
"
fun3.大寫字符:
"
+
uCase
+
"
小寫字符:
"
+
lCase
+
"
數字
"
+
nCase
+
"
其他字符:
"
+
oCase
+
"
總計:
"
+
s.length());
}
}
//
s1字符串一共包含6個子串
//
fun1.大寫字符:11 小寫字符:15 數字6 其他字符:8 總計:40
//
fun2.大寫字符:11 小寫字符:15 數字6 其他字符:8 總計:40
//
fun3.大寫字符:11 小寫字符:15 數字6 其他字符:8 總計:40
posted on 2008-02-12 21:41
菜園小鳥
閱讀(224)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
<
2008年2月
>
日
一
二
三
四
五
六
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1
2
3
4
5
6
7
8
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(1)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2008年2月 (1)
2008年1月 (2)
搜索
最新評論
1.?re: 代理模式與動態代理模式
簡單易懂
--陶然楠軒
2.?re: 一題有關String筆試題的分析
留個記號。
--sitinspring
3.?re: 一題有關String筆試題的分析
討論這沒多大意義,很討厭死摳這些的hr
--你是樓豬
4.?re: 一題有關String筆試題的分析[未登錄]
這些原理是屬于編程基礎吧?
--abc
5.?re: 一題有關String筆試題的分析
評論內容較長,點擊標題查看
--隔葉黃鶯
閱讀排行榜
1.?代理模式與動態代理模式(8019)
2.?一題有關String筆試題的分析(1950)
3.?TestString(224)
評論排行榜
1.?一題有關String筆試題的分析(6)
2.?代理模式與動態代理模式(1)
3.?TestString(0)
Copyright ©2025 菜園小鳥 Powered By
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
深圳市
|
修文县
|
佛学
|
枞阳县
|
五家渠市
|
射洪县
|
青铜峡市
|
南城县
|
南漳县
|
曲周县
|
成武县
|
蒙山县
|
丰顺县
|
镇原县
|
奉节县
|
开远市
|
泽库县
|
沅江市
|
卢氏县
|
四子王旗
|
花莲县
|
新津县
|
白山市
|
察隅县
|
青阳县
|
紫云
|
蒙城县
|
美姑县
|
锦州市
|
喜德县
|
石河子市
|
当阳市
|
江安县
|
湟源县
|
葵青区
|
凉山
|
平山县
|
新乐市
|
科技
|
呼和浩特市
|
巩义市
|