一,字符数组与字符串。

一个字符串可以变成一个字符数组,同样,一个字符数组可以变成一个字符串。

在String类中提供了以下操作方法。

1)将字符串变成字符数组:public char[] toCharArray();

2) 字符数组变成字符串:public String(char [] value)

             public String(char[] value,int offset,int count);//表示从offset位置开始的count个字符,组合成为一个字符串。

public class StringAPIDemo01{
public static void main(String args[]){
String str1 = "hello" ; // 定义字符串
char c[] = str1.toCharArray() ; // 将一个字符串变为字符数组
for(int i=0;i<c.length;i++){ // 循环输出
System.out.print(c[i] + "、") ;
}
System.out.println("") ; // 换行
String str2 = new String(c) ; // 将全部的字符数组变为String
String str3 = new String(c,0,3) ; // 将部分字符数组变为String
System.out.println(str2) ; // 输出字符串
System.out.println(str3) ; // 输出字符串
}
}
运行结果:
h,e,l,l,o
hello
hel

上面分别使用了字符串变数组,和数组变字符串两种方法。

二,从字符串中取出指定位置的字符。

如果要做这种操作,则返回类型肯定是char类型。

使用方法:public char charAt(int index)

public class StringAPIDemo02{
public static void main(String args[]){
String str1 = "hello" ; // 定义String对象
System.out.println(str1.charAt(3)) ; // 取出字符串中第四个字符
}
};
输出结果:
l

三,字符串与bety数组的转换。

byte数组,在一般I/O中经常用到

在String类中,提供以下方法进行字符串和字节数组的转换。

1)字符串变为字节数组:public byte getBytes();

2)讲一个字节数组变为字符串:

将全部数组变为字符串:public String(byte[] byte);

将部分数组变成字符串:public String(char[] byte,int offset,int count);

public class StringAPIDemo03{
public static void main(String args[]){
String str1 = "hello" ; // 定义字符串
byte b[] = str1.getBytes() ; // 将字符串变为byte数组
System.out.println(new String(b)) ; // 将全部的byte数组变为字符串
System.out.println(new String(b,1,3)) ; // 将部分的byte数组变为字符串
}
};

四,取得一个数组的长度。

public int length();

public class StringAPIDemo04{
public static void main(String args[]){
String str1 = "hello LiXingHua" ; // 定义字符串变量
System.out.println("\""+str1+"\"的长度为:"+str1.length()) ;
}
};

五,查找指定字符串是否存在。

在实际操作中,经常会用到判断一个字符串是否存在某些内容,此时就可以使用以下方法:

1)从头开始查找:public int indexof(String str);返回字符串开始的下标。

2)从指定位置查找:public int indexof(String str,int fromIndex);//返回的是一个int数据,表示的是字符串的具体位置,如果没有,返回-1,

public class StringAPIDemo05{
public static void main(String args[]){
String str1 = "abcdefgcgh" ; // 声明字符串
System.out.println(str1.indexOf("c")) ; // 查到返回位置
System.out.println(str1.indexOf("c",3)) ; // 查到返回位置,从第4个位置开始查找
System.out.println(str1.indexOf("x")) ; // 没有查到返回-1
}
};
运行结果:
2,7,-1

六,去掉空格。

trim()可以去掉字符串左右的空格,但是字符串中间的空格是无法去掉的。

public class StringAPIDemo06{
public static void main(String args[]){
String str1 = " hello " ; // 定义字符串
System.out.println(str1.trim()) ; // 去掉左右空格后输出
}
};
运行结果:
hello

七,字符截取。

从一个字符串中取出部分内容,使用的方法是:

1)从指定位置开始截取到最后位置;public String substring(int beginIndex);

2)截取指定范围的位置内容:public String substring(int beginIndex,int endIndex);

public class StringAPIDemo07{
public static void main(String args[]){
String str1 = "hello world" ; // 定义字符串
System.out.println(str1.substring(6)) ; // 从第7个位置开始截取
System.out.println(str1.substring(0,5)) ; // 截取0~5个位置的内容
}
};
运行结果:
world
hello

八,拆分字符串。

如果现在需要按指定字符串去拆分一个字符串的话,则使用:public String[] split(String regex)

public class StringAPIDemo08{
public static void main(String args[]){
String str1 = "hello world" ; // 定义字符串
String s[] = str1.split(" ") ; // 按空格进行字符串的拆分
for(int i=0;i<s.length;i++){ // 循环输出
System.out.println(s[i]) ;
}
}
运行结果:
hello
world

例子按照空格,被拆分成两个字符串,形成一个包含两个字符串的字符串数组。

九,大小写转换。

将一个大写的字符串全部变成小写:public String toUpperCase();

讲一个小写的字符串全部变成大学:public String toLowerCase();

public class StringAPIDemo09{
public static void main(String args[]){
System.out.println("将\"hello world\"转成大写:" + "hello world".toUpperCase()) ;
System.out.println("将\"HELLO WORLD\"转成小写:" + "HELLO WORLD".toLowerCase()) ;
}
运行结果:
HELLO WORLD
hello word

十,判断是否以指定的字符串开头或者结尾。

在String中可以以以下方法完成。

1)是否以指定字符串开头:public boolean starsWith(string prefix)

2)是否以指定字符串结尾:public  boolean endWith(string suffix)

