I want to split string without using split functio

2020-04-16 17:33发布

I want to split string without using split . can anybody solve my problem I am tried but I cannot find the exact logic.

标签: java
15条回答
▲ chillily
2楼-- · 2020-04-16 18:12

Use String tokenizer to split strings in Java without split:

import java.util.StringTokenizer;

public class tt {
    public static void main(String a[]){
        String s = "012ab567ab0123ab";
        String delims = "ab ";
        StringTokenizer st = new StringTokenizer(s, delims);
        System.out.println("No of Token = " + st.countTokens());
        while (st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }
    }
}
查看更多
Ridiculous、
3楼-- · 2020-04-16 18:14

Since this seems to be a task designed as coding practice, I'll only guide. No code for you, sir, though the logic and the code aren't that far separated.

You will need to loop through each character of the string, and determine whether or not the character is the delimiter (comma or semicolon, for instance). If not, add it to the last element of the array you plan to return. If it is the delimiter, create a new empty string as the array's last element to start feeding your characters into.

查看更多
【Aperson】
4楼-- · 2020-04-16 18:16

This is the right answer

import java.util.StringTokenizer;

public class tt {
    public static void main(String a[]){
    String s = "012ab567ab0123ab";

    String delims = "ab ";

    StringTokenizer st = new StringTokenizer(s, delims);
            System.out.println("No of Token = " + st.countTokens());

             while (st.hasMoreTokens())
             {
                 System.out.println(st.nextToken());
             }

     }

}
查看更多
别忘想泡老子
5楼-- · 2020-04-16 18:18

you can try, the way i did `{

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();

    for(int i = 0; i <str.length();i++) {
        if(str.charAt(i)==' ') { // whenever it found space it'll create separate words from string
            System.out.println();
            continue;
        }
        System.out.print(str.charAt(i));
    }
    sc.close();
}` 
查看更多
小情绪 Triste *
6楼-- · 2020-04-16 18:19

You can do it using Java standard libraries.

Say the delimiter is : and

String s = "Harry:Potter"
int a = s.find(delimiter);

and then add

s.substring(start, a)

to a new String array.

Keep doing this till your start < string length

Should be enough I guess.

查看更多
一夜七次
7楼-- · 2020-04-16 18:26

Split a string without using split()

static String[] splitAString(String abc, char splitWith){
    char[] ch=abc.toCharArray();
    String temp="";
    int j=0,length=0,size=0;
    for(int i=0;i<abc.length();i++){
        if(splitWith==abc.charAt(i)){
            size++;
        }
    }
    String[] arr=new String[size+1];
    for(int i=0;i<ch.length;i++){
        if(length>j){
            j++;
            temp="";
        }
        if(splitWith==ch[i]){
            length++;
        }else{
            temp +=Character.toString(ch[i]);          
        }
    arr[j]=temp;
    }
    return arr;
}
public static void main(String[] args) {

    String[] arr=splitAString("abc-efg-ijk", '-');

    for(int i=0;i<arr.length;i++){
        System.out.println(arr[i]);
    }
}

}

查看更多
登录 后发表回答