独家面经总结,超级精彩
本人面试腾讯,阿里,百度等企业总结下来的面试经历,都是真实的,分享给大家!
Java面试准备
准确的说这里又分为两部分:
- Java刷题
- 算法刷题
Java刷题:此份文档详细记录了千道面试题与详解;
System.out.println(isNumberChar(str));
}
public static void main(String[] args) {
String str = “1d4567”;
System.out.println(isNumberChar(str));
}
🎈字节与字符串
字节常用于数据传输以及编码转换的处理之中,String 也能方便的和 byte[] 相互转换
NO | 方法名称 | 类型 | 描述 |
1 | public String(byte bytes[]) | 构造 | 将字节数组变为字符串 |
2 | public String(byte bytes[],int offset,int length) | 构造 | 将部分字节数组中的内容变为字符串 |
3 | public bye[] getBytes() | 普通 | 将字符串以字节数组的形式返回 |
4 | public byte[] getBytes(String charsetNAme)throws UnsupportedEncodingException | 普通 | 编码转化处理 |
代码示例: 实现字符串与字节数组的转换处理
public static void main(String[] args) {
String str = “helloworld” ;
// String 转 byte[]
byte[] data = str.getBytes() ;
for (int i = 0; i < data.length; i++) {
System.out.print(data[i]+" ");
}
System.out.println();
// byte[] 转 String
System.out.println(new String(data));
}
public static void main(String[] args) {
byte[] bytes = {97,98,99,100};
String str = new String(bytes,1,3);
System.out.println(str);
}
🎈小结
byte[] 是把 String 按照一个字节一个字节的方式处理, 这种适合在网络传输, 数据存储这样的场景下使用. 更适合 针对二进制数据来操作.
char[] 是吧 String 按照一个字符一个字符的方式处理, 更适合针对文本数据来操作, 尤其是包含中文的时候.
✨字符串常见操作
============
🎈字符串比较
No | 方法名称 | 类型 | 描述 |
1 | public boolean equals(Object anObject) | 普通 | 区分大小的比较 |
2 | public boolean equalsIanorecase(String anotherString) | 普通 | 不区分大小写的比较 |
3 | public int compareTo(String anotherString) | 普通 | 比较两个字符串大小关系 |
代码示例: 不区分大小写比较
public static void main(String[] args) {
String str1 = “hello” ;
String str2 = “Hello” ;
System.out.println(str1.equals(str2)); // false
System.out.println(str1.equalsIgnoreCase(str2)); // true
}
在String类中compareTo()方法是一个非常重要的方法,该方法返回一个整型,该数据会根据大小关系返回三类内容:
1. 相等:返回0.
2. 小于:返回内容小于0.
3. 大于:返回内容大于0。
public static void main(String[] args) {
System.out.println(“A”.compareTo(“a”)); // -32
System.out.println(“a”.compareTo(“A”)); // 32
System.out.println(“A”.compareTo(“A”)); // 0
System.out.println(“AB”.compareTo(“AC”)); // -1
System.out.println(“刘”.compareTo(“杨”));
}
compareTo()是一个可以区分大小关系的方法,是String方法里是一个非常重要的方法。
字符串的比较大小规则, 总结成三个字 “字典序” 相当于判定两个字符串在一本词典的前面还是后面. 先比较第一 个字符的大小(根据 unicode 的值来判定), 如果不分胜负, 就依次比较后面的内容
🎈字符串查找
从一个完整的字符串之中可以判断指定内容是否存在,对于查找方法有如下定义:
NO | 方法名称 | 类型 | 描述 |
1 | public boolean contains(CharSequence s) | 普通 | 判断一个子字符串是否存在 |
2 | public int indexOf(String str) | 普通 | 从头开始查找指定字符串的位置,查到了返回位置的开始索引,如果查不到返回-1 |
3 | public int indexOf(String str,int fromIndex) | 普通 | 从指定位置查找子字符串位置 |
4 | public int LastIndexOf(String str) | 普通 | 从后向前查找子字符串位置 |
5 | public int LastIndexOf(String str, int fromIdex) | 普通 | 从指定位置由后向前查找 |
6 | public boolean startWith (String prefix) | 普通 | 判断是否以指定字符串开头 |
7 | public boolean startWith(String prefix, int toffset) | 普通 | 从指定位置开始判断是否以指定字符串开头 |
8 | public boolean endWith(String suffix) | 普通 | 判断是否以指定字符串结尾 |
代码示例: 字符串查找,最好用最方便的就是contains()
public static void main(String[] args) {
String str = “helloworld” ;
System.out.println(str.contains(“world”));
System.out.println(str.contains(“forld”));
}
代码示例: 使用indexOf()方法进行位置查找
public static void main(String[] args) {
String str = “helloworld” ;
System.out.println(str.indexOf(“world”)); // 5,w开始的索引
System.out.println(str.indexOf(“bit”)); // -1,没有查到
if (str.indexOf(“hello”) != -1) {
System.out.println(“可以查到指定字符串!”);
}
}
代码示例: 使用indexOf()的注意点
public static void main(String[] args) {
String str = “helloworld” ;
System.out.println(str.indexOf(“l”)); // 2
System.out.println(str.indexOf(“l”,5)); // 8
System.out.println(str.lastIndexOf(“l”)); // 8
}
代码示例: 判断开头或结尾
public static void main(String[] args) {
String str = “**@@helloworld!!” ;
System.out.println(str.startsWith(“**”)); // true
System.out.println(str.startsWith(“@@”,2)); // ture
System.out.println(str.endsWith(“!!”)); // true
}
🎈字符串替换
使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下
No | 方法名称 | 类型 | 描述 |
1 | public String replaceAll(String regex,String replacement) | 普通 | 替换所有指定的内容 |
2 | public String replaceFirst(String regex, String replacement) | 普通 | 替换首个内容 |
代码示例: 字符串的替换处理
public static void main(String[] args) {
String str = “helloworld” ;
System.out.println(str.replaceAll(“l”, “_”));
System.out.println(str.replaceFirst(“l”, “_”));
}
注意事项: 由于字符串是不可变对象 , 替换不修改当前字符串, 而是产生一个新的字符串
字符串拆分
可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。
NO | 方法名称 | 类型 | 描述 |
1 | public String[] split(String regex) | 普通 | 将字符串全部拆分 |
2 | public String[] split(String regex,int limit) | 普通 | 将字符串部分拆分 |
代码示例: 实现字符串的拆分处理
public static void main(String[] args) {
String str = “hello world hello yu” ;
String[] result = str.split(" ") ; // 按照空格拆分
for(String s: result) {
System.out.println(s);
}
}
代码示例: 字符串的部分拆分
public static void main(String[] args) {
String str = “hello world hello yu” ;
String[] result = str.split(" ",2) ;
for(String s: result) {
System.out.println(s);
}
}
代码示例: 拆分IP地址
public static void main(String[] args) {
String str = “192.168.1.1” ;
String[] result = str.split(“\.”) ;
for(String s: result) {
System.out.println(s);
}
}
注意事项:
1. 字符"|“,”*“,”+“都得加上转义字符,前面加上”\".
2. 而如果是"“,那么就得写成”\\".
3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符
代码示例: 多次拆分
public static void main(String[] args) {
String str = “name=zhangsan&age=18” ;
String[] result = str.split(“&”) ;
for (int i = 0; i < result.length; i++) {
String[] temp = result[i].split(“=”) ;
System.out.println(temp[0]+" = "+temp[1]);
}
}
🎈字符串截取
从一个完整的字符串之中截取出部分内容。可用方法如下:
NO | 方法名称 | 类型 | 描述 |
1 | public String substring(int beginIndex) | 普通 | 从指定索引截取到结尾 |
2 | public String substring(int beginIndex, int endIndex) | 普通 | 截取部分内容 |
代码示例: 观察字符串截取
public static void main(String[] args) {
String str = “helloworld” ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));
}
最后
我还为大家准备了一套体系化的架构师学习资料包以及BAT面试资料,供大家参考及学习
已经将知识体系整理好(源码,笔记,PPT,学习视频)
id main(String[] args) {
String str = “helloworld” ;
System.out.println(str.substring(5));
System.out.println(str.substring(0, 5));
}
最后
我还为大家准备了一套体系化的架构师学习资料包以及BAT面试资料,供大家参考及学习
已经将知识体系整理好(源码,笔记,PPT,学习视频)
[外链图片转存中…(img-fDiPcCcp-1715352949412)]
[外链图片转存中…(img-0KfzYouJ-1715352949412)]
[外链图片转存中…(img-TDgCYets-1715352949413)]