【问题标题】:Putting one string into another string java [duplicate]将一个字符串放入另一个字符串java [重复]
【发布时间】:2013-03-04 19:42:31
【问题描述】:

我正在尝试将一个字符串插入另一个字符串的中间,然后将Monkey 放入新字符串的中间。

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

基本上我要做的是将"Monkey" 多次插入"MonkeyPony" 的中间,所以第一次它会读取"MonkeMonkeyyPony",第二次它会读取"MonkeMonMonkeykeyyPony",等等。

【问题讨论】:

    标签: java string performance algorithm insert


    【解决方案1】:

    你可以试试这个:

    One = One.substr(0,One.length()/2)+Two+One.substr(One.length()/2+1, One.length();
    

    字符串连接中的第一个元素取字符串 One 的前半部分,然后与单词 Two 连接,然后添加其余部分。

    【讨论】:

    • 我想我用 c++ 给出了答案,但我不确定你使用的是 c++ 还是 java? java中存在类似的成员函数,你可以找到它们
    【解决方案2】:

    使用 StringBuilder:

    StringBuilder builder = new StringBuilder(One);
    for (int i = 0; i < 10; ++i) {
      builder.insert(builder.length() / 2, Two);
      System.out.println(builder.toString());
    }
    

    【讨论】:

      【解决方案3】:

      我不知道这个问题是来自你的实际工作还是家庭作业/面试算法问题。

      直接的解决方案是计算原始字符串的索引,将新字符串插入该位置。这样做 n 次。

      我只是写一个想法,不确定是否可以:假设我们要插入 100(n) 次

      • 首先我们取字符串二,Monkey
      • 构建两个字符串,一个是"Mon",重复99次(n-1),另一个是"key",同样重复99次
      • 找出字符串ONE的插入索引,比如i
      • 将此字符串插入i 位置:99Mons + String TWO("Monkey") + 99keys

      我不确定这是否是一个好方法......


      做了一个小测试:

      我对我的解决方案的性能很好奇,所以我用@Ame Burmeister 提供的简单解决方案做了一个小的性能测试。

      在我的测试中:

      • 使用Junit Test class 完成测试
      • StopWatch 来自番石榴
      • warming up 测量经过时间之前的 4 次,以避免 Jit
      • 这两种方法的输出相同(n 更小),均已正确实现。
      • 均使用 100000 插入测试

      简单的解决方案(基本上只是复制@Ame的答案)

      @Test
          public void insertWithBuilder() {
              final int n = 100000;
              final String one = "MonkeyPony";
              final String two = "Monkey";
      
              final Stopwatch sw = new Stopwatch();
              int x = 1;
              for (; x <= 5; x++) {// loop 4 times (warming up) to avoid JIT
                  if (x == 5) {
                      sw.start();
                  }
                  final StringBuilder builder = new StringBuilder(one);
                  System.out.println("warming up times:" + x);
                  for (int i = 0; i < n; ++i) {
                      builder.insert(builder.length() / 2, two);
                  }
                  if (x == 5) {
                      sw.stop();
                      // System.out.println("builder:" + builder.toString());
                  }
              }
              System.out.println("SBuilder:" + sw.elapsedTime(TimeUnit.MILLISECONDS));
          }
      

      我的想法的实现:

      @Test
          public void insertWithRepeatString() {
              final int n = 100000;
              final String one = "MonkeyPony";
              final String two = "Monkey";
      
              final Stopwatch sw = new Stopwatch();
              int x = 1;
              for (; x <= 5; x++) { // loop 4 times (warming up) to avoid JIT
                  System.out.println("warming up times:" + x);
                  if (x == 5) {
                      sw.start();
                  }
                  final StringBuilder builder = new StringBuilder(one);
                  final int m = two.length() / 2;
                  final String h1 = two.substring(0, m); //get Mon
                  final String r1 = two.substring(m);    //get Key
                  final String head = new String(new char[n - 1]).replace("\0", h1); //build repeat string
                  final String tail = new String(new char[n - 1]).replace("\0", r1); //build repeat String
                  final StringBuilder builder2 = new StringBuilder(head);
                  builder2.append(two).append(tail);
                  builder.insert(builder.length() / 2, builder2);
                  if (x == 5) {
                      sw.stop();
                      // System.out.println("builder:" + builder.toString());
                  }
              }
              System.out.println("Idea:" + sw.elapsedTime(TimeUnit.MILLISECONDS));
          }
      

      输出:

      warming up times:1
      warming up times:2
      warming up times:3
      warming up times:4
      warming up times:5
      Idea:41
      warming up times:1
      warming up times:2
      warming up times:3
      warming up times:4
      warming up times:5
      SBuilder:4413 
      

      【讨论】:

      • 不知道该怎么做,我觉得有一种更简单的方法可以做到这一点,但我不太确定。也许计算字符串长度,除以 2,然后在该位置插入新字符串?我不是最擅长 java,所以我不确定该功能是否存在。
      • @user2150807 我添加了一个性能测试,以与 Arne Burmeister 的直接解决方案进行比较。结果证明有很大的不同。我希望我做对了。
      • 当然我的方法比较慢。您的想法优雅而快速,但并不容易理解。问题来自 Rep 11,所以我想了一个简单的解决方案 ;-)
      • @ArneBurmeister 正如我所说,我不知道问题出在哪里。如果是面试问题,我会像我的回答一样回答。但是,如果这是实际项目中的问题,并且n 不是那么大,我会和你的一样。除非这个方法会被调用一千次。
      【解决方案4】:

      此解决方案可能很简单,但与其他解决方案相比,它会消耗大量时间。
      这里使用了substring方法。

      import java.util.Scanner;
      
      public class test {
      
           public static void main(String[] args) {
      
               int n=0;
               String str1,str2=null;
      
      
               Scanner in = new Scanner(System.in);
      
               System.out.println("Enter the String1.!");
               str1=in.nextLine();
      
               System.out.println("Enter the String2.!");
               str2=in.nextLine();
      
               System.out.println("Enter the Count.!"); 
               n=Integer.parseInt(in.nextLine());
      
               in.close();
      
               for(int i=0;i<n;i++)
               {
                   //Concept: Say str1=TEST str2=123, Now the below code will do this TE + 123 + ST 
                   str1=str1.substring(0,str1.length()/2) + str2 + str1.substring(str1.length()/2);
                   System.out.println(str1);
               }
      
      
              }
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-11
        • 1970-01-01
        • 2011-08-31
        • 2012-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多