Java 常用类:Integer类,String类

1. Integer类:

1.1. 构造方法:

Integer(int value)构造一个新分配的 Integer对象,该对象表示指定的 int值。  

Integer(String s):构造一个新分配 Integer对象,输入字符串

1.2 静态方法

Integer.MAX_VALUE:最大值:2147483647

Integer.MIN_VALUE:最小值:-2147483648

Integer.BYTES:字节数:4个字节

Integer.SIZE:位数:32

Integer.TYPE:返回int

static int bigCount(int i):返回补码形式表示该数时1的个数

static int compare(int x, int y):返回两个数比较的结果,x>y返回1,x=y返回0,x<y返回-1

static int compareUnsigned(int x, int y):将两个数视为无符号比较,此时如果x>0, y<0,返回-1,同号的结果与compare()相同

static Integer decode(String num):将String解码成Integer

static int hashCode(int value):返回值为int的哈希码,兼容Integer.hashCode()

static int parseInt(String s):将字符串参数解析成带符号的十进制整数

static int parseUnsignedInt(String s):将字符串参数解析成无符号的十进制整数

static intdivideUnsigned(int x, int y):返回无符号整数整除的商

static int remainderUnsigned(int x, int y):返回无符号整数整除的余数

static int max(int x, int y):返回两个数中的最大值

static int min(int x, int y):返回两个数中的最小值

static String toUnsignedString(int i):返回字符串表示的无符号十进制数值

static String toOctalString(int i):返回字符串表示的无符号八进制数值

static String toHexString(int i):返回字符串表示的无符号十六进制数值

static String toBinaryString(int i):返回字符串表示的无符号二进制数值

static long toUnsignedLong(int x):返回无符号long类型转换的数值

static int sum(int a, int b):根据加运算符将两个整数相加

static Integer valueOf(int i):int转Integer

static Integer valueOf(String s):String 转Integer

1.3 接口方法:

byte byteValue():返回值向byte的收缩变换

int compareTo(Integer anotherInteger):比较两个Integer对象

double doubleValue():返回此值像double的宽元变换

boolean equals(Object obj):将此值与指定对象比较

float floatValue():返回此值向float的宽元变换

int hashCode():返回hash码

int intValue():Integer向int转换

long longValue():返回此值向long的宽元变换

short shortValue():返回此值向shor的收缩变换

String toString():返回String类型

测试代码:

    public static void main(String[] args) {
        System.out.println(Integer.parseInt("900"));
        Integer i = new Integer("20");
        System.out.println(i);
        System.out.println(Integer.bitCount(255));
        System.out.println(Integer.compare(20,-100));
        System.out.println(Integer.compareUnsigned(-1,-8));
        System.out.println(Integer.decode("1366"));
        System.out.println(Integer.parseInt("-55"));
        System.out.println(Integer.divideUnsigned(30,7) + "-----" + Integer.remainderUnsigned(30,7));
        System.out.println(Integer.reverse(1));
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.BYTES);
        System.out.println(Integer.SIZE);
        System.out.println(Integer.TYPE);
        System.out.println(Integer.toUnsignedString(-1));
        System.out.println(Integer.toOctalString(-1));
        System.out.println(Integer.toUnsignedLong(-1));
        System.out.println(i.byteValue());
    }

2. String类

2.1 构造方法:

String():初始化新创建的String对象,使其代表空字符序列

String(byte[] bytes):通过使用平台的默认字符集解码指定的字节数组来构造新的String

String(byte[] bytes, Charset charset):通过byte[]数组构造一个新的String由指定的解码方式charset

String(byte[] bytes, int offset, int length):构造一个新的String通过使用平台默认的字符集编码来构造新的String,offset起始位置,length长度

String(byte[] bytes, int offset, int length, Charset charset):构造一个新的String通过使用指定的字符集编码来构造新的String,offset起始位置,length长度

String(byte[] bytes, String charsetName):构造一个指定字节数组解码charset

String(char[] value):使用字节数组构建一个新的字符串

