①Strings are all objects
②Once initialized, they cannot be changed because they are constants.
③You can know through the constructor of the String class to convert a byte array or character into a string.
Function description | |
Create a string with empty content | |
Create an object based on the specified character array | |
Create an object based on the specified string content |
int lastIndexOf(int ch) | |
boolean endsWith(String suffix) | ##int length() |
boolean isEmpty() | |
boolean contains(CharSequence cs) | |
String toLowerCase() | |
char[] toCharArray() | |
String[] split(String regex) | |
String substring(int beginIndex, int endIndex) | |
1 public class Example02 { 2 public static void main(String[] args) { 3 new Thread(new One()).start(); 4 new Thread(new Two()).start(); 5 new Thread(new Three()).start(); 6 new Thread(new Four()).start(); 7 new Thread(new Five()).start(); 8 } 9 } 10 //字符串的基本操作 11 class One implements Runnable{ 12 public void run() { 13 String s = "abcdedcba"; 14 System.out.println("字符串的长度为:"+s.length()); 15 System.out.println("字符串中第一个字符:"+s.charAt(0)); 16 System.out.println("字符c中第一个出现的位置:"+s.indexOf('c')); 17 System.out.println("字符c中最后出现的位置:"+s.lastIndexOf('c')); 18 System.out.println("_____________"); 19 } 20 } 21 //字符串的判断操作 22 class Two implements Runnable { 23 public void run() { 24 String s1 = "abcdedcba"; 25 String s2 = "ABCDEDCBA"; 26 System.out.println("字符串结尾是否是A:" + s1.endsWith("A")); 27 System.out.println("字符串s1与s2是否相同:" + s1.equals("ABCDEDCBA")); 28 System.out.println("字符串s1是否为空:" + s1.isEmpty()); 29 System.out.println("字符串s1是否以abc开头:" + s1.startsWith("abc")); 30 System.out.println("字符串s1是否包含abc:" + s1.contains("abc")); 31 System.out.println("将字符串s1转为大写:" + s1.toUpperCase()); 32 System.out.println("将字符串s2转为小写:" + s1.toLowerCase()); 33 System.out.println("_____________"); 34 } 35 } 36 //字符串的转换操作 37 class Three implements Runnable{ 38 public void run() { 39 String s3 = "abcd"; 40 System.out.print("将字符串转为字符数组后的结果"); 41 char[] charArray=s3.toCharArray(); 42 for (int i = 0;i<chararray.length><p></p>Running result:<div class="cnblogs_code"></div> <pre class="brush:php;toolbar:false">字符串的长度为:9字符串中第一个字符:a 字符c中第一个出现的位置:2字符c中最后出现的位置:6_____________ 字符串结尾是否是A:false字符串s1与s2是否相同:false字符串s1是否为空:false字符串s1是否以abc开头:true字符串s1是否包含abc:true将字符串s1转为大写:ABCDEDCBA 将字符串s2转为小写:abcdedcba _____________ 将字符串转为字符数组后的结果a,b,c,d 将int值转换为String类型之后的结果:123_____________ 从第五个字符截取到末尾的结果:篮球-乒乓球 从第五个字符截取到第六个字符的结果:篮球 分割后的字符串数组中的元素依次为:将it替换为cn.it的结果:cn.itcase去除字符串两端空格后的结果:i t c a s e 去除字符串所有空格后的结果:itcase _____________ 羽毛球,篮球,乒乓球 _____________4. StringBuffer: String buffer, similar to a character container, converts any data into a string and adds it.
5. Commonly used methods of StringBuffer class:
StringBuffer deleteCharAt(int index) | |
StringBuffer replace(int start ,int end, String str) | |
String toString() | |
Example: |
1、添加—————————————————— append添加结果:abcdefg insert添加结果:ab123cdefg 2、删除—————————————————— 删除指定范围结果:afg 删除指定位置结果:af 清空缓冲区结果:
6. The difference between StringBuffer and String
②The String class covers the equals() method of the Object class, but the StringBuffer class does not.
③String class objects can be connected using operator +, but StringBuffer class objects cannot.
2. Common methods of System classstatic long currentTimeMills() | |
static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) | ##static Properties getProperties()static String getProperty( String key) |
The Runtime class is used to represent the status of the virtual machine when it is running. It is used to encapsulate the JVM virtual machine process. Instance method: Runtime run = Runtime.getRuntime(); |
1 public class Testt01 { 2 public static void main(String[] args) { 3 System.out.println("取绝对值:"+Math.abs(-2)); 4 System.out.println("向上取整:"+Math.ceil(2.1)); 5 System.out.println("四舍五入:"+Math.round(2.1)); 6 System.out.println("向下取整:"+Math.floor(2.1)); 7 System.out.println("取最大值:"+Math.max(3.2,2.1)); 8 System.out.println("取最小值:"+Math.min(3.21,2.1)); 9 System.out.println("反余弦:"+Math.acos(Math.PI/90)); 10 System.out.println("反正弦:"+Math.asin(Math.PI/45)); 11 System.out.println("反正切:"+Math.atan(12)); 12 System.out.println(Math.atan2(12,12)); 13 System.out.println("余弦:"+Math.cos(Math.PI/3)); 14 System.out.println("正弦:"+Math.sin(30*Math.PI/180));//单位为弧度,30度用弧度表示(30*Math.PI/180); 15 System.out.println("自然对数的底数e:"+Math.E); 16 System.out.println("自然对数的底数e的n次方:"+Math.exp(12)); 17 System.out.println("圆周率:"+Math.PI); 18 System.out.println("平方根:"+Math.sqrt(5)); 19 System.out.println("生成一个大于等于0.0小于1.0的随机值:"+Math.random()); 20 System.out.println("2的3次方:"+Math.pow( 2,3 )); 21 System.out.println( System.currentTimeMillis() );//获得当前系统时间,运行程序最省内存 22 System.out.println( new Date().getTime() ); 23 System.out.println( Calendar.getInstance().getTime() ); 24 25 } 26 }Running result:
1 取绝对值:2 2 向上取整:3.0 3 四舍五入:2 4 向下取整:2.0 5 取最大值:3.2 6 取最小值:2.1 7 反余弦:1.5358826490960904 8 反正弦:0.06987000497506388 9 反正切:1.4876550949064553 10 0.7853981633974483 11 余弦:0.5000000000000001 12 正弦:0.49999999999999994 13 自然对数的底数e:2.718281828459045 14 自然对数的底数e的n次方:162754.79141900392 15 圆周率:3.141592653589793 16 平方根:2.23606797749979 17 生成一个大于等于0.0小于1.0的随机值:0.3660454717733955 18 2的3次方:8.0 19 1498976435080 20 1498976435085 21 Sun Jul 02 14:20:35 CST 2017
int nextInt() | int nextInt(int n) | ||||||
示例: 1 public class Example16 { 2 public static void main(String[] args) { 3 new Thread(new One()).start(); 4 new Thread(new Two()).start(); 5 new Thread(new Three()).start(); 6 } 7 } 8 class One implements Runnable{ 9 public void run() { 10 Random r = new Random();//不传入种子 11 // 随机产生10个[0,100)之间的整数 12 for (int x=0;x 运行结果: 1 90 2 98 3 23 4 17 5 97 6 66 7 10 8 42 9 10 10 67 11 _____________ 12 92 13 0 14 75 15 98 16 63 17 10 18 93 19 13 20 56 21 14 22 _____________ 23 产生float类型随机数0.9580269 24 产生0~100之间int类型随机数44 25 产生double类型随机数0.13987266903473206 26 _____________ 四、针对日期类型的操作类有三个,分别是java.util.Date,java.util.Calendar和 java.text.DateFormat 。
1、Date类用于表示日期和时间:其中构造方法:
①Date()用于创建当前日期时间的Date对象,
②Date(long date)用于创建指定时间的Date对象。创建如:
Date date1 = new Date();
Date date2 = new Date(9666546565L );
2、Calendar类用于完成日期和时间字段的操作,为抽象类,不能被实例化,在程序中需要调用其静态方法getInstance()来得到一个Calendar对象,然后调用其相应的方法:
Calendar calendar = Calendar.getInstance();
Calendar常用方法:
注意:Calendar.MONTH字段月份的起始值是从0开始而不是1 。
示例:
1 public class Example { 2 public static void main(String[] args) { 3 new Thread(new One()).start(); 4 new Thread(new Two()).start(); 5 } 6 } 7 class One implements Runnable { 8 public void run() { 9 Date date = new Date(); 10 Date date2 = new Date(966666666666L); 11 System.out.println(date); 12 System.out.println(date2); 13 System.out.println("--------------"); 14 } 15 } 16 class Two implements Runnable { 17 public void run() { 18 Calendar calendar = Calendar.getInstance();//获取表示当前时间的Calendar 19 int year = calendar.get(Calendar.YEAR); 20 int month = calendar.get(Calendar.MONTH)+1; 21 int date = calendar.get(Calendar.DATE); 22 int hour = calendar.get(Calendar.HOUR); 23 int minute = calendar.get(Calendar.MINUTE); 24 int second = calendar.get(Calendar.SECOND); 25 System.out.println("当前时间为:"+year+"年"+month+"月"+date+"日"+hour+"时"+minute+"分"+second+"秒"); 26 System.out.println("--------------"); 27 } 28 } 运行结果: Sun Jul 02 14:29:41 CST 2017Sat Aug 19 14:31:06 CST 2000 -------------当前时间为:2017年7月2日2时29分41秒 --------------
3、DateFormat类,专门用于将日期格式化为字符串或者用特定格式显示的日期字符串转换成一个Date对象,抽象类不可实例化,但提供静态方法。
SimpleDateFormat ,DateFormat类子类,可通过new创建实例对象, 是一个以语言环境敏感的方式来格式化和分析日期的类。SimpleDateFormat 允许你选择任何用户 自定义日期时间格式来运行。如:
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
这一行代码确立了转换的格式,其中 yyyy 是完整的公元年,MM 是月份,dd 是日期,HH:mm:ss 是时、分、秒。
注意:有的格式大写,有的格式小写,例如 MM 是月份,mm 是分;HH 是 24 小时制,而 hh 是 12 小时制。
以上实例编译运行结果如下:
Current Date: Sun 2014.07.18 at 14:14:09 PM PDT
其中parse(String source)方法,它试图按照给定的SimpleDateFormat 对象的格式化存储来解析字符串。
示例:
1 public class Example { 2 public static void main(String[] args) throws Exception{ 3 // 创建一个SimpleDateFormat 对象 4 SimpleDateFormat df1 = new SimpleDateFormat( 5 "Gyyyy年MM月dd日:今天是yyyy年的第D天,E"); 6 // SimpleDateFormat 对象的日期模板格式化Date对象 7 System.out.println(df1.format(new Date())); 8 9 SimpleDateFormat df2 = new SimpleDateFormat("yyyy年MM月dd日"); 10 String dt = "2012年8月3日"; 11 // 将字符串解析成Date对象 12 System.out.println(df2.parse(dt)); 13 } 14 } 运行结果: AD2017年07月02日:今天是2017年的第183天,Sun Fri Aug 03 00:00:00 CST 2012
|
The above is the detailed content of Detailed introduction to API in Java. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor