返回由字符串奇数索引位置的字符组成的字符串

huangapple go评论98阅读模式
英文:

Returning a string made by odd index number of the string

问题

  1. public class MeWhileLoop
  2. {
  3. public int a, b;
  4. public String str;
  5. public String OddNumChar(){
  6. int index = 0;
  7. str = "Poland";
  8. String result = ""; // Initialize an empty string to store the result
  9. while(index < str.length()){
  10. if(index % 2 != 0){ // Check if the index is odd
  11. result += str.substring(index, index+1); // Append the odd-indexed character to the result
  12. }
  13. index++;
  14. }
  15. return result; // Return the string containing odd-indexed characters
  16. }
  17. }
英文:

The Return type is a String and no Input parameters. I have to go through the instance variable called str and return a string that is put together by odd index number of the string.

i forgot to mention that it has to be a while loop

Example: str = "Poland"

  1. then the method should return &quot;oad&quot; because P is an even number, o is odd, l is even, a is odd, n is even, d is odd.

I so far have come up with this

  1. public class MeWhileLoop
  2. {
  3. public int a, b;
  4. public String str;
  5. public String OddNumChar(){
  6. int index = 0;
  7. str = &quot;Poland&quot;;
  8. while(index &lt; str.length()){
  9. System.out.println(str.substring(index, index+1));
  10. index++;
  11. }
  12. System.out.println();
  13. return str;
  14. }
  15. }

I'm just stuck because the index+1 won't take out the odd letters or any letter at all and I have no clue why.

答案1

得分: 3

以下是翻译好的内容:

第一段代码:

  1. 如何考虑
  2. String res = "";
  3. // 从第二个字符开始,然后每次增加2
  4. for (int i = 1; i < str.length(); i += 2) {
  5. res += str.charAt(i);
  6. }
  7. return res;

第二段代码:

  1. 对于更大的字符串这可能具有更好的性能
  2. StringBuilder sb = new StringBuilder(str.length() / 2);
  3. for (int i = 1; i < str.length(); i += 2) {
  4. sb.append(str.charAt(i));
  5. }
  6. return sb.toString();

第三段代码:

  1. 如果出于某种原因确实需要使用 while 循环
  2. String res = "";
  3. int i = 1;
  4. while (i < str.length()) {
  5. res += str.charAt(i);
  6. i += 2;
  7. }
  8. return res;
英文:

How about:

  1. String res = &quot;&quot;;
  2. // start at second character and then increment by 2
  3. for (int i = 1; i &lt; str.length(); i += 2) {
  4. res += str.charAt(i);
  5. }
  6. return res;

This might have better performance for larger strings:

  1. StringBuilder sb = new StringBuilder(str.length() / 2);
  2. for (int i = 1; i &lt; str.length(); i += 2) {
  3. sb.append(str.charAt(i));
  4. }
  5. return sb.toString();

If you really need while loop for some reason:

  1. String res = &quot;&quot;;
  2. int i = 1;
  3. while (i &lt; str.length()) {
  4. res += str.charAt(i);
  5. i += 2;
  6. }
  7. return res;

huangapple
  • 本文由 发表于 2020年9月26日 04:30:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64071001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定