In Java, I have a String:
在Java中,我有一个字符串:
Jamaica
I would like to remove the first character of the string and then return amaica
我想删除字符串的第一个字符,然后返回amaica
How would I do this?
我该怎么做呢?
12 个解决方案
#1
251
Use the substring()
function with an argument of 1
to get the substring from position 1 (after the first character) to the end of the string (leaving the second argument out defaults to the full length of the string).
使用substring()函数,参数为1,将子字符串从位置1(在第一个字符后面)到字符串的末尾(将第二个参数默认为字符串的全部长度)。
"Jamaica".substring(1);
#2
50
public String removeFirstChar(String s){
return s.substring(1);
}
#3
47
Use substring()
and give the number of characters that you want to trim from front.
使用substring()并给出要从前面修饰的字符数。
String value = "Jamaica";
value = value.substring(1);
Answer: "amaica"
答:“amaica”
#4
44
In Java, remove leading character only if it is a certain character
Use the Java ternary operator to quickly check if your character is there before removing it. This strips the leading character only if it exists, if passed a blank string, return blankstring.
使用Java三元运算符在删除字符之前快速检查字符是否存在。只有在存在的情况下,如果传递了一个空字符串,则返回空字符串。
String header = "";
header = header.startsWith("#") ? header.substring(1) : header;
System.out.println(header);
header = "foobar";
header = header.startsWith("#") ? header.substring(1) : header;
System.out.println(header);
header = "#moobar";
header = header.startsWith("#") ? header.substring(1) : header;
System.out.println(header);
Prints:
打印:
blankstring
foobar
moobar
Java, remove all the instances of a character anywhere in a string:
String a = "Cool";
a = a.replace("o","");
//variable 'a' contains the string "Cl"
Java, remove the first instance of a character anywhere in a string:
String b = "Cool";
b = b.replaceFirst("o","");
//variable 'b' contains the string "Col"
#5
16
You can use the substring method of the String
class that takes only the beginning index and returns the substring that begins with the character at the specified index and extending to the end of the string.
您可以使用String类的子字符串方法,该方法只获取起始索引,并返回从指定索引处的字符开始的子字符串,并将其扩展到字符串的末尾。
String str = "Jamaica";
str = str.substring(1);
#6
11
The key thing to understand in Java is that Strings are immutable -- you can't change them. So it makes no sense to speak of 'removing a character from a string'. Instead, you make a NEW string with just the characters you want. The other posts in this question give you a variety of ways of doing that, but its important to understand that these don't change the original string in any way. Any references you have to the old string will continue to refer to the old string (unless you change them to refer to a different string) and will not be affected by the newly created string.
在Java中要理解的关键是字符串是不可变的——你不能改变它们。所以说“从一个字符串中去掉一个字符”是没有意义的。相反,您可以用您想要的字符创建一个新的字符串。这个问题中的其他帖子给出了各种方法,但重要的是要理解这些不会以任何方式改变原始字符串。您对旧字符串的任何引用将继续引用旧字符串(除非您将它们更改为引用另一个字符串),并且不会受到新创建的字符串的影响。
This has a number of implications for performance. Each time you are 'modifying' a string, you are actually creating a new string with all the overhead implied (memory allocation and garbage collection). So if you want to make a series of modifications to a string and care only about the final result (the intermediate strings will be dead as soon as you 'modify' them), it may make more sense to use a StringBuilder or StringBuffer instead.
这对性能有很多影响。每次“修改”一个字符串时,实际上都是在创建一个包含所有开销的新字符串(内存分配和垃圾收集)。因此,如果您想对字符串进行一系列修改,并且只关心最终的结果(当您修改这些字符串时,中间字符串将会消失),那么使用StringBuilder或StringBuffer可能更有意义。
#7
9
public String removeFirst(String input)
{
return input.substring(1);
}
#8
8
you can do like this:
你可以这样做:
String str="Jamaica";
str=str.substring(1, title.length());
return str;
or in general:
public String removeFirstChar(String str){
return str.substring(1, title.length());
}
#9
7
substring() method returns a new String that contains a subsequence of characters currently contained in this sequence.
substring()方法返回一个新字符串,该字符串包含该序列中当前包含的字符的子序列。
The substring begins at the specified start
and extends to the character at index end - 1
.
子字符串从指定的开始开始,扩展到索引结束- 1的字符。
It has two forms. The first is
它有两种形式。第一个是
String substring(int FirstIndex)
- 字符串的子串(int FirstIndex)
Here, FirstIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at FirstIndex and runs to the end of the invoking string.
在这里,FirstIndex指定子字符串开始的索引。该表单返回从FirstIndex开始并运行到调用字符串末尾的子字符串的副本。
String substring(int FirstIndex, int endIndex)
- 字符串子串(int FirstIndex, int endIndex)
Here, FirstIndex specifies the beginning index, and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index.
在这里,FirstIndex指定开始索引,而endIndex指定停止点。返回的字符串包含从开始索引到(但不包括)结束索引的所有字符。
Example
String str="Amiyo";
// prints substring from index 3
System.out.println("substring is = " + str.substring(3)); // Output "yo'
#10
3
I came across a situation where I had to remove not only the first character (if it was a #
, but the first set of characters.
我遇到的情况是,我不仅要删除第一个字符(如果它是一个#,而是第一个字符)。
String myString = ###Hello World
could be the starting point, but I would only want to keep the Hello World
. this could be done as following.
字符串myString = ##Hello World可能是起点,但我只想保持Hello World。可以这样做。
while (myString.charAt(0) == '#') { // Remove all the # chars in front of the real string
myString = myString.substring(1, myString.length());
}
For OP's case, replace while
with if
and it works aswell.
对于OP的情况,替换为if,它也可以工作。
#11
2
Another solution, you can solve your problem using replaceAll
with some regex ^.{1}
(regex demo) for example :
另一个解决方案,你可以解决你的问题用replaceAll regex ^。{1} (regex demo)示例:
String str = "Jamaica";
int nbr = 1;
str = str.replaceAll("^.{" + nbr + "}", "");//Output = amaica
#12
1
My version of removing leading chars, one or multiple. For example, String str1 = "01234", when removing leading '0', result will be "1234". For a String str2 = "000123" result will be again "123". And for String str3 = "000" result will be empty string: "". Such functionality is often useful when converting numeric strings into numbers.The advantage of this solution compared with regex (replaceAll(...)) is that this one is much faster. This is important when processing large number of Strings.
我的版本删除了一个或多个字符。例如,字符串str1 = "01234",当删除前导'0'时,结果将是"1234"。对于字符串str2 =“000123”,结果将再次是“123”。对于字符串str3 = "000",结果将为空字符串:""。当把数字字符串转换成数字时,这种功能通常很有用。与regex (replaceAll(…))相比,这个解决方案的优点是它要快得多。这在处理大量字符串时非常重要。
public static String removeLeadingChar(String str, char ch) {
int idx = 0;
while ((idx < str.length()) && (str.charAt(idx) == ch))
idx++;
return str.substring(idx);
}
#1
251
Use the substring()
function with an argument of 1
to get the substring from position 1 (after the first character) to the end of the string (leaving the second argument out defaults to the full length of the string).
使用substring()函数,参数为1,将子字符串从位置1(在第一个字符后面)到字符串的末尾(将第二个参数默认为字符串的全部长度)。
"Jamaica".substring(1);
#2
50
public String removeFirstChar(String s){
return s.substring(1);
}
#3
47
Use substring()
and give the number of characters that you want to trim from front.
使用substring()并给出要从前面修饰的字符数。
String value = "Jamaica";
value = value.substring(1);
Answer: "amaica"
答:“amaica”
#4
44
In Java, remove leading character only if it is a certain character
Use the Java ternary operator to quickly check if your character is there before removing it. This strips the leading character only if it exists, if passed a blank string, return blankstring.
使用Java三元运算符在删除字符之前快速检查字符是否存在。只有在存在的情况下,如果传递了一个空字符串,则返回空字符串。
String header = "";
header = header.startsWith("#") ? header.substring(1) : header;
System.out.println(header);
header = "foobar";
header = header.startsWith("#") ? header.substring(1) : header;
System.out.println(header);
header = "#moobar";
header = header.startsWith("#") ? header.substring(1) : header;
System.out.println(header);
Prints:
打印:
blankstring
foobar
moobar
Java, remove all the instances of a character anywhere in a string:
String a = "Cool";
a = a.replace("o","");
//variable 'a' contains the string "Cl"
Java, remove the first instance of a character anywhere in a string:
String b = "Cool";
b = b.replaceFirst("o","");
//variable 'b' contains the string "Col"
#5
16
You can use the substring method of the String
class that takes only the beginning index and returns the substring that begins with the character at the specified index and extending to the end of the string.
您可以使用String类的子字符串方法,该方法只获取起始索引,并返回从指定索引处的字符开始的子字符串,并将其扩展到字符串的末尾。
String str = "Jamaica";
str = str.substring(1);
#6
11
The key thing to understand in Java is that Strings are immutable -- you can't change them. So it makes no sense to speak of 'removing a character from a string'. Instead, you make a NEW string with just the characters you want. The other posts in this question give you a variety of ways of doing that, but its important to understand that these don't change the original string in any way. Any references you have to the old string will continue to refer to the old string (unless you change them to refer to a different string) and will not be affected by the newly created string.
在Java中要理解的关键是字符串是不可变的——你不能改变它们。所以说“从一个字符串中去掉一个字符”是没有意义的。相反,您可以用您想要的字符创建一个新的字符串。这个问题中的其他帖子给出了各种方法,但重要的是要理解这些不会以任何方式改变原始字符串。您对旧字符串的任何引用将继续引用旧字符串(除非您将它们更改为引用另一个字符串),并且不会受到新创建的字符串的影响。
This has a number of implications for performance. Each time you are 'modifying' a string, you are actually creating a new string with all the overhead implied (memory allocation and garbage collection). So if you want to make a series of modifications to a string and care only about the final result (the intermediate strings will be dead as soon as you 'modify' them), it may make more sense to use a StringBuilder or StringBuffer instead.
这对性能有很多影响。每次“修改”一个字符串时,实际上都是在创建一个包含所有开销的新字符串(内存分配和垃圾收集)。因此,如果您想对字符串进行一系列修改,并且只关心最终的结果(当您修改这些字符串时,中间字符串将会消失),那么使用StringBuilder或StringBuffer可能更有意义。
#7
9
public String removeFirst(String input)
{
return input.substring(1);
}
#8
8
you can do like this:
你可以这样做:
String str="Jamaica";
str=str.substring(1, title.length());
return str;
or in general:
public String removeFirstChar(String str){
return str.substring(1, title.length());
}
#9
7
substring() method returns a new String that contains a subsequence of characters currently contained in this sequence.
substring()方法返回一个新字符串,该字符串包含该序列中当前包含的字符的子序列。
The substring begins at the specified start
and extends to the character at index end - 1
.
子字符串从指定的开始开始,扩展到索引结束- 1的字符。
It has two forms. The first is
它有两种形式。第一个是
String substring(int FirstIndex)
- 字符串的子串(int FirstIndex)
Here, FirstIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at FirstIndex and runs to the end of the invoking string.
在这里,FirstIndex指定子字符串开始的索引。该表单返回从FirstIndex开始并运行到调用字符串末尾的子字符串的副本。
String substring(int FirstIndex, int endIndex)
- 字符串子串(int FirstIndex, int endIndex)
Here, FirstIndex specifies the beginning index, and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index.
在这里,FirstIndex指定开始索引,而endIndex指定停止点。返回的字符串包含从开始索引到(但不包括)结束索引的所有字符。
Example
String str="Amiyo";
// prints substring from index 3
System.out.println("substring is = " + str.substring(3)); // Output "yo'
#10
3
I came across a situation where I had to remove not only the first character (if it was a #
, but the first set of characters.
我遇到的情况是,我不仅要删除第一个字符(如果它是一个#,而是第一个字符)。
String myString = ###Hello World
could be the starting point, but I would only want to keep the Hello World
. this could be done as following.
字符串myString = ##Hello World可能是起点,但我只想保持Hello World。可以这样做。
while (myString.charAt(0) == '#') { // Remove all the # chars in front of the real string
myString = myString.substring(1, myString.length());
}
For OP's case, replace while
with if
and it works aswell.
对于OP的情况,替换为if,它也可以工作。
#11
2
Another solution, you can solve your problem using replaceAll
with some regex ^.{1}
(regex demo) for example :
另一个解决方案,你可以解决你的问题用replaceAll regex ^。{1} (regex demo)示例:
String str = "Jamaica";
int nbr = 1;
str = str.replaceAll("^.{" + nbr + "}", "");//Output = amaica
#12
1
My version of removing leading chars, one or multiple. For example, String str1 = "01234", when removing leading '0', result will be "1234". For a String str2 = "000123" result will be again "123". And for String str3 = "000" result will be empty string: "". Such functionality is often useful when converting numeric strings into numbers.The advantage of this solution compared with regex (replaceAll(...)) is that this one is much faster. This is important when processing large number of Strings.
我的版本删除了一个或多个字符。例如,字符串str1 = "01234",当删除前导'0'时,结果将是"1234"。对于字符串str2 =“000123”,结果将再次是“123”。对于字符串str3 = "000",结果将为空字符串:""。当把数字字符串转换成数字时,这种功能通常很有用。与regex (replaceAll(…))相比,这个解决方案的优点是它要快得多。这在处理大量字符串时非常重要。
public static String removeLeadingChar(String str, char ch) {
int idx = 0;
while ((idx < str.length()) && (str.charAt(idx) == ch))
idx++;
return str.substring(idx);
}