您的位置 首页 java

Java字符串对象详细总结(初学必看)

《大数据和人工智能交流》头条号向广大初学者新增C 、 Java 、Python 、Scala、javascript 等目前流行的计算机、大数据编程语言,希望大家以后关注本头条号更多的内容。

1、创建字符串

(1)方式1

String str1=new String(“abc”);

String str2=new String(“abc”);

System.out.println(str1);

(2)方式2

char c[]={‘a’,’b’,’c’};

String s=new String(c);

System.out.println(s);

2、通过调用length()方法得到String的长度.

public static void main(String[] args)

{

String s=”abc”;

int len=s.length();

System.out.println(len);

}

3、charAt()方法:去字符串中的字符

String s=”abc”;

char c=s.charAt(1);

System.out.println(c);

面试题:有个字符串s=”abc”;请讲字符串翻转ss=”cba”;

private static String reverseStr(String s) {

String ss=””;

for(int i=s.length()-1;i>=0;i–)

{

ss+=s.charAt(i);

}

return ss;

}

4、String的split方法

String s=”s001,lily,21,F,100″;

String[] str = s.split(“,”);

for(String ss:str)

{

System.out.println(ss);

}

示例:

public class Demo {

public String show()

{

int count=0;

String ss=””;

for(int i=1;i<=100;i++)

{

count=0;

for(int j=1;j<=i;j++)

{

if(i%j==0)

{

count++;

}

}

if(count==2)

{

ss=ss+i+”;”;

}

}

return ss;

}

public static void main(String[] args)

{

Demo d=new Demo();

String s = d.show();

String[] str = s.split(“;”);

if(!””.equals(str))

{

for(String s1:str)

{

System.out.print(s1+” “);

}

}

}

}

5、indexOf方法

indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring

示例:

public static void main(String[] args)

{

String s=”abcded”;

int index = s.indexOf(‘d’);

System.out.println(index);

}

6、subString()是提取字符串的另一种方法,它可以指定从何处开始提取字符串以及何处结束。

【1】substring(int beginIndex)

Returns a new string that is a substring of this string.

示例:

public static void main(String[] args)

{

String s=”abcded”;

String ss = s.substring(2);

System.out.println(ss);

}

【2】String substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string.

示例:

public static void main(String[] args)

{

String s=”abcdedety”;

String ss = s.substring(2,5);

System.out.println(ss);

}

7、剪空格 trim

public static void main(String[] args)

{

String s=” abc “;

System.out.println(“剪掉前:=====”+s+”====”);

String ss = s.trim();

System.out.println(“剪掉后:=====”+ss+”====”);

}

8、字符串替换函数

【1】 replace (char oldChar, char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

【2】replace(CharSequence target, CharSequence replacement)

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.

【3】String replaceAll(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.

Parameters:

regex: the regular expression to which this string is to be matched

replacement :the string to be substituted for each match

示例1:使用空串来替换空格

public static void main(String[] args)

{

String s=”abc abc abc”;

String ss=s.replaceAll(” “, “”);

System.out.println(ss);

}

示例2:

public static void main(String[] args)

{

String s=”abc我靠abc我靠abc”;

String ss=s.replaceAll(“我靠”, “”);

System.out.println(ss);

}

9、字符串链接

public static void main(String[] args)

{

String s=”aaa”;

s=s.concat(“111”).concat(“222”);

System.out.println(s);

}

10、修改可变字符串 StringBuffer

(1)StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在

进行字符串处理时,不生成新的对象,在内存使用上要优于String类。所以在实际使用时,如果经常需要对一个字符串进行修改,

例如插入、删除等操作,使用StringBuffer要更加适合一些。

(2))在StringBuffer类中存在很多和String类一样的方法,这些方法在功能上和String类中的功能是完全一样的。

(3)但是有一个最显著的区别在于,对于StringBuffer对象的每次修改都会改变对象自身,这点是和String类最大的区别。

另外由于StringBuffer是线程安全的,关于线程的概念后续有专门的章节进行介绍,所以在多线程程序中也可以很方便的进行使用,

但是程序的执行效率相对来说就要稍微慢一些。

StringBuffer类为可变字符串的修改提供了3种方法,在字符串中间插入和改变某个位置所在的字符。

【1】在字符串后面追加:用append()方法将各种对象加入到字符串中。

【2】在字符串中间插入:用 insert ()方法。例

StringBuffer str=new StringBuffer(“This is a String”);

Str.insert(9,”test”);

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

示例1:

public static void main(String[] args)

{

StringBuffer sf =new StringBuffer(“hello”);

sf.append(100);

sf.append(“me”);

System.out.println(sf);

}

示例2:

public static void main(String[] args)

{

String s=”abc”;

StringBuffer ss=new StringBuffer();

for(int i=s.length()-1;i>=0;i–)

{

ss.append(s.charAt(i));

}

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

}

文章来源:智云一二三科技

文章标题:Java字符串对象详细总结(初学必看)

文章地址:https://www.zhihuclub.com/182036.shtml

关于作者: 智云科技

热门文章

网站地图