String_Integer总结

说实话,就本人对String和Integer也写不出什么深刻的文章,就对其的总结也没什么底, 本人现阶段正在为此而奋发,现在就老师讲的有关他们的几个重要的方法做点小小结:(老师讲的应给比较重要吧!!!)
在平常写代码的时候总会碰到这样或那样的问题,这其中就有很多时有关String和Integer等数据类型的应用问题下边线分析一下String的用法:
1)String 类代表字符串。String 类包括的方法可用于检查序列的单个字符、比较字符串、搜索字符串、提取子字符串、创建字符串副本并将所有字符全部转换为大写或小写。
a)现有如下代码:
public class StringTest {

public static void main(String[] args) {

String s = "http://test.iteye.com/admin";

int index = s.lastIndexOf('/');
String str = s.substring(0, index);

str = str.replace("javaeye", "javake");

System.out.println(str);
}
}
其运行结果为:http://test.javake.com
解析以上代码中String的用法
<1> String s = "http://test.iteye.com/admin"; 语句代表了String的定义,用"……"表示;
<2> int index = s.lastIndexOf('/'); 语句中用到了lastIndexOf(String str) (返回指定子字符串在此字符串中最右边出现处的索引。)方法, 其返回值为int;在这句代码中表示“找到 "http://test.iteye.com/admin"中最后个'/'的具体位置”;
<3>String str = s.substring(0, index);中用到substring(int beginIndex, int endIndex) (返回一个新字符串,它是此字符串的一个子字符串)方法,其返回值为Sting;在本代码中意为把上句代码中找到的最后一个'/'后字符串的移除;其执行结果为:http://test.iteye.com
<4> str = str.replace("javaeye", "javake");此中用到方法 replace(char oldChar, char newChar) (返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的),其返回值为String;这句中为用"javake"替代"javaeye"。最后的执行结果为:http://test.javake.com
b)代码:
public class StringTest {

public static void main(String[] args) {
String s = "abhjfgdabkjhfdsbbbaab";
String s1 = "ab";
int t = s.compareTo(s1);

System.out.println(t);
运行结果为:19
代码解析:
代码中用到 compareTo(String anotherString)( 按字典顺序比较两个字符串)方法,返回一个int;运算结果为 '+'; 则s1>s;反之,则小;说明:数字<字母<汉字;
c)代码
String s = "aa|bb;cc|dd;ee|ff;gg|hh";

String[] arr = s.split(";");

for(int i=0;i<arr.length;i++){
String ss = arr[i].replace('|', '>');
String[] strs = ss.split(">");
for(int j=0;j<strs.length;j++){
System.out.println(strs[j] +"\t");
}
System.out.println();
}
运行结果:
aa bb
cc dd
ee ff
gg hh
解析:
<1>String[] arr = s.split(";"); 代码中用到方法split(String regex) ( 根据给定正则表达式的匹配拆分此字符串)返回一个String[];此句中表达为把';'的地方拆分,得结果:
aa|bb
cc|dd
ee|ff
gg|hh
<2>然后把'|'也拆分,注意 '|'不能直接拆分,要把其先用其他符号替代再拆分。
有关String的用法就分析这些吧!!


2)Integer 类在对象中包装了一个基本类型 int 的值。Integer 类型的对象包含一个 int 类型的字段。
这儿就我自己认为比较重要恰巧我会的几个用法分析一下:
public class int_text {
public static void main(String[] args){
String s = "123";
int t = Integer.parseInt(s);
int t1 = Integer.parseInt(s, 16);
int t2 = Integer.bitCount(54);
System.out.println(t+"\t"+t1+"\t"+t2);
}
}
执行结果为:123 291 4
<1>parseInt(String s)将字符串参数作为有符号的十进制整数进行解析。parseInt是一个静态量,直接用方法名调用;
<2>parseInt(String s, int radix)使用第二个参数指定的基数,将字符串参数解析为有符号的整数。 这里的int radix表示要转换成的进制数,注意:方法中的's'为无符号数;
<3>bitCount(int i)返回指定 int 值的二进制补码表示形式的 1 位的数量;先把十进制int转换成二进制,然后统计二进制中‘1’的个数。
其他的一些方法:
1.Integer(String s)构造一个新分配的 Integer 对象,它表示 String 参数所指示的 int 值。在这里的s要写成数字!!!
2.rotateLeft(int i, int distance)返回根据指定的位数循环左移指定的 int 值的二进制补码表示形式而得到的值。 其意为正在左边移出的数接着再右边重新补上。
引用:
java中String类常用的方法总结...  Java中String类的常用方法

  public char charAt(int index)

  返回字符串中第index个字符;

  public int length()

  返回字符串的长度;

  public int indexOf(String str)

  返回字符串中第一次出现str的位置;

  public int indexOf(String str,int fromIndex)

  返回字符串从fromIndex开始第一次出现str的位置;

  public boolean equalsIgnoreCase(String another)

  比较字符串与another是否一样(忽略大小写);

  public String replace(char oldchar,char newChar)

  在字符串中用newChar字符替换oldChar字符

  public boolean startsWith(String prefix)

  判断字符串是否以prefix字符串开头;

  public boolean endsWith(String suffix)

  判断一个字符串是否以suffix字符串结尾;

  public String toUpperCase()

  返回一个字符串为该字符串的大写形式;

  public String toLowerCase()

  返回一个字符串为该字符串的小写形式

  public String substring(int beginIndex)

  返回该字符串从beginIndex开始到结尾的子字符串;

  public String substring(int beginIndex,int endIndex)

  返回该字符串从beginIndex开始到endsIndex结尾的子字符串

  public String trim()

  返回该字符串去掉开头和结尾空格后的字符串

  public String[] split(String regex)

  将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值