String(char[] value, int offset, int count):使用字节数组由offset起始位置,count为长度的字符串

String(int[] codePoints, int offset, int count):使用int数组由offset起始位置,count为长度的字符串

String(String original):创建一个新的字符串副本对象

String(StringBuilder builder):分配一个字符串,其中包含字符串构造器中的字符序列。

2.2 静态方法

static String copyValueOf(char[] data):返回一个由char数组构成的字符串

static String copyValueOf(char[] data, int offset, int count):返回一个由char数组,offset起始位置,count长度的字符串

String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处。format()方法有两种重载形式。

static String format(String format, Object... args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。

static String format(Locale locale, String format, Object... args) 使用指定的语言环境,制定字符串格式和参数生成格式化的字符串。

String s5 = String.format("%s,%s", "wangjing", "xiaojingzi");
Date date=new Date();                                    
            //b的使用,月份简称
String str=String.format(Locale.US,"英文月份简称:%tb",date);//英文月份简称:Sep
System.out.printf("全部日期和时间信息:%tc%n",date);  // 全部日期和时间信息:星期一 九月 10 10:43:36 CST 2012

static String join(CharSequence delimiter, CharSequence... elements):返回一个字符串,字符串由delimiter连接的elements(CharSequence是char类型的一个可读序列,它本身是一个接口,CharBuffer、String、StringBuffer、StringBuilder这个四个类)

static String join(CharSequence delimiter, Iterable<? extends Charsequence> elements)返回一个字符串,字符串由delimiter连接的elements

        String delimiter = "-";
        String[] elements = {"JDK", "8", "String", "join"};
        List<String> list = new ArrayList<String>(Arrays.asList(elements));

        System.out.println(String.join(delimiter, elements));
        System.out.println(String.join(delimiter, "JDK", "8", "String", "join"));
        System.out.println(String.join(delimiter, list));
//三个输出结果:"JDK-8-String-join"

static String valueOf(boolean b):boolean转String

static String valueOf(char c):char 转String

static String valueOf(char[] data):数组转String,不会加上的数组的[]

static String valueOf(char[] data, int offset, int count):指定offset开始count元素个数的数组转String

static String valueOf(double d):double类型转String

static String valueOf(float f):float类型转String

static String valueOf(int i):int类型转String

static String valueOf(long l):long类型转String

static String valueOf(Object obj):调用底层的toString方法

2.3 接口方法:

char charAt(int index):返回指定索引处的值

int codePointAt(int index):返回索引之前的字符Unicode代码点(a是97)

int codePointCount(int beginIndex, int endIndex):返回String指定文本范围内Unicode代码点数,即统计字符个数

int codePointBefore(int index):返回index索引前一个字符的unicode代码点

int compareTo(String anotherString):按照字典的顺序比较两个字符串

int compareToIgnoreCase(String anotherString):按照字典的顺序比较两个字符串,不考虑大小写

String concat(String str):将指定的字符串连接到该字符串的末尾,需要用String接收,不会改变原来的值

boolean contains(CharSequence s):判断该字符串是否包含输入的序列

boolean contentsEquals(CharSequence cs):判断该字符串是否与输入相同

boolean contentsEquals(StringBuffer sb):判断该字符串是否与输入相同

boolean equals(object anObject):判断两个字符串是否相同

contensEquals与equals区别:equals必须输入是String,而contentsEquals输入时CharSequence的实现类即可

endsWith(string suffix):测试该字符串是否以指定的后缀结尾

equalsIgnoreCase(String anotherString):判断是否相同,忽略大小写

byte[] getBytes():使用平台默认的字符将此String编码为字符序列,结果存储到新的字节数组

byte[] getBytes(Charset charset):使用给定的charset将此String编码为字符序列,结果存储到新的字节数组

byte[] getBytes(String charsetName):使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin):将该字符串中的字符复制到目标字符数组中

int hashCode():返回该字符串的哈希码

int indexOf(int ch):返回指定字符首次出现的字符串的索引,可以输入ascii码字符,也可以输入数字

