1 25 26 package net.yagga.util; 27 28 import java.util.*; 29 32 33 public class StringManip { 34 35 static public void main(String [] argv){ 36 HashMap subst=new HashMap(); 37 subst.put("<NICK>","__NIC__"); 38 subst.put("<SERVER_DIR>","__SERVER_DIR__"); 40 subst.put("<VERSION_NAME>","__VER__"); 41 subst.put("<FILE_VERSION>","__VERNUM__"); 42 subst.put("<OS>","win" ); 43 44 47 String sr="aasd a da\\n sa\\ads asads\\tasda\\nas c:\\\\temp dsda"; 48 System.out.println(sr); 49 System.out.println(escape(sr,true)); 50 } 51 52 public StringManip() { 53 } 54 55 60 public static String makeSubsts(String orig, HashMap pairs){ 61 Iterator it=pairs.keySet().iterator(); 62 String ret=orig; 63 while(it.hasNext()){ 64 String search=(String )it.next(); 65 ret=subst(ret,search,(String )pairs.get(search),true); 66 } 67 return ret; 68 } 69 70 75 public static String subst(String orig, String search, String replace, boolean global) 76 { 77 int i=-1; 78 int len1=search.length(); 79 int len2=replace.length(); 80 81 String _subst=orig; 82 if(orig==null) 83 return null; 84 i=_subst.indexOf(search); 85 while(i>=0){ 87 _subst=_subst.substring(0,i)+replace+_subst.substring(i+len1,_subst.length()); 90 92 i=_subst.indexOf(search,i+len2); 93 if(!global) 95 break; 96 } 97 return _subst; 99 } 100 101 109 public static String escape(String orig, boolean unix){ 110 if(orig==null) 111 return null; 112 String str=orig; 113 StringBuffer sb=new StringBuffer (); 114 int len=str.length(); 115 int i=0, s=0; 116 while(s<len){ 117 i=str.indexOf("\\",s); 118 if(i!=-1){ 119 int ch=-1; 120 String esc=null; 121 if(i<len-1) 122 ch=str.charAt(i+1); 123 switch(ch){ 124 case 't': esc="\t"; break; 125 case 'r': 126 if(unix) 127 esc=""; 128 else 129 esc="\r"; 130 break; 131 case 'n': esc="\n"; break; 132 default: Character c=new Character ((char)ch); esc=c.toString(); 133 } 134 sb.append(str.substring(s,i)); 135 sb.append(esc); 136 s=i+2; 137 } 139 else{ sb.append(str.substring(s)); 141 break; 142 } 143 144 } 145 return sb.toString(); 147 } 148 149 } 150 | Popular Tags |