字符串基础

  1 package zifuchuan;
  2 
  3 import java.util.Date;
  4 
  5 public class dsd {
  6 
  7     public static void main(String[] args) {
  8     
  9         //字符串
 10         
 11         String str = "aa 醉了";   //字符串双引号       常用
 12         
 13            char c = 'c';             //字符单引号
 14                 
 15          int j =1;
 16          
 17          String str1 = new String ("a c bc bc f");   //少用
 18          
 19          //连接字符 串    +
 20          
 21          System.out.println("str+str1+j=" +str+str1+j);  
 22          
 23          //99乘法表 
 24          
 25          int []a= new int [5];
 26          
 27          int l = a. length;  // 不带括号是属性
 28          
 29         int g =str1.length();    //带括号是方法
 30         
 31         System.out.println("str1.length()=" +g);
 32         
 33         //查找
 34         int sy =str1.indexOf("bc");
 35         
 36         System.out.println("str1.indexOf("bc") =" +sy);
 37         
 38         int sy1 =str1.lastIndexOf("bc");
 39         
 40         System.out.println("str1.indexOf("bc") =" +sy1);
 41         
 42         //取出字符
 43         
 44       System.out.println("charAt=" + str1.charAt(2));
 45       
 46      //截取字符串
 47       
 48       System.out.println("substring= " +str1.substring(3));
 49       System.out.println("substring= " +str1.substring(3,5));  //索引值5结束
 50       
 51       //字符串的去除空格
 52       
 53       String str3 = "  abc def gh  ";
 54       
 55       System.out.println("str3.trim()=#" + str3.trim() + "#");//去空格
 56       
 57       
 58       str3 = str3.trim();//保存去空格
 59  
 60       System.out.println("str3.trim()=#" + str3 + "#");
 61       
 62       //替换
 63       
 64       str3 = str3.replace(" ","");
 65       
 66       System.out.println("str3.replace()=#" + str3 + "#");
 67       
 68       str3=str3.replace("bc","BC");
 69       
 70       System.out.println("str3.replace()=#" + str3 + "#");
 71       
 72       //判断字符串开始
 73       
 74       System.out.println("str3.startsWith() =" +str3.startsWith("a"));
 75       System.out.println("str3.endsWith() =" +str3.endsWith("a"));
 76       
 77       String a1 = new String ("aaa");
 78       String a2 = new String ("Aaa");
 79       System.out.println("a1==a2="+a1==a2);        //==比较的是内存地址即a1与a2,不是各自包含的字符串
 80       
 81       a1.equals(a2);
 82       
 83       System.out.println("a1.equals(a2)=" +a1.equals(a2));// 区分大小写
 84       System.out.println("a1.equalsIgnoreCase(a2)=" +a1.equalsIgnoreCase(a2));//不区分大小写
 85       
 86       String b1 = "aaa";
 87       String b2 = "aaa";
 88       System.out.println("b1==b2="+(b1==b2));   //不建议使用
 89 
 90       //按字典顺序比较  Unicode的值
 91       
 92       System.out.println("compareTo=" +a1.compareTo(a2));//从第一位开始比较
 93       
 94       //大小写转换
 95       
 96       System.out.println("a1.toUpperCase=" +a1.toUpperCase());//转换大写
 97       System.out.println("a1.toLowerCase=" +a1.toLowerCase());//转换小写
 98       
 99       //字符串分割   
100       
101       String fg = "aa;bb;cc,ccc;ddd.ss";
102       
103       String [] arr= fg.split(";");
104       String [] arr2 = fg.split(";|,");
105       String [] arr1 = fg.split(";" , 2 );
106       
107       for (String f :arr)
108       {
109           System.out.println(f);
110           
111       }
112       
113       //格式化字符串  日期时间
114       // String.format(格式符,要被格式化的对象 );
115       
116       Date dt =new Date();    //实例化日期,生成当前日期
117       
118       System.out.println(String.format("%tF",dt));
119       
120       System.out.println(String.format("%tT",dt));
121       
122       System.out.println(String.format("%tR",dt));
123       
124       System.out.println(String.format("%tF",dt)+" "+String.format("%tT",dt));
125       
126       System.out.println("200的十六进制="+String.format("%x",200));
127       
128       System.out.println("特定换行"+ String.format("%n")+"换行");
129       
130       System.out.println("特定换行"+ "
"+"换行");
131       
132       StringBuilder strb = new  StringBuilder();
133       
134       strb.append("abc");
135       strb.append("def");
136       strb.append("ghr");
137       
138       strb.insert(2,"$$");
139       strb.delete(2, 5);
140       System.out.println("strb = " + strb.toString());
141       
142       
143       
144       
145       
146       
147       
148     
149       
150       
151       
152         
153     
154          
155 
156          
157          
158     }
159 
160 }
原文地址:https://www.cnblogs.com/ymf123/p/4869819.html