Java学习笔记20(String类应用、StringBuffer类、StringBuilder类)
1.获取指定字符串中大小写和数字的个数:
package demo;
public class StringTest {
public static void main(String[] args) {
getCount("IamHandsome666");
}
public static void getCount(String str) {
int upper = 0;
int lower = 0;
int digit = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 65 && c <= 90) {
upper++;
} else if (c >= 97 && c <= 122) {
lower++;
} else if (c >= 48 && c <= 57) {
digit++;
}
}
System.out.println(upper);//
System.out.println(lower);//
System.out.println(digit);//
}
}
2.将一个字符串中第一个字母转成大写,其余字母转成小写并打印
package demo;
public class StringTest {
public static void main(String[] args) {
String string = toConvert("iAMhaNdSoMe");
System.out.println(string);
//输出:Iamhandsome
}
public static String toConvert(String str) {
String first = str.substring(0,1);
String after = str.substring(1);
first = first.toUpperCase();
after = after.toLowerCase();
return first+after;
}
}
3.从一个长字符串中找小字符串出现的次数:
package demo;
public class StringTest {
public static void main(String[] args) {
System.out.println(getStringCount("Ilikejava,andjavaisthebest,java", "java"));
}
public static int getStringCount(String str, String key) {
int count = 0;
int index = 0;
while ((index = str.indexOf(key)) != -1) {
count++;
str = str.substring(index+key.length());
}
return count;
}
}
//输出:3
String字符串无法改变,会有一些不便之处
所以介绍一个新类
StringBuffer类,字符串缓冲区
出现目的:为了提高字符串操作效率
内部采用了可变数组的方法,类内部定义了数组,这个数组没有final
数组的默认容量是16
关于它的方法,这里用一个示例来理解:
package demo;
public class StringBufferDemo {
public static void main(String[] args) {
append();
delete();
insert();
replace();
reverse();
toString_();
}
public static void append(){
StringBuffer buffer = new StringBuffer();
buffer.append(6);
buffer.append("hello");
System.out.println(buffer);
//6hello
}
public static void delete(){
StringBuffer buffer = new StringBuffer();
buffer.append("helloIlikeJava");
buffer.delete(1, 2);
buffer.deleteCharAt(8);
System.out.println(buffer);
//hlloIlikJava
}
public static void insert(){
StringBuffer buffer = new StringBuffer();
buffer.append("java");
buffer.insert(1, "Python");
System.out.println(buffer);
//jPythonava
}
public static void replace(){
StringBuffer buffer = new StringBuffer();
buffer.append("abcdefg");
buffer.replace(2, 4, "H");
System.out.println(buffer);
//abHefg
}
public static void reverse(){
StringBuffer buffer = new StringBuffer();
buffer.append("abcdefg");
buffer.reverse();
System.out.println(buffer);
//gfedcba
}
public static void toString_(){
StringBuffer buffer = new StringBuffer();
buffer.append("abcdefg");
String string = buffer.toString();
System.out.println(string);
//输出一个String类型的abcdefg,即字符串
}
}
StringBuffer类实例:
public class StringBufferTest {
public static void main(String[] args) {
int[] arr = {4,1,4,56,7,8,76};
System.out.println(toString(arr));
}
/*
* 目的:
* int[] arr = {34,12,89,68};将一个int[]中元素转成字符串
* 格式 [34,12,89,68]
*/
public static String toString(int[] arr){
//创建字符串缓冲区
StringBuffer buffer = new StringBuffer();
buffer.append("[");
//数组遍历
for(int i = 0 ; i < arr.length;i++){
//判断是不是数组的最后一个元素
if(i == arr.length-1){
buffer.append(arr[i]).append("]");
}else{
buffer.append(arr[i]).append(",");
}
}
return buffer.toString();
}
}
还有一个StringBuilder类,方法和StringBuffer的方法完全相同
区别:
StringBuffer类是一个线程安全的类,StringBuilder类是一个线程不安全的类,不过它更快
线程知识在后边会讲到,
日常开发建议使用StringBuilder类,因为相对速度更快
Java学习笔记20(String类应用、StringBuffer类、StringBuilder类)的更多相关文章
- 0025 Java学习笔记-面向对象-final修饰符、不可变类
final关键字可以用于何处 修饰类:该类不可被继承 修饰变量:该变量一经初始化就不能被重新赋值,即使该值跟初始化的值相同或者指向同一个对象,也不可以 类变量: 实例变量: 形参: 注意可以修饰形参 ...
- 3.2常用类(java学习笔记)String与StringBuffer
一.String String又称不可变字符序列. 我们看JDK源码中用于字符存储的数组有final修饰,final修饰变量就代表变量不能改变. 我们可以看API文档中对String的描述. Stri ...
- 【学习笔记】String进阶:StringBuffer类(线程安全)和StringBuilder类
一.除了使用String类存储字符串之外,还可以使用StringBuffer类存储字符串.而且它是比String类更高效的存储字符串的一种引用数据类型. 优点: 对字符串进行连接操作时,使用Strin ...
- Java学习笔记 02 String类、StringBuilder类、字符串格式化和正则表达式
一.String类一般字符串 声明字符串 >>String str 创建字符串 >>String(char a[])方法用于将一个字符数组创建为String对象 >> ...
- Java学习笔记18---final关键字修饰变量、方法及类
英语里final这个单词大家都知道是"最终的"意思,其实还有一个意思是"不可更改的".在Java里,final关键字作"不可更改的"来解释更 ...
- java学习笔记18(基本类型包装类,system类)
基本类型包装类 定义:程序界面用户输入的数据都是以字符串类型存储的,如果需要操作这些字符串进行运算,需要转成基本数据类型,这时就要用到基本类型包装类,例: public class Demo { pu ...
- Thinking in java学习笔记之String的不可变性
为了提高效率,可以使用StringBuffer或StringBuilder 1. 在执行速度方面的比较:StringBuilder > StringBuffer 2. StringBuffer与 ...
- java学习笔记之String类
String类总结 String类概述: java.lang.String 类是字符串操作类 String类的常用构造方法: //1.直接赋值 String str= "hellojava& ...
- java学习笔记5——String类常用方法
1.字符串长度计算: int i = String1.length(); 2.字符串比较:1) equals()和equalsIgnoreCase //比较两个字符串对象的实体是否相同,相同输出tru ...
随机推荐
- 》》stroll--各种特效下拉菜单
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- idea/eclipse下Maven工程集成web服务(tomcat、jetty)
idea/eclipse下Maven工程集成web服务 转载请注明出处:http://www.cnblogs.com/funnyzpc/p/8093554.html 应用服务器最常用的一般有这哥仨: ...
- POJ 1661 Help Jimmy(DP,注意边界)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9399 Accepted: 3025 Descri ...
- Maven构建真正的J2EE项目
今天同事问起我眼下用Maven构建的多模块项目架构和曾经用Eclipse创建的Web项目的问题.以下将讲一下使用maven搭建多模块的J2ee项目,以及採用这样的方式搭建项目对日后项目的水平拆分和垂直 ...
- 如何在华为云软件开发云上搭建JavaWeb,Maven项目
本文将使用华为云软件开发云向大家演示如何搭建JavaWeb,Maven项目. 一.相关信息 1.华为云软件开发云简介 华为云软件开发云(DevCloud)是集华为近30年研发实践,前沿研发理念,先进研 ...
- redis主从配置+哨兵模式
1.搭建redis主从,一个master两个slave,加一个哨兵监听(sentinel),可以新建三个虚拟机,模拟环境,我的电脑没那么多虚拟机,就在一台虚拟机上弄的. 2.安装redis,如果是三台 ...
- 【java】控制台实现贪吃蛇小游戏-LinkedList、Scanner
package com.myproj.snake; public class Node { private int i,j; public Node(){} public Node(int i, in ...
- 三菱Q系列PLC的智能功能模块程序
一.模拟量输入模块Q64AD 1.模块开关或者参数设置 1.1I/O分配 1.2开关设置使用通道1,0-5v, 1.3使用GX configurator设置自动刷新PLC设置智能功能模块参数,即将模拟 ...
- 447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...
- 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the ...