如何使用charAt和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) == ',') {
what should i write here ? ?
}

您的代码需要一些重构,请尝试以下操作:

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++) {
int cutStringIndex;
if (str.charAt(i) == ',') {
cutStringIndex = i;
}
for (int i = 0; i < cutStringIndex(); i++) {
String cutStringOne = "";
cutStringOne = cutStringOne + str.charAt(i);
}
for (int i = cutStringIndex() + 1; i < str.length(); i++) {
String cutStringTwo = "";
cutStringTwo = cutStringTwo + str.charAt(i);
}
cutString = cutStringOne + " " + cutStringTwo;
return cutString;
}

这将去掉逗号,该逗号似乎是您要查找的内容。我只使用了你要求的两种方法。从本质上讲,这段代码获取逗号的索引,然后重建字符串的两个部分,直到它到达逗号的点,然后跳过它。它可能需要根据您的情况进行一些小的调整,但这应该是您想要的。

可以这样做,假设字符串s="200300450600";并且您必须使用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!=','){ //checking if particular character is not ','
str+=ch; //storing it in str string
}
else{
System.out.println(str); //printing each string before ',' is found
str="";
}
}

上述代码的输出将为:200300450>600(所有数字将打印在下一行(

如果你只想使用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;
}

您可以按如下方式执行:

public class Main {
public static void main(String[] args) {
// Test
runApp();
}
static void runApp() {
String str = "345, 688"; // Expected->"345" "688"
String value = strCut(str);
System.out.println(value);// Display the result
}
static String strCut(String str) {
// Initialise result with a "
String result = """;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ',') {// Check char at the index, i
// Add " at the end of one number and again " at the start of the next
result += "" "";
} else if (str.charAt(i) != ' ') {
result += str.charAt(i);
}
}
// Add " at the end
result += """;
// Finally, return result
return result;
}
}

输出:

"345" "688"

如果您必须要使用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

只需执行此操作即可实现,这会给你想要的结果。

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));

最新更新