1. java中如何查找字符串中的'\'
可以用正则表达式.
如果要表示java源码中的正则表达式的一个正常的'\'字符,则需要这样表示'\\\\',其中第一第三个'\'均表示java编译器中的转义字符第二个则表示正则表达式中的转义字符,从而把第四个'\'转义为正则表达式中一个普通的'\'字符
2. java 如何查找匹配的字符和字符串
你可以自己写个方法的!
从返回的第一个位置开始substring,同时记住位置。
public int[] getOffset(String str,String s){
int[] arr=new int[str.length];
int j=1;
while(str.indexOf(s)!=-1){
int i=str.indexOf(s);
if(j==1){
arr[j-1]=i;
}else{
arr[j-1]=i+arr[j-2]+1;
}
String st=str.substring(i+1);
System.out.println(st);
str=st;
j++;
System.out.println("j="+j);
}
return arr;
}
public static void main(String[] args) {
String str="abcaabbddab";
StringText st=new StringText();
int[] i=st.getOffset(str, "ab");
for(int j:i){
System.out.println(j);
}
}
3. 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 - 开始搜索的索引位置。
返回:
指定子字符串在此字符串中最后一次出现处的索引。
4. java如何实现在一个字符串中查找另一个字符串
要判断boy是不是后者中的一部分,不用循环,只要用String类的indexOf函数就行了。
代码如下:
public
class
HH
{
public
static
void
main(String[]
args)
{
String
s="he
is
a
boy";
int
result=s.indexOf("boy");
if(result>=0){
System.out.println("boy是he
is
a
boy的一部分");
}else{
System.out.println("boy不是he
is
a
boy的一部分");
}
}
}
运行结果:
boy是he
is
a
boy的一部分
5. 简单的java字符串中查找字符问题
代码如下:
package demo.joshua;
public class MyFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
filterStr("我喜欢Java程序设计");
filterStr("汉语(Chinese)是我们的母语");
}
public static void filterStr(String str) {
Integer sum = 0;
StringBuffer sb = new StringBuffer();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < 171948 && str.charAt(i) > 19968) {
sb.append(str.charAt(i));
sum++;
}
}
System.out.println("字符串中包含的汉字:" + sb.toString());
System.out.println("汉字的个数:" + sum);
}
}
6. java截取指定字符串中的某段字符如何实现
如下图,给你贴出了代码段。可以利用字符串的substring函数来进行截取。
结果是:456789(注意:包括4。)
示例:
"hamburger".substring(3,8) returns "burge"
"smiles".substring(0,5) returns "smile"
7. java怎么查找字符串第一个字符
通过indexOf进行查找
示例:
String str = "abcdefg";
if(str.indexOf("cd")>=0){//这里查找str中是否存在"cd"字符串,如果存在则会返回大于等于0的数,如果不存在,则返回-1
System.out.println("找到了");
}
补充indexOf
1、返回 String 对象内第一次出现子字符串的字符位置。
2、string.indexOf(subString[, startIndex])
1)参数
string
必选项。String 对象或文字。
subString 必选项。
要在 String 对象中查找的子字符串。
starIndex 可选项。
该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。
2)说明
indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。
8. java怎么获得字符串中某一字符的位置
在java中使用indexOf方法即可获得字符串中某一字符的位置,例如Stringstr="abcdef",System.out.println(str.indexOf("c"))。