当前位置:首页 » 编程语言 » java字符串中查找字符

java字符串中查找字符

发布时间: 2022-04-23 02:49:11

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的一部分

⑵ 简单的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);
}

}

⑶ java 怎么获得字符串中某一字符的位置

在java中使用indexOf方法即可获得字符串中某一字符的位置,例如Stringstr="abcdef",System.out.println(str.indexOf("c"))。

⑷ java中如何能查询出字符串中某个字母的位置

str为你要测试的字符串第一种方法:byte[]temp=str.getbytes();//使用平台默认的字符集将此string解码为字节序列,并将结果存储到一个新的字节数组中。intcount=0;//遍历数组的每一个元素,也就是字符串中的每一个字母for(inti=0;i<temp.length;i++){//如果字母等于cif(temp[i].equals('c')){//计数器加一count++;}}第二种:intcount=0;stringstr=//你要测试的字符串//index为字符串中第一次出现c的位置,如果字符串中没有c将返回-1intindex=str.indexof(c);//如果字符串中有cwhile(str.indexof(c)!=-1){count++;//将字符串出现c的位置之前的全部截取掉str=str.substring(str.indexof(c));}考虑大小写:str=str.tolowercase();//将字符串全部转化成小写

⑸ JAVA中如何查找字符串

问题很简单:
1.首先,你的数据源是数组,那么要想对每一个值作操作,必须遍历,所以就有如下代码:

for(int i=0;i<A.length;i++){
...
}

2.在循环当中,数组中的每一个正在遍历的元素,就是:A[i];

3.String是java的一个字符串处理类,他有一个非常好用的方法就是,
public boolean startsWith(String prefix),测试此字符串是否以指定的前缀开始。 所以:A[i].startsWith("C")如果返回true,那么他就是以C开头的。

4.综上所述,实现很简单,完成代码如下:
public class Test{
public static void main(String[] args){
String[] A ={"CSDF","FDS","CFDSA","DFS","FET"};
for(int i=0;i<A.length;i++){
if(A[i].startsWith("C")){
System.out.println(A[i]);
}
}
}
}

总结:
临时写的,代码没有经过测试,但敢保证其正确性的几率很大,祝你成功。

⑹ java中如何查找字符串中的'\'

可以用正则表达式.
如果要表示java源码中的正则表达式的一个正常的'\'字符,则需要这样表示'\\\\',其中第一第三个'\'均表示java编译器中的转义字符第二个则表示正则表达式中的转义字符,从而把第四个'\'转义为正则表达式中一个普通的'\'字符

⑺ 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 如何查找匹配的字符和字符串

你可以自己写个方法的!
从返回的第一个位置开始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);
}
}

⑼ java编程字符串查找

importjava.util.*;
publicclassTester{
publicstaticvoidmain(String[]args){
Scannersc=newScanner(System.in);
while(true){
Stringpass=sc.nextLine();
if(pass.matches("^.{0,5}$")){
System.err.println("NOPasswordatleast6digits");
}elseif(pass.matches("^.*[^a-zA-Z\d].*$")){
System.err.println("");
}elseif(pass.matches("^[a-zA-Z]{6,}$")){
System.err.println("NOPasswordisatleastonenumber");
}else{
System.out.println("YES");
break;
}
}
sc.close();
}
}

热点内容
linux中进入ip地址服务器 发布:2025-05-18 04:11:21 浏览:606
java用什么软件写 发布:2025-05-18 03:56:19 浏览:27
linux配置vim编译c 发布:2025-05-18 03:55:07 浏览:99
砸百鬼脚本 发布:2025-05-18 03:53:34 浏览:934
安卓手机如何拍视频和苹果一样 发布:2025-05-18 03:40:47 浏览:727
为什么安卓手机连不上苹果7热点 发布:2025-05-18 03:40:13 浏览:797
网卡访问 发布:2025-05-18 03:35:04 浏览:504
接收和发送服务器地址 发布:2025-05-18 03:33:48 浏览:366
ef数据库查询数据 发布:2025-05-18 03:29:36 浏览:668
百度云下载文件夹 发布:2025-05-18 03:17:33 浏览:674