Java基础二----- string 类

一、String类

String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间。

二、String类对象的创建

字符串声明:String stringName;

字符串创建:(1)   stringName = new String(字符串常量);

      或               (2)  stringName = 字符串常量;

三、String类构造方法

1、public String()

无参构造方法,用来创建空字符串的String对象。

String str1 = new String();

2、public String(String value)

用已知的字符串value创建一个String对象。

1    String str2 =new  String("asdf");

2    String str3 =new  String(str2);

3、public String(char[]  value)

用字符数组value创建一个String对象。

 char[] value = {"a","b","c","d"};

  String str4 =new String(value);

//相当于String str4 = new String("abcd");

4、public String(char chars[], int startIndex, int numChars)

 用字符数组chars的startIndex开始的numChars个字符创建一个String对象。

char[] value = {"a","b","c","d"};

String str5 =new String(value,1,2);

//相当于String str5 = new String("bc");

5、public String(byte[]  values)

用byte数组values创建一个String对象。

byte[] strb =new byte[]{65,66};

String str6 = newString(strb);

//相当于String str6 = new String("AB");

四、String类常用方法

1、求字符串长度

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

String str =new String("asdfzxc");

int  strlength = str.length();    //strlength = 7

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

public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一个字符索引是0,最后一个是length()-1。

String str =new String("asdfzxc");

charch = str.charAt(4);//ch = z

3、提取子串

用String类的substring方法可以提取字符串中的子串,该方法有两种常用参数:

1)public String substring(int beginIndex)//该方法从beginIndex位置起,从当前字符串中取出剩余的字符作为一个新的字符串返回。

2)public String substring(int beginIndex, int endIndex)//该方法从beginIndex位置起,从当前字符串中取出到endIndex-1位置的字符作为一个新的字符串返回。

String str1 =new String("asdfzxc");

String str2 = str1.substring(2);//str2 = "dfzxc"

String str3 = str1.substring(2,5);//str3 = "dfz"

4、字符串比较

1)public int compareTo(String anotherString)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。

2)public int compareToIgnore(String anotherString)//与compareTo方法相似,但忽略大小写。

3)public boolean equals(Object anotherObject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。

4)public boolean equalsIgnoreCase(String anotherString)//与equals方法相似,但忽略大小写。

String  str1 =newString("abc");

String  str2 =newString("ABC");

int a = str1.compareTo(str2);//a>0

int b = str1.compareTo(str2);//b=0

boolean c = str1.equals(str2);//c=false

boolean d = str1.equalsIgnoreCase(str2);//d=true

5、字符串连接

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

String str ="aa".concat("bb").concat("cc");

相当于String str ="aa"+"bb"+"cc";

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 str ="I am a good student";

int a = str.indexOf('a');               //a = 2

int b = str.indexOf("good");         //b = 7

int c = str.indexOf("w",2);            //c = -1

int d = str.lastIndexOf("a");          //d = 5

int e = str.lastIndexOf("a",3);          //e = 2

7、字符串中字符的大小写转换

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

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

String str =newString("asDF");

String str1 = str.toLowerCase();     //str1 = "asdf"

String str2 = str.toUpperCase();      //str2 = "ASDF"

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"

9、其他类方法

1)String trim()//截去字符串两端的空格,但对于中间的空格不处理。

String str ="a sd";

String str1 =str.trim();

int a = str.length();//a = 64

int b = str1.length();//b = 4

2)boolean statWith(String prefix)boolean endWith(String suffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。

String str ="asdfgh";

boolean a = str.statWith("as");//a = true

boolean b = str.endWith("gh");//b = true

3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)//从当前字符串的firstStart位置开始比较,取长度为length的一个子字符串,other字符串从otherStart位置开始,指定另外一个长度为length的字符串,两字符串比较,当b为true时字符串不区分大小写。

4)contains(Stringstr)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。

String str ="student";

str.contains("stu");//true

str.contains("ok");//false

5)String[] split(String str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。

String str ="asd!qwe|zxc#";

String[] str1 = str.split("!|#");//str1[0] = "asd";str1[1] = "qwe";str1[2] = "zxc";

五、字符串与基本类型的转换

1、字符串转换为基本类型

java.lang包中有Byte、Short、Integer、Float、Double类的调用方法:

1)public static byte parseByte(String s)

2)public static short parseShort(String s)

3)public static short parseInt(String s)

4)public static long parseLong(String s)

5)public static float parseFloat(String s)

6)public static double parseDouble(String s)

例如:

int n = Integer.parseInt("12");

float f = Float.parseFloat("12.34");

double  d = Double.parseDouble("1.124");

2、基本类型转换为字符串类型

String类中提供了String valueOf()放法,用作基本类型转换为字符串类型。

1)static String valueOf(char data[])

2)static String valueOf(char data[], int offset, int count)

3)static String valueOf(boolean b)

4)static String valueOf(char c)

5)static String valueOf(int i)

6)static String valueOf(long l)

7)static String valueOf(float f)

8)static String valueOf(double d)

例如:

1String s1 = String.valueOf(12);

2String s1 = String.valueOf(12.34);

3、进制转换

使用Long类中的方法得到整数之间的各种进制转换的方法:

Long.toBinaryString(long l)

Long.toOctalString(long l)

Long.toHexString(long l)

Long.toString(long l, int p)//p作为任意进制

参考文章www.cnblogs.com/ABook/p/5527341.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容

  • java笔记第一天 == 和 equals ==比较的比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量...
    jmychou阅读 1,440评论 0 3
  • 今天学习了 Java 字符串的常用操作,写出来和简友们分享一下☺️还是老规矩,先上思维导图。 1.String 概...
    SawyerZh阅读 682评论 5 10
  • 【程序1】 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔...
    叶总韩阅读 5,068评论 0 41
  • 在编写程序的过程中,不了避免的要用到字符串,所以String类的常用方法的用法是必须掌握的。学习一个类的使用方法最...
    Geg_Wuz阅读 1,171评论 0 4
  • 预警: 短篇慢热/角色自然老死设定/我觉得是虐的但是狸哥说是糖里有毒估计还是有点甜的/骗无料专用短篇 =====正...
    后山狗阅读 1,144评论 3 5