1.根据字节数截取字符串(注:汉字不能截取半个)
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
Scanner str1=new Scanner(System.in);
String res=str1.nextLine();
Scanner str2=new Scanner(System.in);
int count=str2.nextInt();
int len=0;
StringBuilder builder=new StringBuilder();
for(int i=0;i<res.length();i++){
len=len+String.valueOf(res.charAt(i)).getBytes().length;
if(len<=count){
builder.append(res.charAt(i));
}else{
break;
}
}
System.out.println(builder);
}
}
2.截取字符串中的部分字符串(两种实现方法)
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test6 {
public static void main(String[] args) {
Scanner str1=new Scanner(System.in);
String res1=str1.nextLine();
Scanner str2=new Scanner(System.in);
String res2=str2.nextLine();
fun1(res1,res2);
Test6 test6=new Test6();
test6.fun2(res1,res2);
}
public static void fun1(String res1,String res2){
int index=res1.indexOf(res2);
String result=res1.substring(index,index+res2.length());
System.out.println(result);
}
public void fun2(String res1,String res2){
String reg=".*("+res2+").*";
Pattern pattern=Pattern.compile(reg);
Matcher matcher=pattern.matcher(res1);
if(matcher.find()) {
System.out.println(matcher.group(1));
}
}
}
3.两种方法:字符串直接转换和字符串数组的首尾互换
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
Scanner str=new Scanner(System.in);
String res=str.nextLine();
StringBuilder result=new StringBuilder();
for(int i=res.length()-1;i>=0;i--){
result.append(res.charAt(i));
}
System.out.println(result);
fun1(res);
}
public static void fun1(String res){
char[] result=res.toCharArray();
for(int i=0;i< result.length/2;i++){
char temp;
temp=result[i];
result[i]=result[result.length-i-1];
result[result.length-i-1]=temp;
}
System.out.println(new String(result));
}
}
4.根据输入的分隔符对字符串进行分割
import java.util.Scanner;
public class Test8 {
public static void main(String[] args) {
Scanner str1=new Scanner(System.in);
String res1=str1.nextLine();
Scanner str2=new Scanner(System.in);
String res2=str2.nextLine();
String[] res=res1.split(res2);
StringBuffer result=new StringBuffer();
for(int i= res.length-1;i>=0;i--){
result.append(res[i]);
}
result.substring(0, result.length()-res2.length());
System.out.println(result);
}
}
5.将字符串中的每个词反转输出(相对位置保持不变)
import java.util.Scanner;
public class Test9 {
public static void main(String[] args) {
Scanner str1=new Scanner(System.in);
String res1=str1.nextLine();
String[] res=res1.split(" ");
StringBuffer result=new StringBuffer();
for(int i=0;i< res.length;i++){
for(int j=res[i].length()-1;j>=0;j--){
result.append(res[i].charAt(j));
}
result.append(" ");
}
result.substring(0,res1.length()-1);
System.out.println(result);
}
}
6.判断输入的IPV4地址是否合法
import java.util.Scanner;
public class Test10 {
public static void main(String[] args) {
Scanner str=new Scanner(System.in);
String ip=str.nextLine();
System.out.println(checkIp(ip));
}
public static boolean checkIp(String ip){
String[] res=ip.split("\\.");
if(res.length!=4){
return false;
}
for(String str:res){
try {
Integer num=Integer.parseInt(str);
if(num>255||num<0){
return false;
}
if(!str.equals("0")&&str.startsWith("0")){
return false;
}
} catch (NumberFormatException e) {
return false;
}
}
return true;
}
}