2013-11-25 19 views
3

我正在用Java开发一个程序。我需要在字符串中找到特定的文本。假设字符串是'夏威夷'。我的需求是搜索'd'&'e'。如果'e'先出现'e'必须返回,如果'd'先出现则'd'必须返回。在字符串'Hawaiie'中,因为'e'是第一个字母'e'必须被返回。我尝试了下面的代码,但它只返回字母的位置,而不是字母本身。Java第一次找到一个特定的字母

String str1 = new String("Hawaiee"); 
System.out.println(str1.indexOf('e'); 
+1

看看String类。你会找到你的答案。 http://docs.oracle.com/javase/7/docs/api/java/lang/String.html –

+0

为什么不创建自己的方法将字符串作为参数并返回该字母? –

回答

2

试试这个代码:

String str = new String("Hawaede"); 
    int a=str.indexOf('d'); 
    int b=str.indexOf('e'); 
    if(a!=-1 && a<b) 
    { 
     return 'd' 
    } 
    else if(b!=-1) return 'e'; 
2

我需要的是寻找 'd' & 'E'。如果'e'首先'e'必须返回 ,如果'd'先来,那么'd'必须返回。

尝试使用indexOf搜索出现在单词上的字母。

public static void main(String[] args) { 
    System.out.println(getFirstChar("Hawaiee")); 
} 

public static char getFirstChar(String str) { 

    int indexOfd = str.indexOf('d'); 
    int indexOfe = str.indexOf('e'); 

    if (indexOfd != -1 && indexOfe != -1) { 

     return indexOfd < indexOfe ? 'd' : 'e'; 
    } else if (indexOfd != -1) { 
     return 'd'; 
    } else if (indexOfe != -1) { 
     return 'e'; 
    } else { 
     return 'o'; 
    } 

} 
+0

这不会确定哪一个是字符串中的第一个 –

+0

雅...感谢Massud为你付出的努力..我需要得到e或d中的第一个字母..假设在字符串中'e'先来'e'有退回,如果'd'先到'd'必须退回。 – roshanpeter

+0

@roshanpeter,现在试试,我已经更新了我的答案。 – Masudul

3

好像你需要得到每个字符的索引(d & e)并比较索引。然后根据哪个索引更少返回适当的字符(不要忘记排除-1,因为这意味着它没有找到)。

5

试试这个:在这里,如果没有字符存在:它会返回'o',你可以放任何默认字符进行比较。

public char getFirstChar(String str) { 

    int indexOfd = str.indexOf('d'); 
    int indexOfe = str.indexOf('e'); 
    if (str.indexOf('d') != -1) { 
     if (str.indexOf('e') != -1) { 
      if (indexOfd < indexOfe) { 
       return 'd'; 
      } else { 
       return 'e'; 
      } 
     } else { 
      return 'd'; 
     } 

    } else { 
     if (str.indexOf('e') != -1) { 
       return 'e'; 
     } else { 
      return 'o'; 
     } 

    } 
} 
+0

这将返回字符串“e”的“o”。第一条if语句中的 –

+0

: “e”。indexOf('d')== -1 ,所以我们去else分支并返回'o' –

+0

@Meagain是的你说得对,谢谢,编辑 – Jhanvi

2

您将得到数值指标,然后通过比较,你可以决定是流行的“e”或“d”使用字符的charAt(INT指数)功能。代码明智它看起来如下:

String s="Hdiey"; 
int eIndex = s.indexOf("e"); 
int dIndex = s.indexOf("d"); 
System.out.println("eIndex "+eIndex+" dindex"+dIndex); 
if(eIndex < dIndex) 
    System.out.println(s.charAt(eIndex)); 
else 
    System.out.println(s.charAt(dIndex)); 
} 
3
public char getFirstChar(String s, char a, char b){ 
    for (int i = 0; i < s.length(); i++){ 
     char c = s.charAt(i); 

     if (c == a){ 
      return a; 
     } 
     if (c == b){ 
      return b; 
     } 
    } 
    return ' '; 
} 
2

尝试。

String str1 = new String("Hawaiee"); 

    if (str1.contains("e") && str1.contains("d") && str1.indexOf("e") < str1.indexOf("d")) { 
     System.out.println("e"); 
    } else if (str1.contains("e") && str1.contains("d") && str1.indexOf("d") <str1.indexOf("e")) { 
     System.out.println("d"); 
    } else if (str1.contains("e")) { 
     System.out.println("e"); 
    } else if (str1.contains("d")) { 
     System.out.println("d"); 
    } else { 
     System.out.println("String does not contain 'e' or 'd' "); 
    } 
3

试试这个:

public char myIndex(String str, char a, char b) { 
    for (char c : str.toCharArray()) { 
     if (a == c)return a; 
     else if (b == c)return b; 
    } 
    return '\0'; 
} 
3

你可以尝试分裂您的字符串,然后遍历它来定位你的角色。你会发现无论哪一个先获取返回

public class SO{ 
public static void main(String[] args){ 
    String str1 = new String("Hawaiee"); 
    String[] sArray = str1.split(""); //Split to array 
    String s = ""; 

    for(int i = 0; i < sArray.length; i++){ 
     s = sArray[i]; //Check each character 
     if(s.equals("e") || s.equals("d")){ //If we get a match 
      System.out.println(s + " appears first."); //Print 
      break; //Break loop as we have the first character 
     } else { 
      if(i == sArray.length -1){ //If at the ned without a match 
       System.out.println("No matches found"); 
      } 
     } 
    } 
} 
} 
2

与模式匹配:

Pattern p = Pattern.compile("[ed]"); 
Matcher m = p.matcher("Hawaiee"); 
if (m.find()) { 
    System.out.printf("Start index: %d%n End index: %d%n Found character: %s%n", m.start(), m.end(), m.group()); 
} 
相关问题