如何使用charAt和string.length()来分割字符串。

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

how to split a string by using charAt and string.length()

问题

只允许使用charAt方法和length方法。非常感谢!

void runApp() {
  String str = "345, 688"; //-> "345" "688"
  String value = strCut(str);
}

String strCut(String str) {
    int result = 0;

    for (int i = 0; i < str.length(); i++) {
      if (str.charAt(3) == ',') {
        在这里应该写什么
      }
英文:

only allow charAt method and length method . Thank you so much!

void runApp() {
  String str = &quot;345, 688&quot;; //-&gt;&quot;345&quot; &quot;688&quot;
  String value = strCut(str);
}

String strCut(String str) {
    int result = 0;

    for (int i = 0; i &lt; str.length(); i++) {
      if (str.charAt(3) == &#39;,&#39;) {
        what should i write here ? ?
      }

答案1

得分: 4

你的代码需要一些重构,尝试使用这个:

void runApp() {
    String str = "345, 688"; //->"345" "688"
    String value = strCut(str);
}

String strCut(String str) {
    int cutStringIndex = 0;

    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) == ',') {
            cutStringIndex = i;
            break;
        }
    }

    String cutStringOne = "";
    for (int i = 0; i < cutStringIndex; i++) {
        cutStringOne += str.charAt(i);
    }

    String cutStringTwo = "";
    for (int i = cutStringIndex + 1; i < str.length(); i++) {
        cutStringTwo += str.charAt(i);
    }

    String cutString = cutStringOne + " " + cutStringTwo;
    return cutString;
}

这将去除逗号,这似乎是你要寻找的。我只使用了你所要求的两种方法。基本上,这段代码获取了逗号的索引,然后重新构造了字符串的两个部分,直到达到逗号的位置,并跳过了逗号。根据你的情况,可能需要进行一些微调,但这应该是你所需要的内容。

英文:

Your code needs some refactoring, try this:

void runApp() {
 String str = &quot;345, 688&quot;; //-&gt;&quot;345&quot; &quot;688&quot;
 String value = strCut(str);
}

String strCut(String str) {
int result = 0;

for (int i = 0; i &lt; str.length(); i++) {
  int cutStringIndex;
  if (str.charAt(i) == &#39;,&#39;) {
       cutStringIndex = i;
  }
  for (int i = 0; i &lt; cutStringIndex(); i++) {
  String cutStringOne = &quot;&quot;;
  cutStringOne = cutStringOne + str.charAt(i);
  }
  for (int i = cutStringIndex() + 1; i &lt; str.length(); i++) {
  String cutStringTwo = &quot;&quot;;
  cutStringTwo = cutStringTwo + str.charAt(i);
  }
  cutString = cutStringOne + &quot; &quot; + cutStringTwo;
 return cutString;
 }

This will take out the comma which appears to be what you were looking for. I only used the two methods you asked for. Essentially this code gets the index of the comma, then reconstructs the two parts of the strings until it reaches the point of the comma, and skips over it. It may need some minor tweaks for your situation but this should be what you're looking for.

答案2

得分: 3

以下是翻译好的部分:

可以像这样完成,假设字符串 s = "200,300,450,600",并且您必须使用 charAt()string.length() 方法拆分给定的字符串,然后首先像下面的代码中所示,在字符串末尾添加 ','。

String s = "200,300,450,600,", str = "";
for (int i = 0; i < s.length(); i++) {
    char ch = s.charAt(i);
    if (ch != ',') { // 检查特定字符是否不是 ','
        str += ch; // 存储在 str 字符串中
    } else {
        System.out.println(str); // 在找到 ',' 之前打印每个字符串
        str = "";
    }
}

上述代码的输出将是:
200
300
450
600(所有数字将在下一行打印)

英文:

It can be done like this, Suppose String s="200,300,450,600" and you have to split given string using charAt() and string.length() method then first add ',' at the end of the string as given in the code below.

String s=&quot;200,300,450,600,&quot;,str=&quot;&quot;;
		for(int i=0;i&lt;s.length();i++){
		    char ch=s.charAt(i); 
		    if(ch!=&#39;,&#39;){ //checking if particular character is not &#39;,&#39;
		       str+=ch; //storing it in str string
		    }
		    else{
		       System.out.println(str); //printing each string before &#39;,&#39; is found
		       str=&quot;&quot;;
		    }
		}

The output of above code will be:200
300
450
600(all the numbers will be printed on next line)

答案3

得分: 2

如果您只想使用charAt和string.length(),那么您可以尝试这样做:

void runApp{
    String str = "345, 688, 123";
    String values[] = strCut(str); //values[0] = 345, values[1] = 688, values[2] = 123
    for(String value : values){
        System.out.print(value + " ");
    }
}

String[] strCut(String str) {
    int elements = 1;
    int index = 0;
    
    for(int i = 0; i < str.length(); i++){
        if(str.charAt(i) == ',')
            elements++;
    }
    
    String result[] = new String[elements];
    for(int i = 0; i < result.length; i++)
        result[i] = "";
        
    for (int i = 0; i < str.length(); i++) {
        if (str.charAt(i) != ',') {
            if(str.charAt(i) != ' ')
                result[index] = result[index] + str.charAt(i);
        } 
        else index++;
    }
    return result;
}
英文:

If you want to use only charAt and string.length() then you should try this

void runApp{
    String str = &quot;345, 688, 123&quot;;
    String values[] = strCut(str); //values[0] = 345, values[1] = 688, values[2] = 123
    for(String value : values){
        System.out.print(value + &quot; &quot;);
    }
}


 String[] strCut(String str) {
    int elements = 1;
    int index = 0;
    
    for(int i = 0; i &lt; str.length(); i++){
        if(str.charAt(i) == &#39;,&#39;)
            elements++;
    }
    
    String result[] = new String[elements];
    for(int i = 0; i &lt; result.length; i++)
        result[i] = &quot;&quot;;
        
    for (int i = 0; i &lt; str.length(); i++) {
      if (str.charAt(i) != &#39;,&#39;) {
          if(str.charAt(i) != &#39; &#39;)
              result[index] = result[index] + str.charAt(i);
      } 
      else index++;
      
    }
    return result;
}

答案4

得分: 1

public class Main {
    public static void main(String[] args) {
        // Test
        runApp();
    }

    static void runApp() {
        String str = "345, 688"; // 期望结果:"345" "688"
        String value = strCut(str);
        System.out.println(value); // 显示结果
    }

    static String strCut(String str) {
        // 用双引号初始化结果
        String result = "\"";

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ',') { // 检查索引i处的字符
                // 在一个数字末尾添加",然后在下一个数字前再次添加"
                result += "\" \"";
            } else if (str.charAt(i) != ' ') {
                result += str.charAt(i);
            }
        }

        // 在末尾添加"
        result += "\"";

        // 最终返回结果
        return result;
    }
}

输出:

"345" "688"
英文:

You can do it as follows:

public class Main {
	public static void main(String[] args) {
		// Test
		runApp();
	}

	static void runApp() {
		String str = &quot;345, 688&quot;; // Expected-&gt;&quot;345&quot; &quot;688&quot;
		String value = strCut(str);
		System.out.println(value);// Display the result
	}

	static String strCut(String str) {
		// Initialise result with a &quot;
		String result = &quot;\&quot;&quot;;

		for (int i = 0; i &lt; str.length(); i++) {
			if (str.charAt(i) == &#39;,&#39;) {// Check char at the index, i
				// Add &quot; at the end of one number and again &quot; at the start of the next
				result += &quot;\&quot; \&quot;&quot;;
			} else if (str.charAt(i) != &#39; &#39;) {
				result += str.charAt(i);
			}
		}

		// Add &quot; at the end
		result += &quot;\&quot;&quot;;

        // Finally, return result
		return result;
	}
}

Output:

&quot;345&quot; &quot;688&quot;

答案5

得分: 0

如果你必须要使用 charAt() 方法,那么可以像下面这样做:

ArrayList<String> stringArr = new ArrayList<String>();
int startindex = 0;
for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) == ',') {
        String partString = str.substring(startindex, i);
        startindex = i + 1;
        stringArr.add(partString);
    }
}
String lastString = str.substring(startindex, str.length());
stringArr.add(lastString);

