字符串的常见用法

//把一个字符串变为一个字符数组
class Haha
{
	public static void main(String args[])
	{
		String name =  "Hello";
		char c[] = name.toCharArray();
		for(int i = 0; i < c.length;i++)
			{
				System.out.println(c[i]);
			}
	}
}


(2)把字符数组变为字符串

//把一个字符串变为一个字符数组
class Haha
{
	public static void main(String args[])
	{
		char c[] = {'h','e','l','l','o'};
		String name = new String(c);
		String name_1 = new String(c,0,3);		//0代表从角标为0开始,显示角标为0,1,2的3个字符
		System.out.println("显示全部的字符:"+name);
		System.out.println("显示从0开始的3个字符:"+name_1);
	}
}


(3)从字符串取出指定位置的字符

class Haha
{
	public static void main(String args[])
	{
		String name = "hello";
		System.out.println("取出角标为2的字符:"+name.charAt(2));
	}
}


(4)字符串与byte数组的转换

class Haha
{
	public static void main(String args[])
	{
		String name = "hello";
		byte b[] = name.getBytes();						//将字符串变为byte数组
		System.out.println(new String(b));				//将字符串数组变为String类型
		System.out.println(new String(b,0,3));		//将角标为0开始的3个字符串变为String类型
	}
}

(5)计算字符串的长度
//在这里,很多人会把计算字符串的长度与计算数组长度的方法混淆
//计算数组的长度是:数组.length
//计算字符串的长度是:字符串.length()
class Haha
{
	public static void main(String args[])
	{
		String name = "hello";
		System.out.println(name+"的长度是:"+name.length());
	}
}

(6)查找字符串是否存在指定内容
//indexOf返回的是一个int类型的数据,表示它在哪个位置
//若没有查找到,则返回-1
class Haha
{
	public static void main(String args[])
	{
		String name = "hellohello";
		System.out.println(name.indexOf("o",0));		//从角标为0开始查找,但只会查找第一个o,后面的o不会查找
		System.out.println(name.indexOf("o",5));		//只有过了第一个o,才会查找下一个o
		System.out.println(name.indexOf("a"));			//没有查找到,返回-1
	}
}

(7)字符串去掉空格
//trim()只能去掉字符串左右的空格,并不能去掉字符串内的空格
class Haha
{
	public static void main(String args[])
	{
		String name = "           h  e  l  l  o  h  e  l  l  o       ";
		System.out.println("去掉左右空格之后的字符串:"+name.trim());		//输出的是h  e  l  l  o  h  e  l  l  o
	}
}

(8)字符截取
//trim()只能去掉字符串左右的空格,并不能去掉字符串内的空格
class Haha
{
	public static void main(String args[])
	{
		String name = " hello";
		System.out.println("从角标为2的字符开始截取"+name.substring(2));				//输出:ello
		System.out.println("截取从角标0到第5个字符前(不包括第5个,即o)的字符:"+name.substring(0,5));	//输出:hell
	}
}

(9)分割字符串
class Haha
{
	public static void main(String args[])
	{
		String name = " h e l l o ";
		String s[] = name.split("e");
		for(int i = 0; i < s.length;i++)
			{
				System.out.println(s[i]);
			}
	}
}
//输出:h 
//      e l l o

(10)大小写转换
class Haha
{
	public static void main(String args[])
	{
		String name = "hello";
		String name_1 = "HELLO";
		System.out.println("将\"hello\"转换为大写"+name.toUpperCase());
		System.out.println("将\"HELLO\"转换为小写"+name_1.toLowerCase());
	}
}

(11)是否以指定的字符串开头或者结尾
//判断是否以指定字符串结尾或者开头,返回的是true或者false
class Haha
{
	public static void main(String args[])
	{
		String name = "hello";
		System.out.println("判断\"hello\"是否以he开头:"+name.startsWith("he"));
		System.out.println("判断\"hello\"是否以he结尾:"+name.endsWith("he"));
	}
}

(12)不区分大小写的比较,equals是区分大小写的
class Haha
{
	public static void main(String args[])
	{
		String name = "hello";
		String name_1 = "HELLO";
		System.out.println(name+"与"+name_1+"的比较结果:"+name.equalsIgnoreCase(name_1));
	}
}
//输出结果:true




字符串函数在编程中用于处理和操作字符串数据,不同的编程语言提供了各自的字符串函数库。以下是一些常见编程语言中的字符串函数使用方法及示例: ### C语言中的字符串函数 C语言标准库 `<string.h>` 提供了一系列用于操作字符串的函数。 - **`strncpy(dest, src, n)`**:将 `src` 中最多 `n` 个字符复制到 `dest`。 ```c #include <stdio.h> #include <string.h> int main() { char src[40] = "i am src hh."; char dest[12]; memset(dest, '\0', sizeof(dest)); strncpy(dest, src, 10); printf("%s\n", dest); // 输出前10字符 return 0; } ``` 此函数返回最终复制的字符串 [^1]。 - **`strcmp(str1, str2)`**:比较两个字符串 `str1` 和 `str2`。 - 如果 `str1` 等于 `str2`,则返回 0。 - 如果 `str1` 小于 `str2`,则返回 -1。 - 如果 `str1` 大于 `str2`,则返回 1。 ```c #include <stdio.h> #include <string.h> int main() { char str1[] = "hello"; char str2[] = "world"; int result = strcmp(str1, str2); if (result == 0) { printf("Strings are equal.\n"); } else if (result < 0) { printf("str1 is less than str2.\n"); } else { printf("str1 is greater than str2.\n"); } return 0; } ``` ### Python中的字符串函数 Python 提供了丰富的内置字符串方法来处理字符串。 - **`casefold()`**:将字符串转换为小写形式。 ```python string = "优快云 is the largest developer community in China" print(string.casefold()) # 输出全部小写的字符串 [^2] ``` - **`lower()`**:将字符串中的所有大写字母转换为小写。 ```python text = "Hello World!" print(text.lower()) # 输出 "hello world!" ``` - **`upper()`**:将字符串中的所有小写字母转换为大写。 ```python text = "hello world!" print(text.upper()) # 输出 "HELLO WORLD!" ``` - **`strip()`**:去除字符串两端的空白字符(包括空格、换行符等)。 ```python text = " Hello World! " print(text.strip()) # 输出 "Hello World!" ``` - **`split(separator)`**:按照指定分隔符分割字符串。 ```python text = "apple,banana,orange" fruits = text.split(',') for fruit in fruits: print(fruit) ``` ### MySQL中的字符串函数 MySQL 数据库提供了一些字符串函数来进行数据库查询时的数据处理。 - **`STRCMP(str1, str2)`**:比较两个字符串并返回结果。 - 如果 `str1` 等于 `str2`,则返回 0。 - 如果 `str1` 小于 `str2`,则返回 -1。 - 如果 `str1` 大于 `str2`,则返回 1。 - 如果任意一个参数为 NULL,则返回 NULL。 ```sql SELECT STRCMP('hello', 'hello'); -- 返回 0 SELECT STRCMP('hello', 'world'); -- 返回 -1 SELECT STRCMP('world', 'hello'); -- 返回 1 ``` - **`LOWER(str)`**:将字符串 `str` 转换为小写形式。 ```sql SELECT LOWER('HELLO WORLD'); -- 返回 'hello world' ``` - **`UPPER(str)`**:将字符串 `str` 转换为大写形式。 ```sql SELECT UPPER('hello world'); -- 返回 'HELLO WORLD' ``` 这些是几种常用编程语言中的一些基本字符串函数及其用法。每种语言都有其特定的库或模块来支持更复杂的字符串操作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值