public class StringAPIDemo10{
public static void main(String args[]){
String str1 = "**HELLO" ; // 定义字符串
String str2 = "HELLO**" ; // 定义字符串
if(str1.startsWith("**")){ // 判断是否以“**”开头
System.out.println("(**HELLO)以**开头") ;
}
if(str2.endsWith("**")){ // 判断是否以“**”结尾
System.out.println("(HELLO**)以**结尾") ;
}
}
};
运行结果:
(**HELLO)以**开头
(HELLO**)以**结尾

十一,不区分大小写的字符串比较。

public boolean equalsIgnoreCase(String anotherString)
public class StringAPIDemo11{
public static void main(String args[]){
String str1 = "HELLO" ; // 定义字符串
String str2 = "hello" ; // 定义字符串
System.out.println("\"HELLO\" equals \"hello\" " + str1.equals(str2)) ;
System.out.println("\"HELLO\" equalsIgnoreCase \"hello\" "
+ str1.equalsIgnoreCase(str2)) ; // 不区分大小写的比较
}
};
运行结果:
false
ture

十二,字符串替换。

使用这个方法完成字符串替换:

public String replaceAll(String regex,String replacement);

public class StringAPIDemo12{
public static void main(String args[]){
String str = "hello" ; // 定义字符串
String newStr = str.replaceAll("l","x") ; // 现在将所有的l替换成x
System.out.println("替换之后的结果:" + newStr) ;
}
};
运行结果:
替换之后的结果:hexxo

String类常用方法。的更多相关文章

  1. JAVA中String类常用方法 I

    String类常用方法有: int length() -– 返回当前字符串的长度 int indexOf(int ch) -– 查找ch字符在该字符串中第一次出现的位置 int indexOf(Str ...

  2. JAVA String类常用方法

    一.String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象.java把String类声明的final类,不能有类.String类对象创建 ...

  3. 菜鸡的Java笔记 第十四 String 类常用方法

    /*String 类常用方法    将所有String类的常用方法全部记下来,包括方法名称,参数作用以及类型    一个成熟的编程语言,除了它的语法非常完善之外,那么也需要提供有大量的开发类库     ...

  4. Java 中的 String 类常用方法

    字符串广泛应用在Java编程中,在Java中字符串属于对象,String 类提供了许多用来处理字符串的方法,例如,获取字符串长度.对字符串进行截取.将字符串转换为大写或小写.字符串分割等. Strin ...

  5. Java的String类常用方法

    一.构造函数 String(byte[ ] bytes):通过byte数组构造字符串对象. String(char[ ] value):通过char数组构造字符串对象. String(Sting or ...

  6. String类常用方法

    1.String类的特点,字符串一旦被初始化就不会被改变. 2.String对象定义的两种方式 ①String s = "affdf";这种定义方式是在字符串常量池中创建一个Str ...

  7. Java中String类常用方法(字符串中的子字符串的个数)

    重点内容 4种方法: 1.int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引. 2.int indexOf(String str, int startInde ...

  8. String类常用方法练习

    String 类代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现. 字符串是常量:它们的值在创建之后不能更改.字符串缓冲区支持可变的字符串. ...

  9. java的异常抛出和String类常用方法

    一.异常抛出 异常是程序的异种非错误的意外情况,分为运行期异常(RuntimeException)和编译期异常(CheckedExcption) 处理异常可以用try——catch或自定义 impor ...

随机推荐

  1. Python: 关于nose

    1. 使用rednose增强输出 pip install rednose nosetests --rednose tests 2. 使用coverage pip install coverage no ...

  2. 弃用的异步get和post方法之Block方法

    #import "ViewController.h" #import "Header.h" @interface ViewController () <N ...

  3. 【转】从viewController讲到强制横屏,附IOS5强制横屏的有效办法

    文字罗嗦,篇幅较长,只需营养可直接看红字部分. 一个viewController的初始化大概涉及到如下几个方法的调用: initWithNibName:bundle: viewDidLoad view ...

  4. LightSpeed 相关问题处理

    1. 关于KeyTable 配置文件中有一个节点 lightSpeedContexts  该节点下存放的是一些使用LightSpeed的配置,如 <add name="myDB&quo ...

  5. centos7 拨号之后添加路由

    问题:拨号主机再自动拨号(/sbin/ifdown ppp0;/sbin/ifup ppp0)之后,无法上网(没有添加路由) 思路:在拨号程序中添加路由代码 vim /sbin/ifup { slee ...

  6. sublime生产力提升利器

    sublime 操作快捷键功能-生产力提升利器 Go to anything  ctrl+p 支持快速模糊匹配 查找替换  ctrl+h 多行游标(当只需查找/替换/选中部分相同内容时)有以下方式来产 ...

  7. Codeforces Round #254 DZY Loves Colors

    题意:输入n, m ; 有n给位置, 初始时第i个位置的color为i, colorfulness为0.      有m次操作,一种是把成段的区域color更新为x, 对于更新的区域,每个位置(令第i ...

  8. python django 与数据库的交互

    下载没有任何问题的mysqdb http://www.codegood.com/archives/4 1创建一个新的app. python manage.py startapp books 2 激活a ...

  9. Error during installing HAXM, VT-X not working 在安装HAXM错误,开始不工作

    最佳答案 (Best Answer) Some antivirus options prevent Haxm installation. ie: Avast : settings (parametre ...

  10. ffmpeg编译x264, 这个libffmpeg即可解码又可以h264编码

      http://blog.csdn.net/u012917616/article/details/40921861 不废话,直接上.sh脚本: export NDK=/home/xxx/my_sof ...