或者你可以简单地使用 split 方法,像下面这样:

String[] parts = string.split(",");
String part1 = parts[0]; // 345
String part2 = parts[1]; // 688

请注意,这是你提供的代码的翻译部分。

英文:

if you must want to make use of charAt() then do like below..

    ArrayList&lt;String&gt; stringArr= new ArrayList&lt;String&gt;();
    int startindex=0;
    for (int i = 0; i &lt; str.length(); i++) 
{
   
      if (str.charAt(i) == &#39;,&#39;)
    {
         String partString = str.substring(startindex, i) ;
          startindex=i+1;

          stringArr.add(partString);
    }
    
        }

        String lastString = str.substring(startindex, str.length()) ;
    stringArr.add(lastString);

OR
You can simply use split method like below

String[] parts = string.split(&quot;,&quot;);
String part1 = parts[0]; // 345
String part2 = parts[1]; // 688

答案6

得分: 0

你可以通过简单地执行这个步骤来实现它,
这将为您提供所期望的结果。

String str = "345,688";
ArrayList<String> stringArray = new ArrayList<>();
int startindex=0;
for (int i = 0; i < str.length(); i++) {
    if(str.charAt(i) == ',') {
        String subStr = str.substring(startindex, i);
        startindex = i+1;
        stringArray.add(subStr);
    }
}
stringArray.add(str.substring(startindex));
英文:

You can achieve it by simply doing this,
This will give you the desired result.

String str = &quot;345,688&quot;;
ArrayList&lt;String&gt; stringArray = new ArrayList&lt;&gt;();
int startindex=0;
for (int i = 0; i &lt; str.length(); i++) {
    if(str.charAt(i) == &#39;,&#39;) {
        String subStr = str.substring(startindex, i);
        startindex = i+1;
        stringArray.add(subStr);
    }
}
stringArray.add(str.substring(startindex));

huangapple
  • 本文由 发表于 2020年10月13日 15:30:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64330554.html
匿名

发表评论

匿名网友

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

确定