java中api教程_Java中常用的API

1. 字符串与数字之间的相互转换

① 将数字转化为字符串:

② 将数字型的字符串转化为数字

int n = Integer.parseInt(String s)

double n = Double.parseDouble(s) public class Main { public static void main(String[] args) { String s = "123456"; int n1 = Integer.parseInt(s); System.out.println(n1); int n2 = 234812; s = Integer.toString(n2); System.out.println(s); double n3 = 23.45; s = Double.toString(n3); System.out.println(s); s = "45.87"; System.out.println(Double.parseDouble(s)); } }

2. String对象与char数组的转换

① 字符串对象转为char类型的数组

② 将char类型的数组转为字符串

String s = new String(char arr[]);

String s = String.valueOf(char arr[]); public class Main { public static void main(String[] args) { String s = "123345"; char arr[] = s.toCharArray(); for(int i = 0; i < arr.length; i++){ System.out.print(arr[i]); } System.out.print("\n"); char arr2[] = {'a', 'b', 'c', 'd', 'e'}; s = new String(arr2); System.out.println(s); char arr3[] = {'e', 'f', 'g', 'h', 'i'}; s = String.valueOf(arr3); System.out.println(s); } }

3. 根据索引获取字符和根据字符获取索引

char c = s.charAt(i);

② 获取字符的最后一个下标(可能有多个重复的字符):

int index = s.lastIndexOf(char c);

获取字符的第一个下标:

int index = s.indexOf(char c); public class Main { public static void main(String[] args) { String s = "abbc"; char c = s.charAt(3); System.out.println(c); System.out.println(s.indexOf('b')); System.out.println(s.lastIndexOf('b')); } }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值