美文网首页
java工具整理---持续更新

java工具整理---持续更新

作者: 提笔忘字_波 | 来源:发表于2019-05-05 10:51 被阅读0次

String类:

  1. 用字符数组value创建一个String对象
char[] value ={"a","b","c","d"};
String str = new String(value);
//相当于String str = newString("abcd")
  1. 用字符数组以x开始的n个字符创建一个String对象
char[] value ={"a","b","c","d"};
String str = new String(value, 1, 2);
  1. 获取字符串长度
String str = new String("478bhjd56");
int strlength = str.length();
  1. 获取字符串某一位置的字符
String str = new String("43dfzyd");
char ch = str.charAt(4);//ch = z

5.获取字符串的子串

public String substring(int beginIndex)
//该方法从beginIndex位置起,
//从当前字符串中取出剩余的字符作为一个新的字符串返回。
public String substring(int beginIndex, intendIndex)
//该方法从beginIndex位置起,从当前字符串中
//取出到endIndex-1位置的字符作为一个新的字符串返回

6.字符串的比较

public int compareTo(String str)
//该方法是对字符串内容按字典顺序进行大小比较,
//通过返回的整数值指明当前字符串与参数字符串的大小关系。
//若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。
public int compareToIgnoreCase (String str)
//与compareTo方法相似,但忽略大小写。
public boolean equals(Object obj)
//比较当前字符串和参数字符串,在两个字符串相等的时候返回true,否则返回false。
public boolean equalsIgnoreCase(String str)
//与equals方法相似,但忽略大小写。

7、查找子串在字符串中的位置

public int indexOf(String str)
//用于查找当前字符串中字符或子串,返回字符或
//子串在当前字符串中从左边起首次出现的位置,若没有出现则返回-1。
public int indexOf(String str, intfromIndex)
//改方法与第一种类似,区别在于该方法从fromIndex位置向后查找。
public int lastIndexOf(String str)
//该方法与第一种类似,区别在于该方法从字符串的末尾位置向前查找。
public int lastIndexOf(String str, intfromIndex)
//该方法与第二种方法类似,区别于该方法从fromIndex位置向前查找。

8.字符串中字符的大小写转换

public String toLowerCase()
//返回将当前字符串中所有字符转换成小写后的新串
public String toUpperCase()
//返回将当前字符串中所有字符转换成大写后的新串

9.字符串两端去空格

String trim()
//去除字符串两端的空格,中间的空格不变,一般用于登陆注册时

10.将字符串分割成字符串数组

String[] split(String str)

11.基本类型转换为字符串

static String valueOf(xxx xx)

String s1 = String.valueOf(12.99);

12.替换字符串

public String replace(char oldChar, charnewChar)
//用字符newChar替换当前字符串中所有的oldChar字符,
//并返回一个新的字符串。

public String replaceFirst(String regex,String replacement)
//该方法用字符replacement的内容替换当前字符串中遇到的
//第一个和字符串regex相匹配的子串,应将新的字符串返回。

public String replaceAll(String regex,String replacement)
//该方法用字符replacement的内容替换当前字符串中遇到的所有
//和字符串regex相匹配的子串,应将新的字符串返回。

https://blog.csdn.net/qq_25406669/article/details/79021911


Array类方法:

1、将数组转化为集合(set,list)

(1)转化为list

String s2[] = {"wyy", "wzz", "wxx"};
 /*2.将数组转化为ArrayList  Arrays.asList(数组)   */
List<String> list = new ArrayList<>(Arrays.asList(s2));
list.add("whh");
System.out.println("ArrayList是否包含:" + list.contains("whh"));

(2)转化为set

Set<String> set = new HashSet<>(Arrays.asList(s2));
System.out.println("集合set是否包含:" + set.contains("wyy"));

2、数组是否包含某个元素

Arrays.toString(s2).contains   转化为字符串,然后用contain方法

3、转换int值为字节数组

byte[] bytes = ByteBuffer.allocate(4).putInt(90).array();
for (byte t : bytes) {
     System.out.format("0x%x ", t);  //0x0 0x0 0x0 0x5a
}

4、如何查看数组是否包含某个元素

方法1:Arrays.toString(s2).contains   转化为字符串,然后用contain方法
方法2:先用2,转化为集合,然后用contain方法
方法3:自循环
public static boolean Loop(String[] arr, String targetValue) {
        for(String s: arr){
            if(s.equals(targetValue))
                return true;
        }
        return false;
    }
方法4:Arrays.binarySearch()  ,只能用于有序数组,当数组存储数据很多时推荐此方法。
public static boolean binarySearch(String[] arr, String targetValue) {
        int j = Arrays.binarySearch(arr, targetValue);
        if (j > 0) {
            return true;
        } else
            return false;
    }

https://www.cnblogs.com/alwayswyy/p/6424509.html


相关文章

网友评论

      本文标题:java工具整理---持续更新

      本文链接:https://www.haomeiwen.com/subject/rcrcoqtx.html