int lastIndexOf(String str):返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索

int lastIndexOf(int ch):返回指定的字符最后一次出现在字符串中的索引

int indexof(String str, int fromIndex):返回指定字符串第一次出现的字符串的索引,从指定的索引开始

String intern():返回字符串对象的规范化表示:详见https://blog.csdn.net/soonfly/article/details/70147205

boolean isEmpty():不为空返回true

int length():返回字符串的成都

boolean matches(String regex):用于检测字符串是否匹配给定的正则表达式。

int offsetByCodePoints(int index, int codePointOffset):返回此String 中从给定的 index 处偏移 codePointOffset 个代码点的索引,返回index + codePpointOffset

boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len):判断两个字符串区域是否相等

boolean regionMatches(int toffset, String other, int ooffset, int len):判断两个字符串区域是否相等

String replace(CharSequence target, CharSequence replacement):将于replacement匹配的每一个字节使用target替换

String replace(String regex, String replacement):将于replacement匹配的每一个字符串使用target替换

String replaceFirst(String regex, String replacement):将于replacement匹配的第一个字符串使用target替换

String[] split(String regex):按照regex规则划分":", ","等

String[] split(String regex, int limit):

limit 参数控制模式应用的次数,因此影响所得数组的长度。如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后一项将包含所有超出最后匹配的定界符的输入。如果 n 为非正,那么模式将被应用尽可能多的次数,而且数组可以是任何长度。如果 n 为 0,那么模式将被应用尽可能多的次数,数组可以是任何长度,并且结尾空字符串将被丢弃。

boolean startsWith(String prefix):测试该字符串是否以指定的前缀开头

boolean startsWith(String prefix, int toffset):测试从指定索引开始该字符串的子字符串是否以指定的前缀开头

String substring(int beginIndex):返回一个字符串,这个字符串是该字符串的子字符串

String substring(int beginIndex, int endIndex):返回一个字符串,这个字符串是该字符串的子字符串

char[] toCharArray():将该字符串转换成新的字符数组

String toLowerCase():将所有该字符串的字符转换成小写返回

String toLowerCase(Local locale):使用Locale本次规则小写

String toString():返回本身

String toUpperCase():将该字符串的所有字符转换成大写返回

String toUpperCase(Locale locale):使用Locale本次规则大写

String trim():返回一个字符串,其值为该字符串,并删除首位的所有空格以及换行

    public static void main(String[] args) throws UnsupportedEncodingException {
        String s1 = new String();
        byte[] by = {'w', 'j', 's', 'm'};
        char[] s = {'w', 'j'};
        String s2 = new String(by);
        String s3 = new String(by, "utf-8");
        String s4 = new String(by, 1, 2);
        System.out.println(s1 + "----" + s2 + "----" + s3 + "----" + s4);
        System.out.println(String.copyValueOf(s));
        System.out.println(String.copyValueOf(s, 0, 1));
        String s5 = String.format("%s,%s", "wangjing", "xiaojingzi");
        System.out.println(s5);
        System.out.println(String.valueOf(s));
        System.out.println(String.valueOf(true));
        String s6 = "wangwangwangwang";
        System.out.println(s5.charAt(5));
        System.out.println(s5.codePointAt(5));
        System.out.println(s5.codePointAt(6));
        System.out.println(s5.codePointBefore(7));
        System.out.println(s5.codePointCount(1, 6));
        s5 = s5.concat("xiaoha");
        System.out.println(s5);
        System.out.println(s5.contains("xiaoha"));
        StringBuilder sb = new StringBuilder("wangjing,xiaojingzixiaoha");
        System.out.println(s5.contentEquals(sb));
        System.out.println(s5.intern());
        System.out.println(s5.indexOf(97));
        System.out.println(s5.offsetByCodePoints(2, 3));
        s5 = s5.replace('a', 'm');
        System.out.println(s5);
        System.out.println("   wangjing    \n".trim());    // wangjing
    }

 

转载于:https://www.cnblogs.com/feng-ying/p/9425277.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值