1. <i id='vPPL5'><tr id='vPPL5'><dt id='vPPL5'><q id='vPPL5'><span id='vPPL5'><b id='vPPL5'><form id='vPPL5'><ins id='vPPL5'></ins><ul id='vPPL5'></ul><sub id='vPPL5'></sub></form><legend id='vPPL5'></legend><bdo id='vPPL5'><pre id='vPPL5'><center id='vPPL5'></center></pre></bdo></b><th id='vPPL5'></th></span></q></dt></tr></i><div id='vPPL5'><tfoot id='vPPL5'></tfoot><dl id='vPPL5'><fieldset id='vPPL5'></fieldset></dl></div>
      <legend id='vPPL5'><style id='vPPL5'><dir id='vPPL5'><q id='vPPL5'></q></dir></style></legend>

      <tfoot id='vPPL5'></tfoot>

        <bdo id='vPPL5'></bdo><ul id='vPPL5'></ul>

      1. <small id='vPPL5'></small><noframes id='vPPL5'>

        替换字符串中的字符的有效方法(java)?

        时间:2023-07-28

          <tfoot id='x7tmK'></tfoot>

        1. <i id='x7tmK'><tr id='x7tmK'><dt id='x7tmK'><q id='x7tmK'><span id='x7tmK'><b id='x7tmK'><form id='x7tmK'><ins id='x7tmK'></ins><ul id='x7tmK'></ul><sub id='x7tmK'></sub></form><legend id='x7tmK'></legend><bdo id='x7tmK'><pre id='x7tmK'><center id='x7tmK'></center></pre></bdo></b><th id='x7tmK'></th></span></q></dt></tr></i><div id='x7tmK'><tfoot id='x7tmK'></tfoot><dl id='x7tmK'><fieldset id='x7tmK'></fieldset></dl></div>

            • <bdo id='x7tmK'></bdo><ul id='x7tmK'></ul>

                <tbody id='x7tmK'></tbody>
                  <legend id='x7tmK'><style id='x7tmK'><dir id='x7tmK'><q id='x7tmK'></q></dir></style></legend>
                • <small id='x7tmK'></small><noframes id='x7tmK'>

                • 本文介绍了替换字符串中的字符的有效方法(java)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在编写一个小的 JAVA 程序:

                  I'm writing a small JAVA program which:

                  • 将文本作为字符串
                  • 需要 2 个字符数组

                  我尝试做的事情听起来像是查找和替换",但它不一样,所以我认为清除它很重要.

                  What im trying to do will sound like "find and replace" but it is not the same so i thought its important to clear it.

                  无论如何,我想获取此文本,查找第一个数组中的任何字符是否与文本中的字符匹配,如果是,则将其替换为第二个字符数组中的匹配字符(根据索引).

                  Anyway I want to take this text, find if any char from the first array match a char in the text and if so, replace it with the matching char (according to index) from the second char array.

                  我会用一个例子来解释:假设我的文本(字符串)是:java 很棒!";我有 2 个数组(char[]):absm"和!@*$".

                  I'll explain with an example: lets say my text (String) is: "java is awesome!"; i have 2 arrays (char[]): "absm" and "!@*$".

                  希望的结果是将 'a' 更改为 '!', 'b' 到 '@' 等等..意味着结果文本将是:

                  The wished result is to change 'a' to '!' , 'b' to '@' and so on.. meaning the resulted text will be:

                  java 太棒了!"改为 -> "j i* @w*o$e!"

                  最有效的方法是什么?为什么?我想过循环文本,但后来发现效率不高.

                  What is the most efficient way of doing this and why? I thought about looping the text, but then i found it not so efficient.

                  (StringBuilder/可以使用String类)

                  (StringBuilder/String class can be used)

                  推荐答案

                  StringBuilder sb = new StringBuilder(text);
                      for(int i = 0; i<text.length(); i ++)
                      {
                          for (int j = 0; j < firstCharArray.length;j++)
                          {
                              if (sb.charAt(i) == firstCharArray[j])
                              {
                                  sb.setCharAt(i, secondCharArray[j]);
                                  break;
                              }
                  
                          }
                      }
                  

                  这种方式很有效,因为它使用 StringBuilder 来更改字符(如果您使用字符串,则每次都必须创建新的,因为它们是不可变的.)而且它还最大限度地减少了您必须执行的传递次数(1 传递文本字符串,n 传递第一个数组,其中 n = text.length())

                  This way is efficient because it uses a StringBuilder to change the characters in place (if you used Strings you would have to create new ones each time because they are immutable.) Also it minimizes the amount of passes you have to do (1 pass through the text string and n passes through the first array where n = text.length())

                  这篇关于替换字符串中的字符的有效方法(java)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Java:带有char数组的println给出乱码 下一篇:为什么我的 char 打印为数字而不是字符?

                  相关文章

                  最新文章

                • <i id='JCp7c'><tr id='JCp7c'><dt id='JCp7c'><q id='JCp7c'><span id='JCp7c'><b id='JCp7c'><form id='JCp7c'><ins id='JCp7c'></ins><ul id='JCp7c'></ul><sub id='JCp7c'></sub></form><legend id='JCp7c'></legend><bdo id='JCp7c'><pre id='JCp7c'><center id='JCp7c'></center></pre></bdo></b><th id='JCp7c'></th></span></q></dt></tr></i><div id='JCp7c'><tfoot id='JCp7c'></tfoot><dl id='JCp7c'><fieldset id='JCp7c'></fieldset></dl></div>
                    1. <small id='JCp7c'></small><noframes id='JCp7c'>

                      <legend id='JCp7c'><style id='JCp7c'><dir id='JCp7c'><q id='JCp7c'></q></dir></style></legend>

                        <bdo id='JCp7c'></bdo><ul id='JCp7c'></ul>

                    2. <tfoot id='JCp7c'></tfoot>