我想用另一个字符串替换字符串的某些部分。

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

I would like to replace a parts of string with another string

问题

import java.util.Scanner;
import java.util.concurrent.CountDownLatch;

public class Main {

    public static String line; // The line to format

    public static void main(String[] arrrgs) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a master String:");
        String line = input.nextLine();

        System.out.println("Enter a letter to look for:");
        String s = input.nextLine();
        System.out.println("Enter a starting location:");
        int begin = input.nextInt();

        // Part a
        int loc = begin;
        while (loc != line.length()) {
            loc++;

            if (loc != line.length()) {
                if (s.matches(line.substring(loc, loc + 1))) {
                    System.out.println(s + " appears at " + (loc + 1)); // prints location of string except for first one
                }
            }
        }
        int oomf = 1;
        if (s.matches(line.substring(0, 1))) // looks at the first letter
            System.out.println(oomf); // prints if necessary

        // Part b
        int count = 0;
        int timer = begin;
        while (timer != line.length()) {
            timer++;

            if (timer != line.length()) {
                if (s.matches(line.substring(timer, timer + 1))) {
                    count++;
                }
            }
        }
        System.out.println(s + " appears " + count + " times.");
    }
}
英文:

I am just starting and I have created parts of my code, part a finds the int location of the letter(or phrase)that the user is looking for in the Master string(which is line in the code). Part b counts how many times the letter(or phrase appears). In the next part(WITH OUT CREATING A NEW METHOD) I want if s(what the user is looking) is a "_" to replace them with something else like "-" for instance, and then print it out.
here is my code:

import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
public class Main
{
//my code acomplishes the goal but it dose not do it the way you are asking for, sorry:(
public static String line; // The line to format
public static void main(String [] arrrgs)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a master String:");
String line = input.nextLine();
System.out.println("Enter a letter to look for:");
String s = input.nextLine();
System.out.println("Enter a starting location:");
int begin = input.nextInt();
//part a
int loc = begin;
while (loc != line.length()){
loc++; 
if (loc != line.length()){
if (s.matches(line.substring(loc, loc + 1))){
System.out.println(s + " appears at " + (loc + 1));//prints location of strin except for first one
}
}   
}
int oomf = 1;
if(s.matches(line.substring(0, 1)))//looks at first letter
System.out.println(oomf); //prints if nesasary
//part b
int count = 0;
int timer = begin;
while (timer != line.length()){
timer++; 
if (timer != line.length()){
if (s.matches(line.substring(timer, timer + 1))){
count++;
}
}   
}
System.out.println(s +"apears " + count + " times.");
}
}

答案1

得分: 0

从注释中可以看出,有JAVA API来操作字符串。

以下是一个示例代码:

    public static void main(String[] args) {
		String testString = "Hello _ World _";
		
		// 使用Java API进行替换:
		System.out.println(testString.replace('_', '-'));
		
		
		// 使用自己编写的简单代码进行替换:
		String newString = "";
		for(int i = 0; i < testString.length(); i++) {
			if(testString.charAt(i) == '_') {
				newString += "-";
			} else {
				newString += testString.charAt(i);
			}
		}
		System.out.println(newString);
	}

你应该能够将这个示例代码应用到你的程序中。

请注意,在Java中,所有的字符串都是不可变的。这意味着一旦创建了一个字符串,就无法更改它。因此,你必须使用带有你的更改的返回值来创建新的字符串。

这就是为什么你需要使用replace()的返回值进行操作,原始字符串将保持不变。

英文:

As you see from the comments there is JAVA API to manipulate strings.

Here you are an example how to do it.

public static void main(String[] args) {
String testString = &quot;Hello _ World _&quot;;
// Replace with Java API:
System.out.println(testString.replace(&#39;_&#39;, &#39;-&#39;));
// Replace using own naive code:
String newString = &quot;&quot;;
for(int i = 0; i &lt; testString.length(); i++) {
if(testString.charAt(i) == &#39;_&#39;) {
newString += &quot;-&quot;;
} else {
newString += testString.charAt(i);
}
}
System.out.println(newString);
}

You should be able to adopt this to your program.

Please note that in java all strings are immutable. That means, that you can't change an string once it is created. So you have to create new strings with your changes.

This is why you have to work with the return value of replace(). Your original String will remain the same.

huangapple
  • 本文由 发表于 2020年10月26日 00:15:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64525930.html
匿名

发表评论

匿名网友

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

确定