【技巧总结】java整数,字符串,数组之间的相互转换

您所在的位置:网站首页 把整数转化为字符串 【技巧总结】java整数,字符串,数组之间的相互转换

【技巧总结】java整数,字符串,数组之间的相互转换

2024-07-13 15:44:02| 来源: 网络整理| 查看: 265

总结了JAVA里面整数,字符串,数组之间的常用变换方法,以及一些常见的用法,方便查阅复习,如有问题,欢迎各位大佬留言。

1.整数变为字符串 int number = 123456; Integer.toString(number); int number = 123456; String.valueOf(number); int number = 123456; String str = number+""; 2.字符串变为整数 String s="123456"; int i; i=Integer.parseInt(s); String s="123456"; int i; i=Integer.valueOf(s).intValue(); 3.整数变为数组 int number = 123456; char[] array = String.valueOf(number).toCharArray(); for (int i = 0; i System.out.print(str[i] + " "); } 4.数组变为整数 int sum = 0; int array[] = new int[]{1,2,3,4,5,6}; for(int j = 0; j array[i] = s.charAt(i); } for(int i = 0; i "123", "abc", "喜 之 郎"}; StringBuffer stringBuffer = new StringBuffer(); for(int i = 0; i 'a','b','c','喜','羊','羊','1','2','3'}; String s = new String(data); char[] array = {'a','b','c','喜','羊','羊','1','2','3'}; String str = String.valueOf(array); System.out.println(str); 7.字符串常用方法 返回指定索引处的字符 String s = "https://blog.csdn.net/weixin_52605156"; char result = s.charAt(6); System.out.println(result); 把这个字符串和另一个对象比较。 String a = "a"; String b = "b"; System.out.println(a.compareTo(b)); //输出 -1 String a = "abc"; String b = "bcde"; System.out.println(a.compareTo(b)); //两个字符串首字母不同时,返回首字母的 ASCII差值,如果首字母相同的时候,那么比较下一个字符,直到有不同的字母为止。 //输出 -1 String a = "abc"; String b = "abcd"; System.out.println(a.compareTo(b)); //上述情况的时候,则返回两个字符串的长度差值。 //输出 -1 按字典顺序比较两个字符串,不考虑大小写。 String str1="php"; String str2="java"; System.out.println(str1.compareToIgnoreCase(str2)); //返回第一个不同的字符相差的ASCII码值 //输出 6 String str1="java"; String str2="JAVAweb"; System.out.println(str1.compareToIgnoreCase(str2)); //返回相差的字符个数 //输出 -3 将指定字符串连接到此字符串的结尾。 String s = "hello "; s = s.concat("world!"); System.out.println(s); //输出 hello world! 返回指定数组中该字符序列 char[] Str1 = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; String Str2 = ""; Str2 = Str2.copyValueOf(Str1); System.out.println("输出:" + Str2); Str2 = Str2.copyValueOf( Str1, 1 , 2); System.out.println("输出:" + Str2); //输出:123456789 //输出:23 判断字符串是否以指定后缀结束 String string = new String("hello world!"); boolean a; a = string.endsWith("hello"); System.out.println("真假?:" + a); a = string.endsWith("world!"); System.out.println("真假?:" + a); //真假?:false //真假?:true 字符串与指定的对象比较 String string1 = new String("abc"); String string2 = string1;//string1和string2共用一片空间 String string3 = new String("abc");//string3单独又开辟了一片空间 boolean a; a = string1.equals(string2); System.out.println("真假?:" + a);//真假?:true a = string1.equals(string3); System.out.println("真假?:" + a);//真假?:true // == 比较引用地址是否相同,equals()比较字符串内容是否相同。 将字符从字符串复制到目标字符数组 public void getChars(int begin, int end, char[] array, int d) //begin: 字符串中要复制的第一个字符的索引。 //end: 字符串中要复制的最后一个字符之后的索引。 //array: 目标数组。 //d: 目标数组中的起始偏移量。 String string1 = new String("123abc你好世界! hello world!"); char[] array = new char[6]; string1.getChars(4, 10, array, 0); System.out.print("拷贝的字符串:"); System.out.println(array); //拷贝的字符串:bc你好世界 检测两个字符串在一个区域内是否相等 public boolean regionMatches(boolean a,int b, String c,int d,int length) /* a : 如果为 true,则比较字符时忽略大小写, 不写的话默认是false。 b : 字符串中的起始偏移量。 c : 字符串参数。 d : 字符串参数中子区域的起始偏移量。 length : 要进行比较的字符数目。 */ String string1 = new String("www.csdn.com"); String string2 = new String("CSDN"); String string3 = new String("csdn"); System.out.print("返回值 :" );//返回值 :false System.out.println(string1.regionMatches(4, string2, 0, 3)); System.out.print("返回值 :" );//返回值 :true System.out.println(string1.regionMatches(4, string3, 0, 3)); System.out.print("返回值 :" );//返回值 :true System.out.println(string1.regionMatches(true, 4, string2, 0, 3)); 检测字符串是否以指定的前缀开始 String string = new String("www.csdn.com"); System.out.print("返回值: " ); System.out.println(string.startsWith("www")); //返回值: true 将字符串转换为小写 String string = new String("ABCDEFG"); System.out.print("变为小写: "); System.out.println(string.toLowerCase()); //变为小写: abcdefg 将字符串小写字符转换为大写 String string = new String("abcdefg"); System.out.print("变为大写:" ); System.out.println(string.toUpperCase()); //变为大写:ABCDEFG 删除字符串的首尾空白符 String string = new String(" a b c d e f g "); System.out.print("初始字符串是: "); System.out.println(string); System.out.print("删除首尾空白: "); System.out.println(string.trim()); /* 初始字符串是: a b c d e f g 删除首尾空白: a b c d e f g */ 判断字符串中是否包含指定的字符或字符串 String string = "csdn"; System.out.println(string.contains("c")); //true System.out.println(string.contains("sd")); //true System.out.println(string.contains("a")); //false 判断字符串是否为空 String string = "csdn"; String string2 = ""; String string3 = " "; System.out.println("string是否为空:" + string.isEmpty());//string是否为空:false System.out.println("string2是否为空:" + string2.isEmpty());//string2是否为空:true System.out.println("string3是否为空:" + string3.isEmpty());//string3是否为空:false 8.数组常用方法

