day04String

/*
String类中的创建方法 String s = new String("good");
1.char a = {'g','o','o','d'};
String s = new String (a,2,2);==>od
2.char a = {'g','o','o','d'};
String s = new String(a);
3.String s;
s = "good";

*/
public class StringDemo {
public static void main(String[] args)
{
char [] a= {'时','间'};
String s = new String(a,0,2);
System.out.println(s);

char [] a1= {'就','是'};
String s1 = new String (a1);
System.out.print(s1);

String s2,s3;
s2 = "We ";
s3 = "are";
System.out.println(s2+s3);

}

}

 

 

/*
String 类型的查询位置
String 类中提供4种来查询的方法:
public int indexOf(String str);//如果查询到就是字符第一个出现的位置s.[角标]的角标,没查询到就返回-1
public int lastIndexOf(String str);//查询的是字符最后一次出现的位置字符串的角标,没查询到就返回-1
public int indexOf(String str,int fromindex)指定位置往后查
public int lastIndexOf(String str,int fromindex)从指定位置往前查询

例子:
在 两个字符串中8888和B888查询出不同
*/
public class StringSearchDemo {
public static void main(String[] args) {
String s = new String("we are friend !");
int x = s.indexOf("e");//s【3】首次出现的位置
System.out.println(x);
int q = s.lastIndexOf("e");//s[10];最后一次的位置
System.out.println(q);

String s1 = "8888";
String s2 = "B888";

if (s1.indexOf("B")>-1)
{
System.out.println(s1+"这个字符串包含B");
}
if (s2.indexOf("B")>-1)
{
System.out.println(s2+"这个字符串包含B");
}

}

}


/*
String 类中长度的查询
用s.length()的方法来查询长度然后赋值给一个局部变量
注意其中空格也占一个长度
*/

public class StringDemo2 {
public static void main(String[] args) {
String s1 = new String ("hello");
String s2 = new String ("world");
String s3 = s1+" " +s2;
System.out.println(s3);
int size1 = s1.length();
System.out.println(size1);
int size2= s2.length();
System.out.println(size2);
int size3 = s3.length();
System.out.println(size3);
}

}

转载于:https://www.cnblogs.com/secretmonster/p/8693092.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值