java.lang.String
1、String 是一个类,广泛应用于 Java 程序中,相当于一系列的字符串。在 Java 语言中 strings are objects。创建一个 strings 最直接的方式是
String greeting = "Hello world!";
可以利用新的关键字和 String 构造器创建新对象。String 类有十三个构造器,可以根据传入类型的不同,构造不同的对象。比如 character 数组:
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.' };
String helloString = new String(helloArray);
System.out.println(helloString);
2、String 类是不变模式,所以它创建的 String 对象也不可变。String 类有一系列用于修改 strings 对象的方法。因为 strings 对象是不可变的,这些方法真正做的是创建并返回一个包含操作结果的新 string 。
3、用于获取一个对象信息的方法是存取器方法。比如,length() 方法,它返回这个 string 对象所包含的字符数量。
public class StringDemo {
public static void main(String[] args) {
String palindrome = "Dot saw I was Tod";
int len = palindrome.length(); // 获取长度
char[] tempCharArray = new char[len];
char[] charArray = new char[len];
// put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i); // 获取指定位置的 char 值 // 第一个 character 是 0 最后一个是 length()-1
}
// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len - 1 - j];
}
String reversePalindrome = new String(charArray); // 将 char 数组转化为 String 类型
System.out.println(reversePalindrome);
}
}
4、String 类包含一个 getChar() 方法,将一个 string 或 string 的一部分转化为一个 characters 数组。因此我们可以将上述程序的第一个 for 循环改为
palindrome.getChars(0, len, tempCharArray, 0);
void java.lang.String.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.
The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1 (thus the total number of characters to be copied is srcEnd-srcBegin). The characters are copied into the subarray of dst starting at index dstBegin and ending at index:
dstbegin + (srcEnd-srcBegin) - 1
5、String 类包含一个连接两个字符串的方法:
string1.concat(string2);
返回一个新的 string,即string1string2(string2 添加至 string1 的末尾)。
也可以这么用:
"My name is ".concat("Rumplestiltskin");
连接字符串,更常用的是"+"。比如:
"Hello," + " world" + "!"
的结果是:
"Hello, world!"
这种连接方法可以用来连接各种对象。对于不是 String 的对象,可以用 toString() 方法转化为 String
6、Java 语言不允许 strings 跨行,所以必须在每一行的结尾,用"+"操作符对每一行进行连接。
String quote =
"Now is the time for all good " +
"men to come to the aid of their country. "
7、将 string 转换为对应的数字类,数字类中的 valueOf 方法,。
public class ValueOfDemo {
public static void main(String[] args) {
// this program requires two arguments on the command line
if (args.length == 2) {
// convert strings to numbers
float a = (Float.valueOf(args[0])).floatValue();
float b = (Float.valueOf(args[1])).floatValue();
// do some arithmetic
System.out.println("a + b = " + (a + b));
System.out.println("a - b = " + (a - b));
} else {
System.out.println("This program " +
"requires two command-line arguments.");
}
}
}
parseXXXX() 方法 : 也是返回数据。praseXXXX() 返回的为原始的数据(如 int 类型数据),valueOf返回的为对象(如 Integer 类的对象)
8、将数字转化为 string :代码展示如下。
// 方法一
int i;
// Concatenate "i" with an empty string; conversion is handled for you.
String s1 = "" + i;
// 方法二
// The valueOf class method.
String s2 = String.valueOf(i); // 方法三
int i;
double d;
String s3 = Integer.toString(i);
String s4 = Double.toString(d);
9、String 中的 substring 方法截取子字符串:
String substring(int beginIndex, int endIndex)
// 第一个int为开始的索引,对应String数字中的开始位置, 从0开始
// 第二个是截止的索引位置,对应String中的结束位置 // 下例输出 ri
System.out.println("String".substring(2, 4));
public String substring(int beginIndex)
"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)
10、String[] split(String regex, int limit)
方法,头一个参数String regex表示字符串分割的模式,包括分隔符和正则表达式;但是第二个参数limit比较迷糊人,api中这样解释:
limit 参数控制模式应用的次数,因此影响所得数组的长度。如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,数组的长度将不会大于 n,而且数组的最后一项将包含所有超出最后匹配的定界符的输入。如果 n 为非正,那么模式将被应用尽可能多的次数,而且数组可以是任何长度。如果 n 为 0,那么模式将被应用尽可能多的次数,数组可以是任何长度,并且结尾空字符串将被丢弃。
11、String trim() 去掉字符串两边的空格
java.lang.String的更多相关文章
- java.lang.String.getBytes(String charsetName)方法实例
java.lang.String.getBytes(String charsetName) 方法编码将此String使用指定的字符集的字节序列,并将结果存储到一个新的字节数组. 声明 以下是java. ...
- hibernate报错Unknown integral data type for ids : java.lang.String
package com.model; // Generated 2016-10-27 14:02:17 by Hibernate Tools 4.3.1.Final /** * CmDept gene ...
- 前台传参数时间类型不匹配:type 'java.lang.String' to required type 'java.util.Date' for property 'createDate'
springMVC action接收参数: org.springframework.validation.BindException: org.springframework.validation.B ...
- 记录maven java.lang.String cannot be cast to XX error
在项目开发中自定义了一个maven plugin,在本地能够很好的工作,但是在ci server上却无法正常工作报错为: --------------------------------------- ...
- javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String
javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String javax.el.Bean ...
- Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found
今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...
- [Ljava.lang.String和java.lang.String区别
在做项目时报了一个got class [Ljava.lang.String的提示,当时看到[Ljava.lang.String这个时,感觉有点怪怪的,第一次遇到这种情况.最后在网上查了下才明白.是数组 ...
- Spring mvc 报错:No qualifying bean of type [java.lang.String] found for dependency:
具体错误: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean w ...
- java.lang.ClassCastException: java.lang.String cannot be cast to com.jy.hfims.domain 映射实体类型错误
今天在做 excel导出的时候,出现了一个问题"java.lang.ClassCastException: java.lang.String cannot be cast to com.do ...
随机推荐
- codeforce ABBYY Cup 3.0 - Finals (online version) B2. Shave Beaver! 线段树
B2. Shave Beaver! The Smart Beaver has recently designed and built an innovative nanotechnologic a ...
- hdu 4114(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...
- 重写ViewPager方法,防止滑动广告尾页的时候,Fragment也改变! (如果广告设置为轮播的话,不需要重写ViewPager)
public class MyViewPager extends ViewPager{ public MyViewPager(Context context) { this(context, null ...
- Temp文件夹缺少network service权限,webservice能访问,但是不能调用
给C:\WINDOWS\temp文件夹添加NETWORK SERVICE权限即可
- WebApi:路由和Action选择
译自:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection ...
- oracle存储海量数据 设计方案
日历基本活动表的数据有四千万条,在这些生产库业务中是小的了. 从三个方面提高效率: 1.sql语句 要绑定变量,sql语句书写规范这些,包括的就多了.主要目的就是提高数据库吞吐量及业务SQL响应时间. ...
- ural 1072. Routing
1072. Routing Time limit: 1.0 secondMemory limit: 64 MB There is a TCP/IP net of several computers. ...
- C语言数组删除增加一个元素
malloc,realloc,calloc一直很头疼,这次笔试题需要在数组后重新分配新的空间的代码是: //删除函数,删除ptr中的ptr[in]元素,n是数组原来的长度. void rmv(int ...
- [知识点]网络流之Edmond-Karp算法
// 此博文为迁移而来,写于2015年2月2日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vr12.html ...
- Vijos 1092 全排列
题目链接 来个水题..难得的1Y. #include <cstdio> #include <cstring> #include <iostream> using n ...