用不使用 replace() 的字符串替换子字符串
Replacing substrings with Strings not using replace()
问题是:mutation()
传递了两个字符串,returns 传递了一个字符串。 fzgh
的第一个字符串中的每个出现都被第二个字符串替换。
mutation("Hello. I want an fzgh. Give me an fzgh now.", "IPhone 6")-> "Hello. I want an IPhone 6. Give me an IPhone 6 now."
这是我的尝试:
public static String mutation(String s, String t){
int f=s.indexOf("fzgh");
String w="";
if(f !=-1){
w=w+s.substring(0,f)+t;
}
return w;
}
我知道有 .replace()
,但我们不允许使用它。我们必须使用 indexOf()
它有点笨拙但是你走了,如果你需要解释我会同时编辑:)
*编辑:它还不够好,会尝试修复它。这是另一种方式,有点不同的解决方案,但不是很好,如果我无法修复,将删除它。
*编辑 #2:已修复
import java.util.Scanner;
import java.io.*;
class Mutation
{
public static String mutation(String s, String t){
String toBeReplaced = "fzgh";
int sizeOfThatString = toBeReplaced.length();
String[] array = s.split(" ");
for (int i = 0; i < array.length; i++) {
String tempString= array[i];
if(tempString.length()>sizeOfThatString) {
if(tempString.contains(toBeReplaced)) {
String other = array[i].substring(sizeOfThatString);
array[i] = t+other;
}
}
if(tempString.equals(toBeReplaced)) {
array[i] = t;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if(i!=array.length) {
sb.append(array[i]+" ");
}
else {
sb.append(array[i]);
}
}
return sb.toString();
}
public static void main(String[] args) {
Arabic_Randomizer ar= new Arabic_Randomizer();
System.out.println(ar.mutation("Hello. I want an fzgh. Give me an fzgh now.", "IPhone 6"));
}
}
如果你可以使用split
和join
方法,这很容易解决。
public static String mutation(String s, String t) {
return String.join(t, s.split("fzgh"));
}
split
会将输入字符串围绕 "fzgh"
和 return 拆分为 String
的数组,在这种情况下,它将 return ["Hello. I want an ", ". Give me an ", " now."]
.现在您可以使用 join
方法将每个元素与 "IPhone 6"
连接在一起。
注意在Java8中引入了join
。
但是如果你只需要indexOf
和substring
来解决这个问题,代码会更复杂,但不会那么复杂。
public static String mutation(String s, String t) {
StringBuilder sb = new StringBuilder();
int p = 0, q = 0;
while ((q = s.indexOf("fzgh", p)) >= 0) {
sb.append(s.substring(p, q));
sb.append(t);
p = q+4;
}
sb.append(s.substring(p));
return sb.toString();
}
你可以这样定义变异
public static String mutation(String s,String t){
int f=s.indexOf("fzgh");
int l =4;//length of "fzgh"
String w = s;
while(f!=-1){
w=w.substring(0,f)+t+w.substring(f+l,w.length());
f=w.indexOf("fzgh");
}
return w;
}
这将从 String s 中删除所有 "fzgh" 并将它们替换为 String t。
使用此代码
public class 运行 {
public static String mutation(String w,String s, String t){
String toBeReplaced = w;
int sizeOfThatString = toBeReplaced.length();
String[] array = s.split(" ");
for (int i = 0; i < array.length; i++) {
String tempString= array[i];
if(tempString.length()>sizeOfThatString) {
tempString = array[i].substring(0,sizeOfThatString);
System.out.println("TempString " +tempString);
}
if(tempString.equals(w)) {
array[i] = t;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if(i!=array.length) {
sb.append(array[i]+" ");
}
else {
sb.append(array[i]);
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(mutation("fzgh","Hello. I want an fzgh. Give me an fzgh now.", "IPhone 6"));
}
}
输出:
- TempString 地狱
- TempString fzgh
- 您好。我想要一个 IPhone 6 现在给我一个 IPhone 6。
问题是:mutation()
传递了两个字符串,returns 传递了一个字符串。 fzgh
的第一个字符串中的每个出现都被第二个字符串替换。
mutation("Hello. I want an fzgh. Give me an fzgh now.", "IPhone 6")-> "Hello. I want an IPhone 6. Give me an IPhone 6 now."
这是我的尝试:
public static String mutation(String s, String t){
int f=s.indexOf("fzgh");
String w="";
if(f !=-1){
w=w+s.substring(0,f)+t;
}
return w;
}
我知道有 .replace()
,但我们不允许使用它。我们必须使用 indexOf()
它有点笨拙但是你走了,如果你需要解释我会同时编辑:)
*编辑:它还不够好,会尝试修复它。这是另一种方式,有点不同的解决方案,但不是很好,如果我无法修复,将删除它。
*编辑 #2:已修复
import java.util.Scanner;
import java.io.*;
class Mutation
{
public static String mutation(String s, String t){
String toBeReplaced = "fzgh";
int sizeOfThatString = toBeReplaced.length();
String[] array = s.split(" ");
for (int i = 0; i < array.length; i++) {
String tempString= array[i];
if(tempString.length()>sizeOfThatString) {
if(tempString.contains(toBeReplaced)) {
String other = array[i].substring(sizeOfThatString);
array[i] = t+other;
}
}
if(tempString.equals(toBeReplaced)) {
array[i] = t;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if(i!=array.length) {
sb.append(array[i]+" ");
}
else {
sb.append(array[i]);
}
}
return sb.toString();
}
public static void main(String[] args) {
Arabic_Randomizer ar= new Arabic_Randomizer();
System.out.println(ar.mutation("Hello. I want an fzgh. Give me an fzgh now.", "IPhone 6"));
}
}
如果你可以使用split
和join
方法,这很容易解决。
public static String mutation(String s, String t) {
return String.join(t, s.split("fzgh"));
}
split
会将输入字符串围绕 "fzgh"
和 return 拆分为 String
的数组,在这种情况下,它将 return ["Hello. I want an ", ". Give me an ", " now."]
.现在您可以使用 join
方法将每个元素与 "IPhone 6"
连接在一起。
注意在Java8中引入了join
。
但是如果你只需要indexOf
和substring
来解决这个问题,代码会更复杂,但不会那么复杂。
public static String mutation(String s, String t) {
StringBuilder sb = new StringBuilder();
int p = 0, q = 0;
while ((q = s.indexOf("fzgh", p)) >= 0) {
sb.append(s.substring(p, q));
sb.append(t);
p = q+4;
}
sb.append(s.substring(p));
return sb.toString();
}
你可以这样定义变异
public static String mutation(String s,String t){
int f=s.indexOf("fzgh");
int l =4;//length of "fzgh"
String w = s;
while(f!=-1){
w=w.substring(0,f)+t+w.substring(f+l,w.length());
f=w.indexOf("fzgh");
}
return w;
}
这将从 String s 中删除所有 "fzgh" 并将它们替换为 String t。
使用此代码 public class 运行 {
public static String mutation(String w,String s, String t){
String toBeReplaced = w;
int sizeOfThatString = toBeReplaced.length();
String[] array = s.split(" ");
for (int i = 0; i < array.length; i++) {
String tempString= array[i];
if(tempString.length()>sizeOfThatString) {
tempString = array[i].substring(0,sizeOfThatString);
System.out.println("TempString " +tempString);
}
if(tempString.equals(w)) {
array[i] = t;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
if(i!=array.length) {
sb.append(array[i]+" ");
}
else {
sb.append(array[i]);
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(mutation("fzgh","Hello. I want an fzgh. Give me an fzgh now.", "IPhone 6"));
}
}
输出:
- TempString 地狱
- TempString fzgh
- 您好。我想要一个 IPhone 6 现在给我一个 IPhone 6。