数组基础请移步:https://blog.csdn.net/weixin_52605156/article/details/120355549

遍历二维数组 int array[][] = {{4,3},{5,3}}; int i = 0; for(int a[]: array){ i++; int j=0; for(int b: a){ j++; if(i==array.length && j==a.length){ System.out.println(b); }else{ System.out.println(b + ","); } } } 替换元素 fill(int[] array,int value); 使用指定的int值分配给int型数组的每个元素 fill(int[] array,int fromIndex,int toIndex,int value); fromIndex 是要使用指定值填充的第一个元素的索引(被包括) toIndex 是使用指定值填充的最后一个元素的索引(不包括) value 是储存在数组所有元素中的值 //我们应当注意不要让索引位置越界,否则会出现数组越界异常 数组排序 Arrays.sort(object);//object也就是数组的名称 //JAVA里面对于String类型数组的排序,遵循的原则是数字排在字母前面,大写字母排在小写字母前面。 数组拷贝 copyOf(array,int newlength); array 是要进行复制的数组 newlength 是复制后的新数组的长度,如果比原来大,那么空余的地方用0填充,如果小,那么就截取到满足条件为止。 copyOfRange(array,int fromIndex,int toIndex) array 是要进行复制的数组对象 fromIndex 开始复制数组的索引位置,需要包括 toIndex 是指要复制范围的最后索引位置,但是是不包括Index的元素 元素查询 binarySearch(Object[ ] array,Object key) //array 是要进行搜索的数组 //key 是要进行搜索的值,如果这个key包含在数组里面,则返回搜索值得索引,否则返回 -1 或 " - " binarySearch(Object[ ] array,int fromIndex, int toIndex ,Object key) array要检索的数组 fromIndex是指定范围的开始处索引 toIndex 是指范围内的结束处索引 key 是指要搜索的元素 //使用此方法依然要进行数组的排序 数组创建ArrayList String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList arrayList = new ArrayList(Arrays.asList(stringArray)); System.out.println(arrayList); // [a, b, c, d, e] 判断数组是否包含某个值 String[] stringArray = { "a", "b", "c", "d", "e" }; boolean b = Arrays.asList(stringArray).contains("a"); System.out.println(b); // true 连接两个数组 int[] intArray = { 1, 2, 3, 4, 5 }; int[] intArray2 = { 6, 7, 8, 9, 10 }; int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2); String a[] = { "a", "b", "c" }; String b[] = { "d", "e", "f" }; List list = new ArrayList(Arrays.asList(a)); list.addAll(Arrays.asList(b)); Object[] c = list.toArray(); System.out.println(Arrays.toString(c)); 根据分隔符拼接数组元素(去掉最后一个分隔符) String j = StringUtils.join(new String[] { "a", "b", "c" }, ", "); System.out.println(j); // a, b, c ArrayList转数组 String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList arrayList = new ArrayList(Arrays.asList(stringArray)); String[] stringArr = new String[arrayList.size()]; arrayList.toArray(stringArr); for (String s : stringArr) System.out.println(s); 反转数组 int[] intArray = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(intArray); System.out.println(Arrays.toString(intArray)); //[5, 4, 3, 2, 1] 删除数组元素 int[] intArray = { 1, 2, 3, 4, 5 }; int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array System.out.println(Arrays.toString(removed)); 获取数组长度 int [ ] [ ] array = { {1 ,2,3},{4,5,6},{7,8,9}}; int rows = array.length; int columns = array[0].length; 数组获取最大和最小值 Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5}; int min = (int) Collections.min(Arrays.asList(numbers)); int max = (int) Collections.max(Arrays.asList(numbers)); System.out.println("最小值: " + min); System.out.println("最大值: " + max); 判断数组是否相等 int[] array = {1,2,3}; int[] array1 = {1,2,3,4}; int[] array2 = {1,2,3}; System.out.println("array与array1是否相等? "+Arrays.equals(array, array1)); System.out.println("array与array2是否相等? "+Arrays.equals(array, array2));

参考资料:https://www.apiref.com/java11-zh/java.base/java/util/package-summary.html https://www.runoob.com/java/java-tutorial.html



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