【问题标题】:How to remove string characters except first in java如何在java中删除除第一个之外的字符串字符
【发布时间】:2015-05-18 17:00:30
【问题描述】:

在java中,我有一个字符串:

James May, Jack Jones

我想删除除 first 之外的字符并添加点。 重新运行应该看起来像J. May, J. Jones

我该怎么做?

我试过了

for (int i=1; i<str.length();i++)
    {
        if (i%2==0)
        {
            dot=str.replaceAll("[a-z]", ".");
            jTextArea2.setText(dot);
        } 
    }

但它会替换所有小写字符

【问题讨论】:

  • 到目前为止你有没有尝试过?
  • 为什么它不会删除May 子字符串?
  • 有很多简单的方法可以解决这个问题。你试过什么?为什么失败了?你测试了哪些输入?你得到了什么输出?调试时发生了什么?
  • @AvinashRaj 在英语中,有时以这种方式缩写一个人的名字是很常见的。像 J.K.以罗琳为例。这是我最好的猜测。
  • 请用您尝试过的内容更新您的帖子。

标签: java string


【解决方案1】:

您可以使用这个类并将其改编为您自己的 java 代码,假设您将原始字符串保留为"FirstName LastName" 的格式并使用", " 拆分字符串"FirstName LastName, FirstName LastName"

public class StringEditor
{
    String editMe = "James May, Jack Jones";

    public void runStringEdit()
    {
        String edit1 = editMe.replaceAll(",", "");
        System.out.println("After step one: "+edit1);
        String[] edit2 = edit1.split(" ");

        for(int i =0; i<edit2.length; i++)
        {
            int correctNum = i+1;
            System.out.println("After part "+correctNum+" of step 2: "+edit2[i]);
        }

        for(int i =0; i<edit2.length; i++)
        {
            int correctNum = i+1;
            if(correctNum%2!=0)
            {
                edit2[i]=edit2[i].charAt(0)+". ";
                System.out.println("After part "+correctNum+" of step 3: "+edit2[i]);
            }
            else
            {
                try
                {
                    if(!edit2[correctNum].equals("")) //Stops "," being added to last surname by causing a null pointer exception in the if statement
                    {
                        edit2[i]=edit2[i]+", ";
                    }
                }
                catch(Exception e)
                {

                }

                System.out.println("After part "+correctNum+" of step 3: "+edit2[i]);
            }
        }

        String finalEdit = "";

        for(int i =0; i<edit2.length; i++)
        {
            int correctNum = i+1;
            finalEdit = finalEdit+edit2[i];
        }

        System.out.println("After final step: "+finalEdit);
    }

    public static void main(String[] args)
    {
        StringEditor sE = new StringEditor();
        sE.runStringEdit();
    }
}

使用这个类的最终输出将是FirstName Intial. LastName, FirstName Intial. LastName,在这种情况下等于J. May, J. Jones

实际的输出显示了程序是如何一步步完成的,会是这样的;

【讨论】:

    【解决方案2】:

    您也可以使用它,它可以随心所欲地使用

    String a = "James May";
    String[] b = a.split(" ");
    String c = b[0].charAt(0) + ". ";
    for (int i = 1; i < b.length; i++) {
        c += b[i] + " ";
    }
    System.out.println(c);
    

    或者如果你想要让它多次使用的东西: (归功于 Avinash Raj)

    String a = "James May, Jack Jones, Jaime Martinez";
    String[] b = a.split(", ");
    String c = b[0].replaceFirst("(?<=^.)\\S+", "."); 
    for(int i = 1; i < b.length; i++) {
    c += ", " + b[i].replaceFirst("(?<=^.)\\S+", ".");
    }
    System.out.println(c);
    

    【讨论】:

    • 谢谢,但我从键盘输入字符串,这仅适用于第一个名字。例如,我输入“James May, Jack Jones, ...”这给了我“J. May, Jack Jones, ...”
    • 这就是我想要的。非常感谢!
    【解决方案3】:

    你可以使用它,它不是很灵活,但我认为它适用于你想做的事情。

    String a = "James May";
    String[] c = a.split("\\s+");
    String b = c[0].substring(0, 1) + ". " + c[1].substring(0, c[1].length());  
    System.out.println(b);
    

    【讨论】:

    • 谢谢,这很好,但我需要做更多次。就像输入是“詹姆斯·梅,杰克·琼斯......”
    【解决方案4】:

    这样

        String str = "James May";
        String abr = 
                str.substring(0, 1)//get the first character
                +". "// add a dot and space
                +str.split(" ")[1];//get the last name
    

    编辑:遍历数组

    for(String str : arrayOfNames){
        String str = "James May";
        String abr = 
                str.substring(0, 1)//get the first character
                +". "// add a dot and space
                +str.split(" ")[1];//get the last name
                //do something with abr
    }
    

    【讨论】:

    • 谢谢,这很好,但我需要做更多次。就像输入是“詹姆斯·梅,杰克·琼斯......”
    • 检查编辑。不确定您打算如何存储您的姓名列表
    猜你喜欢
    • 2011-05-29
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多