String

String

分割文字列

String sentence = "This is a pen.";
String[] words =sentence.split(" ");
for(String word : words){
	System.out.println(word);
	}

结果为

This
is
a
pen.

另外一个例子

String ur1 ="www.acroquest.co.jp";
String[] words = ur1.split("\\.");
//“.”在正规表现中,作为任意文字进行处理,所以“.”作为通常文字,前面需要加上转义字符“\”。
for(String word : words){
    System.out.println(word);
   }

若去掉“\\”则表示出不同的结果。 还是不太明白

将多个文字列连接起来

List<String> stringList = new ArrayList<>();
stringList.add("This");
stringList.add("is");
stringList.add("a");
stringList.add("pen.");

StringBuilder message = new StringBuilder();
for(String word :stringList){
	message.append(word);
	message.append(" ");
}
if (message.length()>0){
	message.deleteCharAt(message.length() - 1);
}//这里最后多了一个空格,所以要去掉。

System.out.println(message.toString());

这样代码非常长所以在Java8以后可以用join的方法连接:

List<String> stringList = new ArrayList<>();
stringList.add("This");
stringList.add("is");
stringList.add("a");
stringList.add("pen.");

String message = String.join(" ",stringList);
System.out.println(message);

结果为

This is a pen.

或者join方法的另外一种使用方法,第一引数作为连接文字,第二引数以后作为要连接的文字。

String message = String.join(".","www","acroquest","co","jp");
System.out.println(message);

文字列的内部位置互换

String sentence = "This is a pen.";
String replacedSentence = sentence.replace("is","at");
System.out.println(replacedSentence);

结果

That at a pen.

搜索文字列

是否含有指定的文字列

//                 0123456789
String sentence = "This is a pen.";
int index = sentence.indexOd("is");
System.out.println(index);

结果是

2

indexOf 是说的想要找的文字列 最先 登场的位置的数值。

//                 0123456789
String sentence = "This is a pen.";
int index = sentence.indexOd("at");
System.out.println(index);

想要找的文字列不存在的情况返回值index=-1;

若***不是想找最先出现的指定文字列***,可以

//                 0123456789
String sentence = "This is a pen.";
int index = sentence.indexOf("is",3);
System.out.println(index);

结果是

5

这样可以指定,从第多少个字开始以后,指定的字列的位置。

用lastIndexOf也可以从后往前找

//                 0123456789
String sentence = "This is a pen.";
int index = sentence.lastIndexOf("is");
System.out.println(index);

结果是

5
//                 0123456789
String sentence = "This is a pen.";
int index = sentence.lastIndexOf("is");
System.out.println(index);

结果是

2

那么问题来了 两个不是一前一后相反方向还是查么?怎么他们两个返回的索引值相同呢?

indexOf是从前向后查 而lastIndexOf是从后向前查 但是二者返回索引都是从前开始数数和计算的

总结:

当数组(字符串)中所要查询的数(字符串/字符)在字符串(数组)中只出现一次的时候 二者返回的索引值相同

当数组(字符串)中所要查询的数(字符串/字符)在字符串(数组)中出现两次及以上的时候

indexOf 返回的是 valuesearch 第一次在数组(字符串)出现的位置(从左往右)

lastIndexOf 返回的是 valuesearch 最后一次在数组(字符串)出现的位置(从左往右)《只不过查询的方向不同而已》

文字列的标准格式

文字列的输出

int number = 13;
String parameter = "apples";
System.out.printf("I have %d %s.",number,parameter);

结果为

I have 13 apples.

%d代表十进位的整数,%s代表文字列的信息。

int number = 13;
String parameter = "apples";
System.out.printf("I have %X %S.",number,parameter);

%X代表十六进位的整数
(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27)
(1,2,3,4,5,6,7,8,9,A ,B ,C ,D ,E ,F , 10,11,12,13,14,15,16,17,18,19,1A,1B)
%S代表大写
结果为

I have D APPLES
System.out.printf("%-10s %d","銘柄コード",number);

%-10s当中的-10代表长度

结果为

I have 13 apples.銘柄コード      13
System.out.printf("%s-10%d","銘柄コード",number);

也能表示出来但是为

I have 13 apples.銘柄コード-10 13

关于MessageFormat

MessageFormat 的表达更为简单;

import java.text.MessageFormat;

int number = 13;
String parameter = "apples";
String message = MessageFormat.format("I have {0} {1}.",number ,parameter);
System.out.println(message);

结果为

I have 13 apples.

{0} {1}的顺序改变也可以

import java.text.MessageFormat;

int number = 13;
String parameter = "apples";
String message = MessageFormat.format("I have {1} {0}.",parameter,number );
System.out.println(message);

结果为

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值