嗨,我只是使用 stringbuilder 来获取字符串的某些部分,使用 substring(int start, int end).. 但我遇到了这个错误
08-17 20:27:40.737: E/AndroidRuntime(15313): Caused by: java.lang.StringIndexOutOfBoundsException: length=3; regionStart=0; regionLength=4
08-17 20:27:40.737: E/AndroidRuntime(15313): at java.lang.AbstractStringBuilder.startEndAndLength(AbstractStringBuilder.java:216)
08-17 20:27:40.737: E/AndroidRuntime(15313): at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:625)
08-17 20:27:40.737: E/AndroidRuntime(15313): at java.lang.StringBuilder.substring(StringBuilder.java:44)
08-17 20:27:40.737: E/AndroidRuntime(15313): at com.example.john.Contact.getAllCallLogs(Contact.java:196)
08-17 20:27:40.737: E/AndroidRuntime(15313): at com.example.john.Contact.onCreate(Contact.java:49)
08-17 20:27:40.737: E/AndroidRuntime(15313): at android.app.Activity.performCreate(Activity.java:4479)
08-17 20:27:40.737: E/AndroidRuntime(15313): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
08-17 20:27:40.737: E/AndroidRuntime(15313): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
这就是我简单实现的方式:
StringBuilder str = new StringBuilder(phoneNumber);
String fourth = str.substring(0, 4);
我只想获取我的 phoneNumber 字符串的前 4 个字符