您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

java string怎么使用?

2025/5/23 16:45:11发布26次查看
java string的用法
string类在java.lang包中,java使用string类创建一个字符串变量,字符串变量属于对象。java把string类声明的final类,不能有子类。string类对象创建后不能修改,由0或多个字符组成,包含在一对双引号之间,下面简单的熟悉一下其常用的api
java.lang.string char charat (int index) 返回index所指定的字符 string concat(string str) 将两字符串连接 boolean endswith(string str) 测试字符串是否以str结尾 boolean equals(object obj) 比较两对象 char[] getbytes 将字符串转换成字符数组返回 char[] getbytes(string str) 将指定的字符串转成制服数组返回 boolean startswith(string str) 测试字符串是否以str开始 int length() 返回字符串的长度 string replace(char old ,char new) 将old用new替代 char[] tochararray 将字符串转换成字符数组 string tolowercase() 将字符串内的字符改写成小写 string touppercase() 将字符串内的字符改写成大写 string valueof(boolean b) 将布尔方法b的内容用字符串表示 string valueof(char ch) 将字符ch的内容用字符串表示 string valueof(int index) 将数字index的内容用字符串表示 string valueof(long l) 将长整数字l的内容用字符串表示 string substring(int1,int2) 取出字符串内第int1位置到int2的字符串
1.构造方法
//直接初始化string str = "abc";//使用带参构造方法初始化char[] char = {'a','b','c'};string str1 = new string("abc");string str2 = new string(str);string str3 = new string(char);
2.求字符串长度和某一位置字符
string str = new string("abcdef");int strlength = str.length();//strlength = 7char ch = str.charat(4);//ch = e
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("abcdef");string str2 = str1.substring(2);//str2 = "cdef"string str3 = str1.substring(2,5);//str3 = "cde"
4.字符串比较
1)public int compareto(string anotherstring)//该方法是对字符串内容按字典顺序进行大小比较,通过返回的整数值指明当前字符串与参数字符串的大小关系。若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
2)public int comparetoignorecase(string anotherstring)//与compareto方法相似,但忽略大小写。
3)public boolean equals(object anotherobject)//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
4)public boolean equalsignorecase(string anotherstring)//与equals方法相似,但忽略大小写。
string str1 = new string("abc");string str2 = new string("abc");int a = str1.compareto(str2);//a>0int b = str1.comparetoignorecase(str2);//b=0boolean c = str1.equals(str2);//c=falseboolean 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 really miss you !";int a = str.indexof('a');//a = 4int b = str.indexof("really");//b = 2int c = str.indexof("gg",2);//c = -1int d = str.lastindexof('s');//d = 6int e = str.lastindexof('s',7);//e = 7
7.大小写转换
1)public string tolowercase()//返回将当前字符串中所有字符转换成小写后的新串
2)public string touppercase()//返回将当前字符串中所有字符转换成大写后的新串
string str = new string("abcd");string str1 = str.tolowercase();//str1 = "abcd"string str2 = str.touppercase();//str2 = "abcd"
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 bc ";string str1 = str.trim();int a = str.length();//a = 6int b = str1.length();//b = 4
2)boolean statwith(string prefix)或boolean endwith(string suffix)//用来比较当前字符串的起始字符或子字符串prefix和终止字符或子字符串suffix是否和当前字符串相同,重载方法中同时还可以指定比较的开始位置offset。
string str = "abcdef";boolean a = str.statwith("ab");//a = trueboolean b = str.endwith("ef");//b = true
3)contains(string str)//判断参数s是否被包含在字符串中,并返回一个布尔类型的值。
string str = "abcdef";str.contains("ab");//truestr.contains("gh");//false
4)string[] split(string str)//将str作为分隔符进行字符串分解,分解后的字字符串在字符串数组中返回。
string str = "abc def ghi";string[] str1 = str.split(" ");//str1[0] = "abc";str1[1] = "def";str1[2] = "ghi";
10.类型转换
字符串转基本类型
java.lang包中有byte、short、integer、float、double类的调用方法:
public static byte parsebyte(string s)public static short parseshort(string s)public static short parseint(string s)public static long parselong(string s)public static float parsefloat(string s)public static double parsedouble(string s)int n = integer.parseint("12");float f = float.parsefloat("12.34");double d = double.parsedouble("1.124");
基本类型转字符串
string类中提供了string valueof()放法,用作基本类型转换为字符串类型
static string valueof(char data[])static string valueof(char data[], int offset, int count)static string valueof(boolean b)static string valueof(char c)static string valueof(int i)static string valueof(long l)static string valueof(float f)static string valueof(double d)//将char '8' 转换为int 8string str = string.valueof('8');int num = integer.parseint(str);
进制转换
使用long类中的方法得到整数之间的各种进制转换的方法:
long.tobinarystring(long l)//二进制long.tooctalstring(long l)//十进制long.tohexstring(long l)//十六进制long.tostring(long l, int p)//p作为任意进制
相关学习推荐:java基础教程
以上就是java string怎么使用?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product