如何将一个字符串插入另一个字符串的中间? [英] How to insert one string into the middle of another string?

查看:236
本文介绍了如何将一个字符串插入另一个字符串的中间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个字符串插入另一个字符串的中间,例如:

I am trying to stick one string into the middle of another string, ex:

String One = "MonkeyPony";
String Two = "Monkey";

我该如何将String 2放入String One中,这样它会读取类似MonkeMonkeyyPony的内容?

How would I put String Two into String One so it would read something like MonkeMonkeyyPony?

我想做的是多次将"Monkey"插入"MonkeyPony"的中间,因此第一次将其读为"MonkeMonkeyyPony",第二次将其读为"MonkeMonMonkeykeyyPony",依此类推

What I'm trying to do is insert "Monkey" into the middle of "MonkeyPony" numerous times, so on the first time it would read "MonkeMonkeyyPony", and on the second time it would read "MonkeMonMonkeykeyyPony", etc.

推荐答案

您必须将第一个字符串的两个子字符串连接到第二个字符串的末尾.

You have to concat two substrings of the first string onto the ends of the second.

// put the marble in the bag
public static String insert(String bag, String marble, int index) {
    String bagBegin = bag.substring(0,index);
    String bagEnd = bag.substring(index);
    return bagBegin + marble + bagEnd;
}

这篇关于如何将一个字符串插入另一个字符串的中间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