Java如何获取一个字符串中子串的位置
//你描述的不是很清楚 请问是一下这个意思吗?
private static final String CONVERT=”ABCDEFGHIJKLMN”;
public static void main(String[] args) {
test(“A”);
}
public static void test(String a){
for (int i = 0; i CONVERT.length(); i++) {
//判断当前字符串中的单个位置
if (a.equals(String.valueOf(CONVERT.charAt(i)))) {
//当前字符串
System.out.println(CONVERT.charAt(i));
//获取当前字符串位置
System.out.println(i+1);
}
}
}
JAVA中怎样在一个字符串中查找给定的子字符串
调用类java.lang.String
的以下方法都可以:
public int indexOf(String str)
返回指定子字符串在此字符串中第一次出现处的索引。
参数:
str – 任意字符串。
返回:
如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的第一个字符的索引;如果它不作为一个子字符串出现,则返回 -1。
public int indexOf(String str,int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
参数:
str – 要搜索的子字符串。
fromIndex – 开始搜索的索引位置。
返回:
指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
public int lastIndexOf(String str)
返回指定子字符串在此字符串中最右边出现处的索引。将最右边的空字符串 “” 视为出现在索引值 this.length() 处。
参数:
str – 要搜索的子字符串。
返回:
如果字符串参数作为一个子字符串在此对象中出现一次或多次,则返回最后一个这种子字符串的第一个字符。如果它不作为一个子字符串出现,则返回 -1。
public int lastIndexOf(String str,int fromIndex)
返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
参数:
str – 要搜索的子字符串。
fromIndex – 开始搜索的索引位置。
返回:
指定子字符串在此字符串中最后一次出现处的索引。
java中怎么判断一个字符串中包含某个字符或字符串
方法如下:
一、contains方法
1:描述
java.lang.String.contains() 方法返回true,当且仅当此字符串包含指定的char值序列
2:声明
public boolean contains(CharSequence s)
3:返回值
此方法返回true,如果此字符串包含,否则返回false。
4:实例
public static void main(String[] args) {
String str = “abc”;
boolean status = str.contains(“a”);
if(status){
System.out.println(“包含”);
}else{
System.out.println(“不包含”);
}
}
二、indexOf方法
1:描述
java.lang.String.indexOf() 的用途是在一个字符串中寻找一个字的位置,同时也可以判断一个字符串中是否包含某个字符。
2:声明
int indexOf(int ch,int fromIndex)
3:返回值
indexOf的返回值为int
4:实例
public static void main(String[] args) {
String str1 = “abcdefg”;
int result1 = str1.indexOf(“a”);
if(result1 != -1){
System.out.println(“字符串str中包含子串“a””+result1);
}else{
System.out.println(“字符串str中不包含子串“a””+result1);
}
}
JAVA中如何判断一个字符串是否为另一个字符串的子串
indexOf(String s)的使用,如果包含,返回的值是包含该子字符串在父类字符串中起始位置;如果不包含必定全部返回值为-1。
代码如下:
package my_automation;
public class z_test {
public static void main(String[] args) {
String test = “This is test for string”;
System.out.println(test.indexOf(“This”)); //0
System.out.println(test.indexOf(“is”)); //2
System.out.println(test.indexOf(“test”)); //8
System.out.println(test.indexOf(“for”)); //13
System.out.println(test.indexOf(“for string “));//-1
if (test.indexOf(“This”)!=-1){
//”只要test.indexOf(‘This’)返回的值不是-1说明test字符串中包含字符串’This’,相反如果包含返回的值必定是-1″
System.out.println(“存在包含关系,因为返回的值不等于-1”);
}else{
System.out.println(“不存在包含关系,因为返回的值等于-1”);
}
if (test.indexOf(“this”)!=-1){
//”只要test.indexOf(‘this’)返回的值不是-1说明test字符串中包含字符串’this’,相反如果包含返回的值必定是-1″
System.out.println(“存在包含关系,因为返回的值不等于-1”);
}else{
System.out.println(“不存在包含关系,因为返回的值等于-1”);
}
}
扩展资料
java string中indexOf()常用用法
Java中字符串中子串的查找共有四种方法,如下:
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。
2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。
3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。
4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
public class Test {
public static void main(String[] args) {
String s = “xXccxxxXX”;
// 从头开始查找是否存在指定的字符 //结果如下
System.out.println(s.indexOf(“c”)); //2
// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(s.indexOf(“c”, 3)); //3
//若指定字符串中没有该字符则系统返回-1
System.out.println(s.indexOf(“y”)); //-1
System.out.println(s.lastIndexOf(“x”)); //6
System.out.println(s.indexOf(120,2)); //4
System.out.println(s.indexOf(‘x’,2)); //4 120是x的ASCII编码
}
}