在Java中查找具有连续字符的字符串 [英] Finding strings with consecutive characters in Java
本文介绍了在Java中查找具有连续字符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中编写一个函数,它接受一个字符串数组,并从字符串数组中返回那些连续重复特定字母的字符串,例如:如果I / P是
{Dauresselam,slab,fuss,boolean,clap}
那么O / P应该是
{Dauresselam, fuss,boolean}
我可以使用
解决它
import java.util.Scanner;
public class doubleChars {
public static String [] getDoubles(String [] In)
{
int inLen = In.length;
String zoom [] = new String [inLen];
int count = 0;
if(inLen == 0)
{
return zoom;
}
for(int i = 0; i< = inLen-1; i ++)
{
String A = In [i];
//System.out.println(A);
int striLen = A.length();
for(int j = 0; j< striLen-1; j ++)
{
if(A.substring(j,j + 1).equals(A.substring (j + 1,j + 2)))
{
zoom [count] = A;
count ++;
休息;
}
}
}
返回缩放;
}
public static void main(String [] args)
{
char more ='y';
int ab = 0;
String [] res = {};
String [] fillMe = {durres,murres,,abcdeee,boolean,nger,lagger};
Scanner strobe = new Scanner(System.in);
System.out.println(请输入字符串的数组);
/ * while(strobe.hasNext())
{
fillMe [ab] = strobe.next();
ab ++;
}
* /
res = doubleChars.getDoubles(fillMe);
for(int k = 0; k< res.length; k ++)
{
if(res [k] == null)
{
break;
}
System.out.println(res [k]);
}
}
}
IS 有没有办法使用正则表达式来缩短它?
解决方案
你可以用一个
Java示例:
String [] strings = {Dauresselam,slab ,fuss,boolean,clap};
String regex =([a-z])\\1;
模式模式= Pattern.compile(正则表达式);
for(String string:strings){
Matcher matcher = pattern.matcher(string);
if(matcher.find()){
System.out.println(string);
}
}
打印:
Dauresselam
fuss
boolean
Write a function in Java which takes an Array of strings and from the array of strings returns only those strings which have a consecutive repetition of a particular letter for eg: if I/P is
{"Dauresselam", "slab", "fuss", "boolean", "clap"}
then O/P should be
{"Dauresselam", "fuss", "boolean"}
I could solve it using
import java.util.Scanner;
public class doubleChars {
public static String[] getDoubles(String[]In)
{
int inLen=In.length;
String zoom[]=new String[inLen];
int count=0;
if(inLen==0)
{
return zoom;
}
for(int i=0;i<=inLen-1;i++)
{
String A=In[i];
//System.out.println(A);
int striLen=A.length();
for(int j=0;j<striLen-1;j++)
{
if(A.substring(j, j+1).equals(A.substring(j+1, j+2)))
{
zoom[count]=A;
count++;
break;
}
}
}
return zoom;
}
public static void main(String[] args)
{
char more='y';
int ab=0;
String[] res={};
String[] fillMe={"durres", "murres", "", "abcdeee", "boolean", "nger", "lagger"};
Scanner strobe=new Scanner(System.in);
System.out.println("Please enter the arraye of the string");
/*while(strobe.hasNext())
{
fillMe[ab]=strobe.next();
ab++;
}
*/
res=doubleChars.getDoubles(fillMe);
for(int k=0;k<res.length;k++)
{
if(res[k]==null)
{
break;
}
System.out.println(res[k]);
}
}
}
IS there a way to use regex to make it shorter?
解决方案
You could use a backreference:
([a-z])\1
Visualization by Debuggex
Java example:
String[] strings = { "Dauresselam", "slab", "fuss", "boolean", "clap" };
String regex = "([a-z])\\1";
Pattern pattern = Pattern.compile(regex);
for (String string : strings) {
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println(string);
}
}
Prints:
Dauresselam
fuss
boolean
这篇关于在Java中查找具有连续字符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文