koby  wan
koby wan

Reputation: 15

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

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

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 ? ?
      }

Upvotes: -1

Views: 360

Answers (6)

Riya Singh
Riya Singh

Reputation: 31

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="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="";
            }
        }

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

Upvotes: 3

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79435

You can do it as follows:

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

Output:

"345" "688"

Upvotes: 1

Trushit Shekhda
Trushit Shekhda

Reputation: 561

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

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

Upvotes: 0

Ravindra Bagale
Ravindra Bagale

Reputation: 17665

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

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

OR You can simply use split method like below

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

Upvotes: 0

ABC
ABC

Reputation: 655

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

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

Upvotes: 2

CobaltGecko
CobaltGecko

Reputation: 228

Your code needs some refactoring, try this:

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

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.

Upvotes: 4

Related Questions