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条回答
我想做一个坏孩纸
2楼-- · 2020-04-16 18:28
public class WithoutSpit_method {
    public static void main(String arg[])
    {

       char[]str;
       String s="Computer_software_developer_gautam";
       String s1[];
       for(int i=0;i<s.length()-1;)
       {
       int lengh=s.indexOf("_",i); 
       if(lengh==-1)
       {
           lengh=s.length();
       }
       System.out.print(" "+s.substring(i,lengh));
       i=lengh+1;
       }

    }
}

Result: Computer software developer gautam

查看更多
做自己的国王
3楼-- · 2020-04-16 18:30

You do now that most of the java standard libraries are open source

In this case you can start here

查看更多
聊天终结者
4楼-- · 2020-04-16 18:30

The logic is: go through the whole string starting from first character and whenever you find a space copy the last part to a new string.. not that hard?

查看更多
何必那么认真
5楼-- · 2020-04-16 18:31

Here is my way of doing with Scanner;

import java.util.Scanner;

public class spilt {

    public static void main(String[] args)
    {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter the String to be Spilted : ");
         String st = input.nextLine();
         Scanner str = new Scanner(st);
         while (str.hasNext())
         {
             System.out.println(str.next());
         }


    }

}

Hope it Helps!!!!!

查看更多
Ridiculous、
6楼-- · 2020-04-16 18:32
/**
 * My method split without javas split.
 * Return array with words after mySplit from two texts;
 * Uses trim.
 */

public class NoJavaSplit {

    public static void main(String[] args) {
        String text1 = "Some text for example   ";
        String text2 = "   Second sentences     ";
        System.out.println(Arrays.toString(mySplit(text1, text2)));
    }

    private static String [] mySplit(String text1, String text2) {
        text1 = text1.trim() + " " + text2.trim() + " ";
        char n = ' ';
        int massValue = 0;
        for (int i = 0; i < text1.length(); i++) {
            if (text1.charAt(i) == n) {
                massValue++;
            }
        }
        String[] splitArray = new String[massValue];
        for (int i = 0; i < splitArray.length; ) {
            for (int j = 0; j < text1.length(); j++) {
                if (text1.charAt(j) == n) {
                    splitArray[i] = text1.substring(0, j);
                    text1 = text1.substring(j + 1, text1.length());
                    j = 0;
                    i++;
                }
            }
            return splitArray;
        }
        return null;
    }
}
查看更多
聊天终结者
7楼-- · 2020-04-16 18:32

The way to go is to define the function you need first. In this case, it would probably be:

String[] split(String s, String separator)

The return type doesn't have to be an array. It can also be a list:

List<String> split(String s, String separator)

The code would then be roughly as follows:

  1. start at the beginning
  2. find the next occurence of the delimiter
  3. the substring between the end of the previous delimiter and the start of the current delimiter is added to the result
  4. continue with step 2 until you have reached the end of the string

There are many fine points that you need to consider:

  • What happens if the string starts or ends with the delimiter?
  • What if multiple delimiters appear next to each other?
  • What should be the result of splitting the empty string? (1 empty field or 0 fields)
查看更多
登录 后发表回答