public class Main { public static void main(String[] args) { String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i = 15; int j = 16; boolean b1=str.contains(String.valueOf(i)); boolean b2=str.contains(String.valueOf(j)); System.out.println(b1); System.out.println(b2); } }
public class Main { public static void main(String[] args) { String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i = 15; int j = 16; boolean b=str.contains(String.valueOf(i))&&!str.contains(String.valueOf(j)); System.out.println(b); } }
public static void main(String[] args) { String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i = 15; int j = 16; boolean b1=str.contains(i+""); boolean b2=str.contains(j+""); System.out.println(b1); System.out.println(b2); }
楼上的答案都有遗漏,假设这个里面字符串里面是 str="1,12" 我要找是否包含2,,楼上的全部都要找出来,所以嘛,呵呵。这样解决 public static void main(String[] args) { String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; String temp=","+str+","; int i = 15; int j = 16; boolean b1=str.contains(","+i+","); boolean b2=str.contains(","+j+","); System.out.println(b1); System.out.println(b2); }
public static void main(String[] args) { String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; String temp=","+str+","; int i = 15; int j = 16; boolean b1=temp.contains(","+i+","); boolean b2=temp.contains(","+j+","); System.out.println(b1); System.out.println(b2); }
import java.util.ArrayList;public class Main{ public static void main(String[] args) { String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i=15; int j=16; String strs[]=str.split(","); ArrayList<Integer> list=new ArrayList<Integer>(); for (int k = 0; k < strs.length; k++) { list.add(Integer.valueOf(strs[k])); } boolean b=list.contains(i)&&!list.contains(j); System.out.println(b); } }
public class Ss { public static void main(String args[]){ String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i = 15; int j = 16;
int index=str.indexOf(String.valueOf(i)); int index2=str.indexOf(String.valueOf(j)); System.out.println(index+" "+index2); }} index 为-1时则不包含,不为-1时则包含
public static void main(String[] args) { String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; String temp=","+str+","; int i = 15; int j = 16; boolean b1=str.contains(","+i+","); boolean b2=str.contains(","+j+","); System.out.println(b1); System.out.println(b2); }
String num = "1,2,3,4,16"; int[] intArr = {1, 6}; for(int i = 0; i < intArr.length; i++){ StringTokenizer st1 = new StringTokenizer(num, ","); while (st1.hasMoreElements()) { if (st1.nextElement().equals(new String(new Integer(intArr[i]).toString()))) { System.out.println("true"); break; } } if(!st1.hasMoreElements()){ System.out.println("false"); } }
public void compare(){ char[] c = str.toChar(); int length = c.length; int i = 0; int a = 15; int b = 16; for( int i = 0; i < length; i++ ){ switch( (int)char[i] ){ case a: i = 1; break; case b: i = 2; break; } } if( i == 1 ){ 只有a; }else if( i == 2 ){ 2个都有 }else { 2个都没有 } }
上面a,b不是赋值15,16,而是先对应char的转成int的值。
indexOf(String.valueOf(i))=-1 如果是数组..就不一样了
上面的做法都对,但是效率太低,你们用的contains这个本身要创建2个对象然后用equals比较,效率肯定低。正确的做法是 public class FindNumber{ public static void main(String[] args) { String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i = 15; int j = 16; String[] strValue = str.split(","); for(int k=0;k<strValue.length;k++) { if(Integer.parseInt(strValue[k]) == i) { System.out.println("Y"); return; } } System.out.println("N"); } }
最简单的最有效率的,不用说了,正则表达式.这个题目正则表达式匹配如下: String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i = 15; int j = 16; Pattern p = Pattern.compile("[15]*"); Matcher m = p.matches(str); if(m.find()) { 查找到进行相关处理 } else { 查找不到做相关处理。 }
public class Test { public static void main(String[] args) { String str = "1,2,3,4,5,690,7"; Test test = new Test(); System.out.println(test.check(str,690)); } public boolean check(String str, int arg) { String[] arr = str.split(","); for (String a : arr) { if (a.equals(String.valueOf(arg))) { return true; } } return false; }}
public static void main(String[] args){ String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i = 15; int j = 16; for(String s : str.split(",")){ if(s.equals(i+"")){ if(!s.equals(j+"")){ System.out.println("ok"); } } } }
[Quote=引用 26 楼 xiao5122122455 的回复:] 引用 25 楼 lgg201 的回复: 最简单的最有效率的,不用说了,正则表达式. 这个题目正则表达式匹配如下: Java codeString str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35"; int i = 15; int j = 16; Pattern p = Pattern.compile("[15]*"); Matcher m = p.matches(str); if(m.find()) { 查找到进行相关处理 } else { 查找不到做相关处理。 } 如果str里有个151呢?那不是也匹配? 这种除了把str用split成数组,其它的办法都不怎么合适
楼主【yhtorange】截止到2008-07-30 16:38:06的历史汇总数据(不包括此帖):
发帖的总数量:19 发帖的总分数:460 每贴平均分数:24
回帖的总数量:32 得分贴总数量:8 回帖的得分率:25%
结贴的总数量:19 结贴的总分数:460
无满意结贴数:6 无满意结贴分:80
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:31.58 % 无满意结分率:17.39 %
敬礼!
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=yhtorange
public class Main { public static void main(String[] args) {
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i = 15;
int j = 16;
boolean b1=str.contains(String.valueOf(i));
boolean b2=str.contains(String.valueOf(j));
System.out.println(b1);
System.out.println(b2);
}
}
public class Main { public static void main(String[] args) {
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i = 15;
int j = 16;
boolean b=str.contains(String.valueOf(i))&&!str.contains(String.valueOf(j));
System.out.println(b);
}
}
public static void main(String[] args) {
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i = 15;
int j = 16;
boolean b1=str.contains(i+"");
boolean b2=str.contains(j+"");
System.out.println(b1);
System.out.println(b2);
}
楼上的答案都有遗漏,假设这个里面字符串里面是
str="1,12"
我要找是否包含2,,楼上的全部都要找出来,所以嘛,呵呵。这样解决 public static void main(String[] args) {
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
String temp=","+str+",";
int i = 15;
int j = 16;
boolean b1=str.contains(","+i+",");
boolean b2=str.contains(","+j+",");
System.out.println(b1);
System.out.println(b2);
}
public static boolean contains(String str, int i){
String[] gs = str.split(",");
for(int k=0; k<gs.length; k++){
if(String.valueOf(i).equalsIgnoreCase(gs[k])){
return true;
}
}
return false;
}
public class ContainNumber {
public boolean isContainNumber(String str, int num) {
if(str.startsWith(",")) str = ","+str;
if(str.endsWith(",")) str += ",";
/*****************************************************
* 以上2步保证最终的字符串是",1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35,"
* 然后就能用indexOf(",15,") 这样就能唯一了.
* 如果没有开始的逗号的话那么"115," indexOf(",15,")返回的不是-1
*/
if(str.indexOf(""+num)!=-1) return true;
return false;
} public static void main(String[] args) {
ContainNumber number = new ContainNumber();
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
System.out.println(number.isContainNumber(str, 15));
System.out.println(number.isContainNumber(str, 16));
}
}
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
String temp=","+str+",";
int i = 15;
int j = 16;
boolean b1=temp.contains(","+i+",");
boolean b2=temp.contains(","+j+",");
System.out.println(b1);
System.out.println(b2);
}
public static void main(String[] args) {
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i=15;
int j=16;
String strs[]=str.split(",");
ArrayList<Integer> list=new ArrayList<Integer>();
for (int k = 0; k < strs.length; k++) {
list.add(Integer.valueOf(strs[k]));
}
boolean b=list.contains(i)&&!list.contains(j);
System.out.println(b);
}
}
public static void main(String args[]){
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i = 15;
int j = 16;
int index=str.indexOf(String.valueOf(i));
int index2=str.indexOf(String.valueOf(j));
System.out.println(index+" "+index2);
}}
index 为-1时则不包含,不为-1时则包含
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
String temp=","+str+",";
int i = 15;
int j = 16;
boolean b1=str.contains(","+i+",");
boolean b2=str.contains(","+j+",");
System.out.println(b1);
System.out.println(b2);
}
int[] intArr = {1, 6};
for(int i = 0; i < intArr.length; i++){
StringTokenizer st1 = new StringTokenizer(num, ",");
while (st1.hasMoreElements()) {
if (st1.nextElement().equals(new String(new Integer(intArr[i]).toString()))) {
System.out.println("true");
break;
}
}
if(!st1.hasMoreElements()){
System.out.println("false");
}
}
char[] c = str.toChar();
int length = c.length;
int i = 0;
int a = 15;
int b = 16;
for( int i = 0; i < length; i++ ){
switch( (int)char[i] ){
case a:
i = 1;
break;
case b:
i = 2;
break;
}
}
if( i == 1 ){
只有a;
}else if( i == 2 ){
2个都有
}else {
2个都没有
}
}
如果是数组..就不一样了
public class FindNumber{
public static void main(String[] args)
{
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i = 15;
int j = 16;
String[] strValue = str.split(",");
for(int k=0;k<strValue.length;k++)
{
if(Integer.parseInt(strValue[k]) == i)
{
System.out.println("Y");
return;
}
}
System.out.println("N");
}
}
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i = 15;
int j = 16; Pattern p = Pattern.compile("[15]*");
Matcher m = p.matches(str);
if(m.find()) {
查找到进行相关处理
} else {
查找不到做相关处理。
}
public class Test
{
public static void main(String[] args)
{
String str = "1,2,3,4,5,690,7";
Test test = new Test();
System.out.println(test.check(str,690)); } public boolean check(String str, int arg)
{
String[] arr = str.split(",");
for (String a : arr)
{
if (a.equals(String.valueOf(arg)))
{
return true;
}
}
return false;
}}
String str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i = 15;
int j = 16;
for(String s : str.split(",")){
if(s.equals(i+"")){
if(!s.equals(j+"")){
System.out.println("ok");
}
}
}
}
引用 25 楼 lgg201 的回复:
最简单的最有效率的,不用说了,正则表达式. 这个题目正则表达式匹配如下: Java codeString str = "1,2,3,4,5,6,7,8,11,14,15,18,22,24,27,30,33,35";
int i = 15;
int j = 16; Pattern p = Pattern.compile("[15]*");
Matcher m = p.matches(str);
if(m.find()) {
查找到进行相关处理
} else {
查找不到做相关处理。
}
如果str里有个151呢?那不是也匹配?
这种除了把str用split成数组,其它的办法都不怎么合适
这是你前提知道 String 里有 “,”。假如不知道呢 或者没有
那怎么 使用 split
比如 String str=“abd12ds13ed”;让你判断 字符串里是否有数字
则表示a包含b