从原始myString中删除输入字符串。

huangapple go评论79阅读模式
英文:

Remove the input string from the original myString

问题

我正在尝试从公共字符串myString中移除输入字符串。

myString = "Hello"

removeS("el")将返回"Hlo"

以下是我的代码和其中的问题:不兼容的类型:java.lang.String无法转换为int,我的问题在于String result。我希望您使用substring()和indexOf()来解决我的问题,而不要使用除这些之外的任何其他工具。我该如何修复这个问题。

public class StringI
{
public String myString;

public String removeS(String s){

    myString = "Hello";
    
    int index = myString.indexOf(s);  // 使用indexOf()找到要移除的子字符串的索引
    if (index != -1) {  // 确保找到了要移除的子字符串
        String result = myString.substring(0, index) + myString.substring(index + s.length());  // 使用substring()进行字符串拼接
        return result;
    } else {
        return myString;  // 如果未找到要移除的子字符串,返回原字符串
    }
}

public static void main(String args[]){
    StringI n = new StringI();
    System.out.println(n.removeS("el"));  // 打印结果
}

}


<details>
<summary>英文:</summary>

I am trying to remove the input string from the public string myString

myString = &quot;Hello&quot;

removeS(&quot;el&quot;) will return &quot;Hlo&quot;

Heres my code below and the problem in it is incompatible types: java.lang.String cannot be converted to int in my String result. I would like it if you used substring() and indexOf() to solve my issue and not use any other power tools than those. How would I go about fixing this.

public class StringI
{
public String myString;

public String removeS(String s){

myString = &quot;Hello&quot;;


String result = s.substring(myString);

return result;
}

public static void main(String args[]){

StringI n = new StringI();

n.removeS("el");

}

}


</details>


# 答案1
**得分**: 1

我们需要获取前半部分和后半部分,然后将它们拼接在一起,如下所示:

```java
String result = myString.substring(0, myString.indexOf(s)) + myString.substring(myString.indexOf(s) + s.length());

由于 .indexOf() 返回 s 的位置,我们可以使用 substring 从字符串的开头开始到该位置,然后使用另一个 substring 获取最后部分。

以下程序:

import java.util.Scanner;

public class Main {
    public static String removeS(String s) {
        String myString = "Hello";
        String result = myString.substring(0, myString.indexOf(s)) + myString.substring(myString.indexOf(s) + s.length(), myString.length());
        return result;
    }

    public static void main(String[] args) {
        System.out.println(removeS("el"));
    }
}

产生的输出结果为:

Hlo
英文:

We need to get the first half and the last half, then piece them together like so:

String result = myString.substring(0, myString.indexOf(s))+myString.substring(myString.indexOf(s)+s.length());

Since .indexOf() returns the position of s, we can use substring to start from the start of the String to that position, then use another substring to get the final part.

The following program:

import java.util.Scanner; 
 
 public class Main {
     public static String removeS(String s){
         String myString = &quot;Hello&quot;;
         String result = myString.substring(0, myString.indexOf(s))+myString.substring(myString.indexOf(s)+s.length(), myString.length());
         return result;
     }
 public static void main(String[] args) {
    System.out.println(removeS(&quot;el&quot;));
 }
}

Produces:

Hlo

答案2

得分: 1

public class StringI
{
    public String myString;

    public String removeS(String s){
        myString = "Hello";
        
        // Before the `s`
        String part1 = myString.substring(0, myString.indexOf(s));
        // After the `s`
        String part2 = myString.substring(myString.indexOf(s) + s.length());

        return part1 + part2;
    }

    public static void main(String args[]){
        StringI n = new StringI();
        String result = n.removeS("el");
        
        System.out.println(result);
    }
}
英文:

Using Substring: You can make this into two part:

public class StringI
{
    public String myString;

    public String removeS(String s){
        myString = &quot;Hello&quot;;
        
        // Before the `s`
        String part1 = myString.substring(0, myString.indexOf(s));
        // After the `s`
        String part2 = myString.substring(myString.indexOf(s) + s.length());

        return part1 + part2;
    }

    public static void main(String args[]){
        StringI n = new StringI();
        String result = n.removeS(&quot;el&quot;);
        
        System.out.println(result);
    }
}

答案3

得分: -1

以下是翻译好的内容:

最简单的方法是-

public static void main(String[] args) {
    String myString = "Hello";
    String removeString = "el";
    System.out.println(myString.replace(removeString, ""));
}
英文:

The easiest way to do is-

public static void main(String[] args) {
    String myString = &quot;Hello&quot;;
    String removeString=&quot;el&quot;;
    System.out.println(myString.replace(removeString,&quot;&quot;));
}

huangapple
  • 本文由 发表于 2020年10月9日 05:33:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64270933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定