2010-04-01 22 views

cevap

4

normal ifadeler kullanmam gerekir mi bunun için? Dize olur. lastIndexOf("/") dizini bulmak için çalışın ve sonuçta String.substring(int start, int end) kullanın? Yoksa gerçek verileriniz farklı ve daha karmaşıktır ve düzenli ifadeler gerektirir?

int lastSlash = mystring.lastIndexOf("/"); 
String start = mystring.substring(0, lastSlash); 
String end = mystring.substring(lastSlash + 1, mystring.length); 
9
/(?=[^/]*$) 

artık / s tarafından takip edilmeyen bir / eşleşir. İstediğin, sadece String'in lastIndexOf overkill kullanmalıdır bir karakter regex son örneğini bulmak için ise, üzerinde bölmek için,

String[] splitArray = subjectString.split("/(?=[^/]*$)"); 
+0

Bu kabul cevap olmalıdır. –

2

kullanmak

int pos = myString.lastIndexOf('/'); 
+2

+1 - Bu daha verimlidir. Öte yandan, son '/' doğrudan '.split()' ile beslenebilen bir ifade, muhtemelen daha okunaklı olan kişisel çözümdür (çözümümü justkt'lerle karşılaştır). –

3

çekirdek soru verdi örneği buna ihtiyacı yapar rağmen iyidir: Son/'üzerinde dize bölmek ne sağlanan sayesinde, burada kod. Java'nın indexOf düzenli ifadeleri almaz. Sorunun sadece konu bölümünü cevaplama, burada gerekir ne:

/** 
* Version of indexOf that uses regular expressions for the search 
* by Julian Cochran. 
*/ 
public static int indexOfRegex(String message, String toFind) { 
    // Need to add an extra character to message because to ensure 
    // split works if toFind is right at the end of the message. 
    message = message + " "; 
    String separated[] = message.split(toFind); 
    if (separated == null || 
     separated.length == 0 || 
     separated.length == 1) { 
    return -1; 
    } 
    return separated[0].length(); 
} 

son endeksi gerekiyorsa:

/** 
* Version of lastIndexOf that uses regular expressions for 
* the search by Julian Cochran. 
*/ 
public static int lastIndexOfRegex(String message, String toFind) { 
    // Need to add an extra character to message because to ensure 
    // split works if toFind is right at the end of the message. 
    message = message + " "; 
    String separated[] = message.split(toFind); 
    if (separated == null || 
     separated.length == 0 || 
     separated.length == 1) { 
    return -1; 
    } 
    return separated[separated.length - 1].length(); 
} 
+0

Bu benim için yararlı oldu. –

+0

@Julian Cochran. Kullanılmış bir kitap aldım ve isminiz var ve sizde, kızınız ve oğlunuzda bir resim var mı? Eğer sen, sana fotoğrafı çekmek istiyorum. Gürültü için özür dilerim SE duymak, ama başka bir yolu bulmak için ... – clg4

7

Ben standart String.lastIndexOf() yöntemini kullanarak olduğunu kabul En iyi hareket tarzı, ancak son zamanlarda Regex kısmı için kullanmıştım (yani, bir dizede son alfasayısal olmayan karakteri bulmak istedim). İlk giriş dizesi tersine çevirerek daha hızlı bu yöntemi yapmak mümkün olabilir, aynı zamanda

/** 
* Indicates that a String search operation yielded no results. 
*/ 
public static final int NOT_FOUND = -1; 

/** 
* Version of lastIndexOf that uses regular expressions for searching. 
* By Tomer Godinger. 
* 
* @param str String in which to search for the pattern. 
* @param toFind Pattern to locate. 
* @return The index of the requested pattern, if found; NOT_FOUND (-1) otherwise. 
*/ 
public static int lastIndexOfRegex(String str, String toFind) 
{ 
    Pattern pattern = Pattern.compile(toFind); 
    Matcher matcher = pattern.matcher(str); 

    // Default to the NOT_FOUND constant 
    int lastIndex = NOT_FOUND; 

    // Search for the given pattern 
    while (matcher.find()) 
    { 
     lastIndex = matcher.start(); 
    } 

    return lastIndex; 
} 

/** 
* Finds the last index of the given regular expression pattern in the given string, 
* starting from the given index (and conceptually going backwards). 
* By Tomer Godinger. 
* 
* @param str String in which to search for the pattern. 
* @param toFind Pattern to locate. 
* @param fromIndex Maximum allowed index. 
* @return The index of the requested pattern, if found; NOT_FOUND (-1) otherwise. 
*/ 
public static int lastIndexOfRegex(String str, String toFind, int fromIndex) 
{ 
    // Limit the search by searching on a suitable substring 
    return lastIndexOfRegex(str.substring(0, fromIndex), toFind); 
} 

:

Ben başkalarına yardım etmek hizmet verecek umuduyla, paylaşmaya kendim yazma ve düşünce sona erdi Daha sonra, ilk grubun bitiş indeksini alarak (tüm gruplara gitmekten ziyade).

Fakat bunu yapmak için deseni de tersine çevirmeniz gerekir; Bazı durumlarda basit olabilir (tek bir karakter arayışımdaki gibi), ancak başkalarında problemli olabilir.

0
 String name ="rami is good boy, and he is working for andorid,is completed"; 
    int lastSlash = name.lastIndexOf("is"); 
    String start = name.substring(0, lastSlash); 
    String end = name.substring(lastSlash + 1, name.length()); 
    StringBuffer sb = new StringBuffer(name); 
    sb.replace(start.length(), name.lastIndexOf(end)+1, ""); 

    System.out.println(sb.toString()); 
0

ref: https://github.com/apache/commons-lang/pull/273/files

public static int lastIndexOfAnyChar(final CharSequence str,final String searchChars) { 
    return searchChars == null ? INDEX_NOT_FOUND : lastIndexOfAnyChar(str,searchChars.toCharArray()); 
} 

/** 
* <p>Search a CharSequence to find the last index of any 
* character in the given set of characters.</p> 
* 
* <p>A {@code null} String will return {@code -1}. 
* A {@code null} or zero length search array will return {@code -1}.</p> 
* 
* <pre> 
* StringUtils.lastIndexOfAnyChar(null, *)    = -1 
* StringUtils.lastIndexOfAnyChar("", *)     = -1 
* StringUtils.lastIndexOfAnyChar(*, null)    = -1 
* StringUtils.lastIndexOfAnyChar(*, [])     = -1 
* StringUtils.lastIndexOfAnyChar("zzabyycdxx",['z','a']) = 2 
* StringUtils.lastIndexOfAnyChar("zzabyycdxx",['b','y']) = 5 
* StringUtils.lastIndexOfAnyChar("aba", ['z'])   = -1 
* </pre> 
* 
* @param cs the CharSequence to check, may be null 
* @param searchChars the chars to search for, may be null 
* @return the last index of any of the chars, -1 if no match or null input 
*/ 
public static int lastIndexOfAnyChar(final CharSequence str,final char... searchChars) { 
    if (isEmpty(str) || ArrayUtils.isEmpty(searchChars)) { 
     return INDEX_NOT_FOUND; 
    } 
    int csLen = str.length(); 
    int csLast = csLen - 1; 
    int searchLen = searchChars.length; 
    int searchLast = searchLen - 1; 
    for (int i = csLast ; i >= 0 ; i--) { 
     char ch = str.charAt(i); 
     for (int j = 0; j < searchLen; j++) { 
      if (searchChars[j] == ch) { 
       if (i < csLast && j < searchLast && Character.isHighSurrogate(ch)) { 
        // ch is a supplementary character 
        if (searchChars[j + 1] == str.charAt(i + 1)) { 
         return i; 
        } 
       } else { 
        return i; 
       } 
      } 
     } 
    } 
    return INDEX_NOT_FOUND; 
} 
İlgili konular