我希望用'和'替换最后一个逗号。

时间:2015-05-02 05:14:39

标签: android string replace comma

我想从字符串中删除最后一个逗号。我试过这样做:

int endIndex = frnd.lastIndexOf(",");
if (endIndex != -1) {
    frnd = frnd.substring(0, endIndex);
}

并将第二个逗号替换为'和'

5 个答案:

答案 0 :(得分:1)

朋友String将获得所需的结果。

String frnd1 = "";
String frnd2 = "";
String frnd3 = "";
int endIndex = frnd.lastIndexOf(",");
if (endIndex != -1) {
    frnd1 = frnd.substring(0, endIndex);
    frnd2 = frnd.substring(endIndex);
    int endIndex1 = frnd1.lastIndexOf(",");
    if (endIndex1 != -1) {
        frnd3 = frnd1.substring(0, endIndex1) + " and" + frnd1.substring(endIndex1+1);;
        frnd = frnd3 + frnd2;
        System.out.println(frnd3 + frnd2);                      
    }
}

答案 1 :(得分:0)

检查下面的小例子。

String test="abc,d,";

int endIndex = test.lastIndexOf(",");

if (endIndex != -1) {

test = test.substring(0, endIndex);

if(test.contains(","))

{

System.out.println(test);

String replacedstring=test.replace(",","and");

System.out.println(replacedstring);

}

}

答案 2 :(得分:0)

这个简单的解决方案:

    int lastIndx = yourWord.lastIndexOf(","); // last inedx of ','

    if(lastIndx != -1){
       int secondLast = yourWord.lastIndexOf(",", lastIndx -1); // second last index of  ','
       if (secondLast != -1){
           result = yourWord.substring(0, secondLast ) + "and" +  yourWord.substring(secondLast +1) ;    
       }
}

结果是您需要的输出

答案 3 :(得分:0)

首先尝试删除上一个comma,然后尝试将最后一个comma替换为and

String frnd="abcde,fghi,jklmn,nopq";
if (frnd.lastIndexOf(",") != -1) {
    frnd = frnd.substring(0, frnd.lastIndexOf(",")) + frnd.substring(frnd.lastIndexOf(",") + 1, frnd.length());
    if (frnd.lastIndexOf(",") != -1) {
        frnd = frnd.substring(0, frnd.lastIndexOf(",")) + "and" + frnd.substring(frnd.lastIndexOf(",") + 1, frnd.length());
    }
}

Toast.makeText(this,frnd,Toast.LENGTH_LONG).show();

答案 4 :(得分:0)

如果有人看起来像这样 输入:纽约,拉斯维加斯,德里,孟买 产出:纽约,拉斯维加斯,德里和孟买。

然后这个工作

public String formatString(String inputString)
{
    String firstOccurance = "";
    String secondOccurance = "";
    String thirdOccurance = "";
    String formattedString="";
    String matcher1=",";
    String matcher2=", ";
    String matcher3=" ,";

    inputString=inputString.replaceAll(", $", "");
    inputString=inputString.replaceAll(" ,$", "");
    inputString=inputString.replaceAll(" ,$", "");
    inputString=inputString.replaceAll(".$", "");
    inputString=inputString.replaceAll(" .$", "");
    inputString=inputString.replaceAll(". $", "");

    int endIndex = inputString.lastIndexOf(matcher1);

    if(endIndex==-1)
        endIndex = inputString.lastIndexOf(matcher2);
    if(endIndex==-1)
        endIndex = inputString.lastIndexOf(matcher3);
    if (endIndex != -1) {
        firstOccurance = inputString.substring(0, endIndex);
        secondOccurance = inputString.substring(endIndex);
        int endIndex1 = secondOccurance.lastIndexOf(matcher1);
        if(endIndex1==-1)
             endIndex1 = secondOccurance.lastIndexOf(matcher2);
        if(endIndex1==-1)
             endIndex1 = secondOccurance.lastIndexOf(matcher3);

        if (endIndex1 != -1) {
            thirdOccurance = firstOccurance + " and " + secondOccurance.substring(endIndex1+1)+".";
            formattedString =thirdOccurance;// thirdOccurance + secondOccurance;
            System.out.println(thirdOccurance + secondOccurance);    
        }
    }


    return !formattedString.isEmpty()?formattedString:inputString;
}