简体   繁体   English

如何在字符串中x个字符后返回所有内容

[英]How to return everything after x characters in a string

Say I had String s = "This is a loooooooooooooooong string"; 说我有String s = "This is a loooooooooooooooong string"; . Now say I want to cut off This and return everything else. 现在说我想切断This并返回其他所有内容。 How would I do this? 我该怎么做? What I'm asking is, is there a method that returns everything after x characters? 我要问的是,有没有一种方法可以在x字符后返回所有内容?

Yes there is. 就在这里。 And that method is the substring() method, which takes an integer as its argument and slices off everything before the specified character position. 该方法是substring()方法,该方法以整数作为参数,并在指定字符位置之前切掉所有内容。

String s = "This is a loooooooooooooooong string";
System.out.println(s.substring(5));

Output: 输出:

is a loooooooooooooooong string

You are looking for string result = s.substring(4); 您正在寻找string result = s.substring(4);

String#substring(int) takes the start index. String#substring(int)采用起始索引。

Sure. 当然。 :-) :-)

return s.substring(5);

substring is what you're looking for. substring是您要寻找的。

Example from the docs: 来自文档的示例:

 "unhappy".substring(2) returns "happy"
 "Harbison".substring(3) returns "bison"
 "emptiness".substring(9) returns "" (an empty string)

You'll want to use String.substring(int) . 您将要使用String.substring(int) It takes a String and returns a piece of it. 它需要一个String并返回它的一部分。 Like most things in Java, it is zero-based. 像Java中的大多数东西一样,它是从零开始的。 That means that the first character is 0, not 1. Also, substring is inclusive . 这意味着第一个字符是0,而不是1。此外, substring包含在内的 That means it will keep the character indicated by the index, not lose it. 这意味着它将保留索引指示的字符,而不丢失它。 Finally, substring does not change the original string, so you need to assign the return value to something. 最后, substring不会更改原始字符串,因此您需要将返回值分配给某些内容。 Here's an example: 这是一个例子:

String str = "Hello World!";
System.out.println(str.substring(6)); // World!
System.out.println(str); // Hello World!

Now, sometimes you want to take a part of the string that is in the beginning or middle, not the end. 现在,有时您希望将字符串的一部分放在开头或中间,而不是结尾。 You can do this with String.substring(int, int) . 您可以使用String.substring(int, int) This has two ints, the start index and the end index. 它有两个整数,开始索引和结束索引。 Now, while the start index is inclusive , the end index is exclusive . 现在,虽然开始索引是包含的 ,但结束索引是排他的 Here's an example: 这是一个例子:

String str = "Hello World!";
System.out.println(str.substring(0, 5)); // Hello

You can find both of these methods in the JavaDocs . 您可以在JavaDocs中找到这两种方法。 In fact, generally the first Google result for the phrase java {class name} is the reference for that class, so they're easy to look up. 实际上,通常词组java {class name}的第一个Google结果就是该类的引用,因此很容易查找。 They're extremely useful, so be sure to check them out. 它们非常有用,因此请务必将其签出。

substring() is the method you can use. substring()是您可以使用的方法。

String word= "This";
return s.substring(word.length());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM