android 之细说字符串(String)的各种用法和TextUtils 用法及如何校验手机号合法性
2016-12-17 09:57
501 查看
总结一下字符串的各种用法,方便以后的查阅。
场景:实现数据的移动
顺便总结一下校验:手机号,密码,邮箱,邮编合法性的方法。
第二种方法:
java-正则表达式判断 邮编
中国邮政编码为6位数字,第一位不为0
java-正则表达式判断 Email邮箱 是否合法
函数 | 说明 |
---|---|
char charAt(int index) | 获取给定的index处的字符 |
int compareTo(String str) | 按照字典方式比较两个字符串 |
int compareToIgnore(String str) | 按照字典方式比较两个字符串,忽略大小写 |
String concat(String str) | 将给定的字符串连接到该字符串末尾 |
static String copyValueOf(char[] data) | 创建一个和给定字符数组相同的String对象 |
static String copyValueOf( char[] data,int offset,int count ) | 使用偏移量,创建一个和给定字符数组相同的String对象 |
boolean equals(Object obj) | 将String 对象和另一个String对象进行比较 |
boolean equalsIgnoreCase(String str) | 将String 对象和另一个String对象进行比较,忽略大小写 |
void getChars(int strbegin,int strend,char[] data,int offset) | 将字符串中的字符复制到字符数组中 |
int indexOf(int char) | 返回该字符串中第一次出现给定字符的索引位置 |
int indexOf(int char,int fromindex) | 返回从给定索引处开始,该字符串中第一次出现给定字符串的索引位置 |
int indexOf(String str) | 返回该字符串中第一次出现给定字符串的索引位置 |
int indexOf(String str,int fromindex) | 返回从给定索引处开始,该字符串第一次出现给定字符串的索引位置 |
int lastIndexOf() | 返回该字符串中最后一次出现给定字符的索引位置 |
int length() | 返回字符串的长度 |
booean regionMatches( boolen ignoreCase, int toffset, Stirng str, int ooffset, int len ) | 检查两个字符串区域是否相等,允许选择是否忽略大小写 |
String replace(char oldChar,char newChar) | 通过将字符串中的 oldChar 字符替换为 newChar 字符来构造一个新字符串 |
boolen starsWith(String prefix) | 检查字符串是否以给定前缀开头 |
boolen starsWith(String prefix,int offset) | 从给定索引处开始,检查字符串是否以给定前缀开头 |
String substring(int strbegin) | 将字符串中从给定索引处开始到末尾的子串构造一个新字符串 |
String substring(int strbegin,int strend) | 将字符串中从给定索引处开始到给定索引处结尾的子串构造一个新字符串 |
char[] toCharArray | 将字符串转换为数组 |
Stirng toLowerCase() | 返回将字符串中所有字符转变为小写的新字符串 |
Stirng toString | 返回字符串对象 |
String toUpperCase() | 返回字符串中所有字符转变为大写的新字符串 |
String trim() | 返回字符串去掉首尾空格后的新字符串 |
static Stirng valueOf(int i) | 将参数(不仅仅为int型)转换为字符串 |
boolean contains(charsequesce e) | 当且紧当此字符串包含指定的char值序列 。检查当前字符串中是否包含给定的字符串 |
调换集合中的数据的位置
public static void main(String[] args) { List<String> stringList = new ArrayList<>(); stringList.add("1"); stringList.add("2"); stringList.add("3"); stringList.add("4"); stringList.add("5"); //将2,5对调 int replaceNum1 = 1; int replaceNum2 = 4; stringList.add(replaceNum1, stringList.get(replaceNum2)); stringList.add(replaceNum2 + 1, stringList.get(replaceNum1 + 1)); stringList.remove(replaceNum1 + 1); stringList.remove(replaceNum2 + 1); for (int i = 0; i < stringList.size(); i++) { System.out.println("MyClass.main stringList[i]=" + stringList.get(i)); } }
场景:实现数据的移动
TextUtils 的简单用法
//字符串拼接 Log.d(TAG, TextUtils.concat("Hello", " ", "world!").toString()); //判断是否为空字符串 Log.d(TAG, TextUtils.isEmpty("Hello") + ""); //判断是否只有数字 Log.d(TAG, TextUtils.isDigitsOnly("Hello") + ""); //判断字符串是否相等 Log.d(TAG, TextUtils.equals("Hello", "Hello") + ""); //获取字符串的倒序 Log.d(TAG, TextUtils.getReverse("Hello", 0, "Hello".length()).toString()); //获取字符串的长度 Log.d(TAG, TextUtils.getTrimmedLength("Hello world!") + ""); Log.d(TAG, TextUtils.getTrimmedLength(" Hello world! ") + ""); //获取html格式的字符串 Log.d(TAG, TextUtils.htmlEncode("<html>\n" + "<body>\n" + "这是一个非常简单的HTML。\n" + "</body>\n" + "</html>")); //获取字符串中第一次出现子字符串的字符位置 Log.d(TAG, TextUtils.indexOf("Hello world!", "Hello") + ""); //截取字符串 Log.d(TAG, TextUtils.substring("Hello world!", 0, 5)); //通过表达式截取字符串 Log.d(TAG, TextUtils.split(" Hello world! ", " ")[0]); // 字符串是否含有可打印的字符 Log.e(TAG, TextUtils.isGraphic(" \n\t \b") + ""); List<String> list = new LinkedList<String>(); list.add("I"); list.add("love"); list.add("you"); Log.e(TAG, TextUtils.join("-", list));// 在数组中每个元素之间使用“-”来连接
顺便总结一下校验:手机号,密码,邮箱,邮编合法性的方法。
/** * 验证手机格式 */ public static boolean isMobile(String number) { /* 移动:134、135、136、137、138、139、150、151、157(TD)、158、 159、187、188 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通) 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9 */ String num = "[1][358]\\d{9}";//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。 if (TextUtils.isEmpty(number)) { return false; } else { //matches():字符串是否在给定的正则表达式匹配 return number.matches(num); } }
//移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 //联通:130、131、132、152、155、156、185、186 //电信:133、153、180、189、(1349卫通) public static boolean isMobileNO(String mobiles){ Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); }
第二种方法:
String value="手机号"; String regExp = "^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$"; Pattern p = Pattern.compile(regExp); Matcher m = p.matcher(value); return m.find();//boolean
java-正则表达式判断 邮编
中国邮政编码为6位数字,第一位不为0
String str = "^[1-9][0-9]{5}$"; /** * 判断邮编 * @param paramString * @return */ public static boolean isZipNO(String zipString){ String str = "^[1-9][0-9]{5}$"; return Pattern.compile(str).matcher(zipString).matches(); }
java-正则表达式判断 Email邮箱 是否合法
/** * 判断邮箱是否合法 * @param email * @return */ public static boolean isEmail(String email){ if (null==email || "".equals(email)) return false; //Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配 Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配 Matcher m = p.matcher(email); return m.matches(); }
/** * 验证手机号是否正确 * * @param mobiles * @return */ public static boolean isMobileNO(String mobiles) { Pattern p = Pattern.compile("^(13[0-9]|14[57]|15[0-35-9]|17[6-8]|18[0-9])[0-9]{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); } /** * 验证密码 * * @param pwd * @return */ public static final boolean isRightPwd(String pwd) { Pattern p = Pattern.compile("^(?![^a-zA-Z]+$)(?!\\D+$)[0-9a-zA-Z]{8,16}$"); Matcher m = p.matcher(pwd); return m.matches(); }
相关文章推荐
- 【我的Android进阶之旅】Android Studio如何轻松整理字符串到string.xml中
- Android中的string.xml里如何给字符串前后加空格
- javascript之string对象 以及字符串各种用法
- leetcode——8 String to Integer (atoi)(自定义字符串转整型,如何避开各种奇葩输入)
- 【总结】关于在Android中如何使用字符串数组String[]
- Android中资源文件中的字符串数组string-array简单用法
- [转的哦】 Android-字符串资源及其格式化Android 中的string.xml用法小结
- Android中资源文件中的字符串数组string-array简单用法
- Visual C++ 如何:在各种字符串类型之间进行转换
- 如何:在各种字符串类型之间进行转换
- Visual C++ 如何:在各种字符串类型之间进行转换
- eval()用法 vb的数据以字符串传递给JS,如何处理(1)
- DataFormatString属性用法(数据格式字符串)
- Visual C++ 如何:在各种字符串类型之间进行转换
- 如何将字符串序列化到CDATA块中(Serializing A String Within a CDATA Element )
- 如何:在各种字符串类型之间进行转换
- String的trim()方法如何去掉字符串中前/后面的空格
- 如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++)
- (原創) 如何将字符串前后的空白去除? (使用string.find_first_not_of, string.find_last_not_of) (C/C++)
- Visual C++ 如何:在各种字符串类型之间进行转换