首页 > 编程知识 正文

java循环做字符串拼接,java do while循环语句

时间:2023-05-06 11:30:49 阅读:169595 作者:1193

while循环while是最基本的循环

结构如下。

while (布尔表达式(//循环的内容)布尔表达式是逻辑函数,常量只能为false和true

只要布尔表达式为true,循环就会一直运行

while循环由初始化、条件判断、循环体、反复4个结构组成。

让我们来看看简单的东西。 ((来吧,展示一下) )。

字符串的使用是什么? 关于下面的内容,大部分都是

原文链接: 3359 blog.csdn.net/practical _ sharp/article/details/87550434

字符串是由多个字符组成的数据的集合

Java字符串可以分为以下两类:

1 .字符串类

2.StringBuffer和StringBuilder类

String类是immutable(不可变的),不可修改,所有当我们用”=,+“这些运算的时候会重新生成一个新的String类的实例,在循环中使用String类的”+=“运算会带来一定的效率问题。

String类声明与创建

字符串声明: String stringName;

创建字符串: stringName=new String; 或字符串名称=字符串常量;

字符串名称; name='Jack '; //stringname=newstring('jack );

String类构造方法

1、public String (),一种无参数的方法,用于创建空字符串的String对象。

例如,String string1=new String (; String1==null 2,具有公共字符串(string value ),使用已知字符串value创建字符串对象。

示例: stringstr2=newstring('Helllo,world!' ); str2='Hello,world!' stringstr3=newstring(str2; str3='Hello,world!' 3、使用publicString(char[]value )、字符数组value创建string对象。

示例: char[] value={'a ',' b ',' c ',' d'}; stringstr4=newstring(value; //stringstr4=newstring(ABCD ); 4、用从publicString(charchars[],int startIndex,int numChars )和字符数组chars的startIndex开始的numChars字符创建string对象。

示例: char[] value={'a ',' b ',' c ',' d'}; stringstr5=newstring(value,1,2 ); //stringstr5=相当于new string (BC );String类的常用方法

在String类java.lang包中,java使用String类创建属于对象的字符串变量。 java是声明字符串类的final类。 **String类的对象在创建后不能更改。 **由0个或多个字符组成,包含在一对双引号之间。 String类有很多方法,可以通过查看API来理解。

1、求字符串长度

public int length () /返回字符串的长度

2、求字符串某一位置字符

publiccharcharat(intindex ) /返回字符串中指定位置的字符。 请注意,字符串的第一个字符索引为0,最后一个字符为length ()-1。

3、提取子串

可以使用String类的substring方法来提取字符串中的子字符串。 此方法有两个常见参数:

public string substring (int beginindex ) /此方法从begin index的位置检索当前字符串中的剩余字符,并将其作为新字符串返回。

2 ) public string substring (int beginindex,int endIndex ) /该方法将从begin index的位置检索到当前字符串endIndex-1的位置的字符作为新字符串返回stringstr1=newstring(abcdefg ); stringstr2=str1.substring(2; //str2='cdefg '; stringstr3=str1. substring (2,5 ); //str3='cde ';4、字符串比较

1 )公共公司到(stringa not

herString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。
3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写

String name = "Liu Yankee";name.compareTo("Hello,World!");//name.compareTo("Hello,World!")返回负整数name.compareTo("Yellow");//name.compareTo("Yellow")返回正整数name.compareTo("Liu Yankee");//返回0name.equal("Liu Kee");//返回falseString name = "Liu YanKee";System.out.println(name.equalsIgnoreCase("Liu YanKee"));//输出true

5、字符串连接
public String concat(String str)//将参数中的字符串str连接到当前字符串的后面,效果等价于"+"。

String name1 = "Tom";String name2 = "son";System.out.println(name1 + name2);//输出Tomson

6、字符串中单个字符查找
1)public int indexOf(int ch/String str)//用于查找当前字符串中字符或子串,返回字符或子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
2)public int indexOf(int ch/String str, int fromIndex)//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
3)public int lastIndexOf(int ch/String str)//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
4)public int lastIndexOf(int ch/String str, int fromIndex)//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查

String name = "Liu YanKee";name.indexOf("Kee");//name.indexOf("Kee")==7//name.indexOf("cdoihj")=-1

7、字符串中字符的大小写转换
1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串

String name ="Jack Ma"; System.out.println(name.toLowerCase());System.out.println(name.toUpperCase());输出:jack maJACK MA

8、字符串中字符的替换
1)public String replace(char oldChar, char newChar)//用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。
2)public String replaceFirst(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的第一个和字符串regex相匹配的子串,应将新的字符串返回。
3)public String replaceAll(String regex, String replacement)//该方法用字符replacement的内容替换当前字符串中遇到的所有和字符串regex相匹配的子串,应将新的字符串返回。

String str = "asdzxcasd";String str1 = str.replace('a','g');//str1 = "gsdzxcgsd";String str2 = str.replace("asd","fgh");//str2 = "fghzxcfgh";String str3 = str.replaceFirst("asd","fgh");//str3 = "fghzxcasd";String str4 = str.replaceAll("asd","fgh");//str4 = "fghzxcfgh"; 字符串与基本类型的转换

public byte[] getBytes(): 把字符串转换为字节数组。
public char[] toCharArray(): 把字符串转换为字符数组。
public static String valueOf(char[] chs): 把字符数组转成字符串。
public static String valueOf(int i): 把int类型的数据转成字符串。(String类的valueOf方法可以把任意类型的数据转成字符串。)
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写。
public String concat(String str): 把字符串拼接。

StringBuffer和StringBuilder类

既然在Java中已经存在了String类,那为什么还需要StringBuilder和StringBuffer类呢?事实上,StringBuilder和StringBuffer类拥有的成员属性以及成员方法基本相同,区别是StringBuffer类的成员方法前面多了一个关键字:synchronized,不用多说,这个关键字是在多线程访问时起到安全保护作用的,也就是说StringBuffer是线程安全的。

String、StringBuilder、StringBuffer三者的执行效率:

StringBuilder > StringBuffer > String

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。