JAVA novice cool-test extracts substrings composed of the same characters, java cool-test

Source: Internet
Author: User

JAVA novice cool-test extracts substrings composed of the same characters, java cool-test

Given a string and a substring feature tag, you must extract all the substrings represented by the feature tag from the source string, such:

Given source string: AaaBbb, and sub-string feature mark AB, the sub-string aa and bb must be extracted from all the same characters that match the feature mark.

The JAVA code is as follows:

 1 import java.util.ArrayList; 2 import java.util.Scanner; 3  4 import static java.lang.System.*; 5 import java.lang.String; 6 public class FindSub 7 { 8     public static void main(String[] args) 9     {10         String src;11         String sublable;12         ArrayList<String> list=new ArrayList<String>();13 14         out.println("Please input the source string");15         Scanner input=new Scanner(System.in);16         src=input.nextLine();17 18         out.println("please input the lable of substring");19         sublable=input.nextLine();20 21         list=getSub(src, sublable);22 23         out.println("All substring is:");24         for(String s: list)25         {26             out.println(s);27         }28     }29 30     public static ArrayList<String> getSub(String src, String sublable)31     {32         int flag=0;33         int low=0, high=0;34         char sign='\u0000';35 36         ArrayList<String> list=new ArrayList<String>();37 38         for (int i=0; i<src.length(); i++)39         {40             if (sublable.contains(src.substring(i, i+1)))41             {42                 if (flag==0)43                 {44                     low=i;45                     high=i;46                     flag=1;47                     sign=src.charAt(i);48                 }49                 else50                 {51                     if (src.charAt(i)==sign)52                     {53                         high=i;54                     }55                     else56                     {57                         list.add(src.substring(low, high+1));58                         low=i;59                         high=i;60                         sign=src.charAt(i);61                     }62                 }63             }64             else65             {66                 if (flag==1)67                 {68                     list.add(src.substring(low, high+1));69                     flag=0;70                 }71             }72         }73         if (flag==1)74             list.add(src.substring(low, high+1));75 76         return list;77     }78 }

Running result

This program can be improved to print the starting and ending numbers of substrings in the source string while outputting the extracted substrings, so that different substrings with the same feature tags can be distinguished, such:

Source string AaaBaa feature flag

Output aa 1, 2

Aa 5, 6

If you are interested, try it yourself.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.