Java-字符串处理的整理

内容:(1)连接字符串(2)获取字符串信息(3)字符串操作(4)格式化字符串

    String类:形如“ABCD”,“你好”

    声明:String str=null;//String指定该变量为字符串类型,str标识符,表示字符串变量的名称,null未初始化

    创建:String str=new String(“good”)

或:String str=“good”;

  1.连接字符串:对已经声明的字符串进行连接

 方法:使用“+”运算符

 举例:

public class Test{	
	public static void main(String[] args){
		String a="hello";
		String b="world";
		String c=a+" "+b;
		System.out.println(c);
	}		
}
系统输出:hello world

  2.获取字符串信息:获取字符串长度、字符串查找、获取指定索引位置的字符、获取子字符串、字符串替换、判断字符串的开始与结尾、判断字符串是否相等、字母大小写转换、字符串分割

(1)获取字符串长度:a.length();

public class Test{	
	public static void main(String[] args){
		String a="Students";
		int size=a.length();
		System.out.println(size);//系统输出字符串a的长度
	}		
}

(2)字符串查找:a.indexOf(substr); substr为要搜索的字符串,语句返回int值为substr的位置

public class Test{	
	public static void main(String[] args){
		String a="Students";
		int size=a.indexOf("u");
	}		
}
size值为2

(3)获取指定索引位置的字符:a.charAt(int index); index整型值,指定要索引的位置

public class Test{	
	public static void main(String[] args){
		String a="Students";
		char b=a.charAt(5);
		System.out.println(b);
	}		
}
系统输出:n

(4)获取子字符串:

截取直至字符串结尾。a.substring(int beginIndex);包含起始位置

public class Test{	
	public static void main(String[] args){
		String a="Students";
		String b=a.substring(3);
		System.out.println(b);
	}		
}

系统输出:dents

截取指定起始位置,终止位置字符串:a.substring(int beginIndex,endIndex);包含起始位置不包含终止位置

public class Test{	
	public static void main(String[] args){
		String a="Students";
		String b=a.substring(0,3);
		System.out.println(b);
	}		
}
系统输出:Stu

(5)字符串替换:a.replace(char oldChar,char newChar);

public class Test{	
	public static void main(String[] args){
		String a="Students";
		String b=a.replace("t","l");
		System.out.println(b);
	}		
}
系统输出:Sludents

(6)判断字符串的开始与结尾:a.startsWith(String b);判断开头是否为字符串b,是返回true,不是返回false;

  a.endsWith(String b);判断结尾是否为字符串b,是返回true,不是返回false;

public class Test{	
	public static void main(String[] args){
		String a="Students";
		boolean b=a.startsWith("St");
		System.out.println(b);
		boolean c=a.endsWith("c");
		System.out.println(c);
	}		
}
系统输出:true

    false

(7)判断字符串是否相等:a.equals(String b);区分大小写进行比较,返回boolean值

  a.equalsIgnoreCase(String b);不区分大小写进行比较,返回boolean值

public class Test{	
	public static void main(String[] args){
		String a="Students";
		String b="students";
		boolean c=a.equals(b);
		boolean d=a.equalsIgnoreCase(b);	
		System.out.println(c);
		System.out.println(d);
	}		
}
系统输出:false

    true

(8)字母大小写转换:a.toLowerCase();转为小写

  a.toUpperCase();转为大写

public class Test{	
	public static void main(String[] args){
		String a="Students";
		String b="students";
		String c=a.toLowerCase();
		String d=b.toUpperCase();	
		System.out.println(c);
		System.out.println(d);
	}		
}
系统输出:students
                   STUDENTS

(9)字符串分割:a.split(String sign);根据指定符号sign进行分割

  a.split(String sign,int limit);根据指定符号sign进行分割,并限定拆分次数

public class Test{	
	public static void main(String[] args){
		String a="a,b,c,d";
		String b="s,t,u,d,e,n,t,s";
		String c[]=a.split(",");
		String d[]=b.split(",",5);	
		for(int i = 0;i<4;i++){
			System.out.println(c[i]);
		}
		for(int i = 0;i<6;i++){
			System.out.println(d[i]);
		}
		
	}		
}
系统输出:






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我了解了您的要求。关于您的问题,字符串和时间格式的转换是编程中常见的操作,各种编程语言都提供了相应的函数和库来进行处理。以下是一些常见的语言中实现字符串和时间格式转换的函数和方法: 1. Python 字符串转时间格式: ```python import datetime str_time = '2022-01-01 12:00:00' time_obj = datetime.datetime.strptime(str_time, '%Y-%m-%d %H:%M:%S') ``` 时间格式转字符串: ```python time_obj = datetime.datetime.now() str_time = time_obj.strftime('%Y-%m-%d %H:%M:%S') ``` 2. Java 字符串转时间格式: ```java import java.text.SimpleDateFormat; import java.util.Date; String str_time = "2022-01-01 12:00:00"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date time_obj = formatter.parse(str_time); ``` 时间格式转字符串: ```java Date time_obj = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String str_time = formatter.format(time_obj); ``` 3. C++ 字符串转时间格式: ```cpp #include <iostream> #include <ctime> #include <string> std::string str_time = "2022-01-01 12:00:00"; struct tm time_obj; strptime(str_time.c_str(), "%Y-%m-%d %H:%M:%S", &time_obj); time_t timestamp = mktime(&time_obj); ``` 时间格式转字符串: ```cpp time_t timestamp = time(NULL); struct tm time_obj = *localtime(&timestamp); char str_time[20]; strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &time_obj); std::string result(str_time); ``` 以上是常见的几种语言中实现字符串和时间格式转换的方式,具体的实现方法可能会有所不同,但大致思路是相似的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值