截取字符串替换成星号

时间:2021-09-13 14:47:41
 1 public class TestDemo8 {  2 
 3     final static String STARS = "*******************************";  4 
 5     public static void main(String[] args) {  6 
 7         String s = "(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.217)(PORT = 1521))";  8           int beginIndex = s.indexOf("H");  9           int endIndex = s.lastIndexOf("7"); 10           String news = s.substring(beginIndex, endIndex+1); 11           
12           System.out.println("s..." + s); 13           System.out.println("beginIndex..." + beginIndex); 14           System.out.println("endIndex..." + endIndex); 15           System.out.println("news..." + news); 16          
18         System.out.println("getEmail..."+getEmail("liudehua@126.com"));    
20  } 21     
22     public static String getEmail(String email){ 23 if(email == null) 24 ;// error 25 String[] emailParts = email.split("@"); 26 if(emailParts.length != 2) 27 ;// 格式错误 28 int len = emailParts[0].length(); 29 String char1 = emailParts[0].substring(0, 1); 30 String char2 = emailParts[0].substring(len-1, len); 31 // String char3 = STARS.substring(0, len - 2); // 截取中间字符串位数 32 String char3 = "***"; 33 34 System.out.println("1..." + emailParts[0]); // liudehua 35 System.out.println("2..." + emailParts[1]); // 126.com 36 System.out.println("char1..." + char1); // l 37 System.out.println("char2..." + char2); // a 38 39 return char1+char3+char2+"@"+emailParts[1]; 40  } 41 
42 }


输出结果如下:

s...(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.217)(PORT = 1521))
beginIndex...28
endIndex...47
news...HOST = 192.168.1.217
1...liudehua
2...126.com
char1...l
char2...a
getEmail...l***a@126.com

 

 

Java中常用的字符串的截取经验

http://jingyan.baidu.com/article/a17d5285224c918098c8f215.html