英文:
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 = "Hello _ World _";
// Replace with Java API:
System.out.println(testString.replace('_', '-'));
// Replace using own naive code:
String newString = "";
for(int i = 0; i < testString.length(); i++) {
if(testString.charAt(i) == '_') {
newString += "-";
} 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论