如何在java中删除除第一个以外的字符串字符
在java中,我有一个字符串:如何在java中删除除第一个以外的字符串字符,java,string,Java,String,在java中,我有一个字符串: James May, Jack Jones 我想删除字符,除了第一个和添加点。 Retrun应该看起来像J.May,J.Jones 我该怎么做 我试过这个 for (int i=1; i<str.length();i++) { if (i%2==0) { dot=str.replaceAll("[a-z]", "."); jTextArea2.setText(dot);
James May, Jack Jones
我想删除字符,除了第一个和添加点。
Retrun应该看起来像J.May,J.Jones
我该怎么做
我试过这个
for (int i=1; i<str.length();i++)
{
if (i%2==0)
{
dot=str.replaceAll("[a-z]", ".");
jTextArea2.setText(dot);
}
}
for(inti=1;i您可以使用它,它不是很灵活,但我认为它适用于您想要做的事情
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);
像这样
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
}
您也可以使用这个,它可以像您所希望的那样工作
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);
String a=“詹姆斯·梅”;
字符串[]b=a.split(“”);
字符串c=b[0]。字符(0)+“;
for(int i=1;i
或者,如果您想要一些可以多次使用的东西:
(归功于阿维纳什·拉吉)
String a=“詹姆斯·梅、杰克·琼斯、詹姆·马丁内斯”;
字符串[]b=a.split(“,”);
字符串c=b[0].replaceFirst((?您可以使用该类并使其适应您自己的java代码,前提是您将原始字符串保持为“FirstName LastName”
格式,并使用,“
拆分字符串“FirstName LastName,FirstName LastName”
公共类StringEditor
{
String editMe=“詹姆斯·梅,杰克·琼斯”;
public void runStringEdit()
{
字符串edit1=editMe.replaceAll(“,”,”);
System.out.println(“在第一步之后:+edit1”);
字符串[]edit2=edit1.split(“”);
对于(int i=0;我到目前为止你试过什么了吗?为什么它不会删除May
子字符串?有很多简单的方法。你试过什么?为什么失败了?你测试了什么输入?得到了什么输出?调试时发生了什么?@AvinashRaj在英语中,在这个人身上缩写人名有时很常见内尔。比如J.K.罗琳。这是我最好的猜测。请用你尝试过的内容更新你的帖子。谢谢,这很好,但我需要做更多次。比如输入是“詹姆斯·梅,杰克·琼斯。。。“检查编辑。不确定您打算如何存储名称列表谢谢,这很好,但我需要做更多次。例如,如果输入为“James May,Jack Jones…”谢谢,但我从键盘输入字符串,这只适用于第一个名称。例如,我键入“James May,Jack Jones…”,这给我“J.May,Jack Jones…””这就是我想要的。非常感谢!
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);
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();
}
}