字符串1

本文通过一个Java程序演示了字符串的各种操作,包括比较、匹配、连接等,并展示了如何使用equalsIgnoreCase和regionMatches等方法进行不区分大小写的比较及部分字符串的匹配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import javax.swing.*;

public class StringTest2 {
    
public static void main(String args[]){
        String s1,s2,s3,s4,output;
        
        s1
=new String ("good morning");
        s2
=new String ("John");
        s3
=new String("How are you");
        s4
=new String("   how are you");
        
        output
="s1="+s1+"\ns2="+s2+"\ns3="+s3+"\ns4"+s4+"\n\n";
        
        
if(s1.equals("hello"))
    output
+="s1 equals\"hello\"\n";
        
else
        output
+="s1 dose not equal\"hello\"\n";
        
        
if (s3.equalsIgnoreCase(s4))
        output
+="s3 equals s4\n";
        
else
        output
+="s3 dose not equal s4\n";
        
        
if(s3.regionMatches(0,s4,0,5))
        output
+="First 5 characters of s3 and s4 match\n";
        
else
        output
+="First 5 characters of s3 and s4 do not match\n";
        
            
if(s3.regionMatches(true,0,s4,0,5))
        output
+="First 5 characters of s3 and s4 match\n";
        
else
        output
+="First 5 characters of s3 and s4 do not match\n";
    
        output
+="s1与s4连接起来是:"+s1.concat(s4)+"\n";
        
        output
+="the first\"day\"of s4 is :"+s4.indexOf("you")+"\n";
        output
+="the last\"day\"of s4 is :"+s4.lastIndexOf("we");
        
        JOptionPane.showMessageDialog(
null,output,"Demonstring String Class CONstructors",JOptionPane.INFORMATION_MESSAGE);
        System.exit(
0);
    }

    
}

//p133
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值