简体   繁体   中英

How can i split string with number periods also, commas

I have string like below

1. menggambarkan , mengecat , melukis;

2. menyalin , membuat; 

3. memotret , film , menjepret

How can I split them so the string became array of string the result will be like

menggambarkan
mengecat
melukis 
menyalin 
membuat
memotret 
film
menjepret

below is what i have already tried

input.Split(new string[] {  " , "}, StringSplitOptions.None)

but how can i remove number with dots like 1. 2. 3. and sign;

I think we can use regex for the solution, do u have any idea?

RegEx approach to excract all words

string input = "1. menggambarkan , mengecat , melukis ; 2. menyalin , membuat; 3. memotret , film , menjepret";
string[] result = Regex.Matches(input, "[a-z]+").Cast<Match>().Select(x => x.Value).ToArray();

https://dotnetfiddle.net/mHf1eu

For example:

var regex = new Regex(@"^\d+\.\s*");
var s = "1. menggambarkan , mengecat , melukis ; 2. menyalin , membuat; 3. memotret , film , menjepret";
var words = s.Split(new char[]{',', ';'}).Select(w => regex.Replace(w.Trim(), string.Empty));

Something along this line. You have to remove the matched empty line.

using System;
using System.Text.RegularExpressions;  

public class Program
{
    public static void Main()
    {
        string URL = " 1. menggambarkan , mengecat , melukis ; 2. menyalin , membuat ; 3. memotret , film , menjepret";

        string[] splitString = Regex.Split(URL, @"[(\s;\s\d.\s)(\s\d.\s)(\s,\s)]");  

        foreach (var item in splitString)  
        {  
            Console.WriteLine(item);  
        } 
    }
}

Also you can use a Linq

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var str = "1. menggambarkan , mengecat , melukis ; 2. menyalin , membuat; 3. memotret , film , menjepret";
        str = new string((from c in str
                  where char.IsWhiteSpace(c) || char.IsLetter(c)
                  select c
       ).ToArray());
        Console.WriteLine(str);
        string[] array = str.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries);
        Console.WriteLine(array.Count());
    }
}

Output

menggambarkan  mengecat  melukis   menyalin  membuat  memotret  film  menjepret
8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM