Java - 删除字符串的第一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4503656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - removing first character of a string
提问by Calibre2010
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?
我该怎么做?
回答by Ahmed Kotb
public String removeFirstChar(String s){
return s.substring(1);
}
回答by Reese Moore
public String removeFirst(String input)
{
return input.substring(1);
}
回答by moinudin
Use the substring()
function with an argument of 1
to get the substring from position 1 (afterthe 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);
回答by codaddict
You can use the substringmethod 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.
您可以使用该类的substring方法,该方法String
仅采用开始索引并返回以指定索引处的字符开头并延伸到字符串末尾的子字符串。
String str = "Jamaica";
str = str.substring(1);
回答by Chris Dodd
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 可能更有意义。
回答by Eric Leschinski
In Java, remove leading character only if it is a certain character
在Java中,仅当它是某个字符时才删除前导字符
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:
Java,删除字符串中任意位置的所有字符实例:
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:
Java,删除字符串中任意位置的第一个字符实例:
String b = "Cool";
b = b.replaceFirst("o","");
//variable 'b' contains the string "Col"
回答by Thilina Gihan Priyankara
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”
回答by IntelliJ Amiya
substring()method returns a new String that contains a subsequenceof characters currently contained in this sequence.
SUBSTRING()方法返回一个包含一个新的字符串序列当前包含在该序列的字符。
The substringbegins at the specified start
and extends to the character at index end - 1
.
所述子开始在指定的start
,并且延伸到在该字符index end - 1
。
It has two forms. The first is
它有两种形式。第一个是
String substring(int FirstIndex)
String substring(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)
String substring(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'
回答by eli am
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());
}
回答by Mathieu Brouwers
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.
String 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 的情况,替换while
为if
并且它也可以工作。