You must Sign In to post a response.
  • Category: .NET

    How to split a string by using common words in c#?

    Hi ,

    How to split a string by using common words in c#

    For ex: Below one is string
    "srkwersrktyusrkvbnsrkvfgsrkvbn"
    Form Below SRK is common string
    I need like array strings are
    srkwer,
    srktyu,
    srkvbn,
    srkvfg,
    srkvbn




    Thanks,
    Urmila
  • #765311
    Hi Urmila,

    I assume that srk is the fixed word to separate the string.

    If yes, then refer below sample code

    string ip="srkwersrktyusrkvbnsrkvfgsrkvbn";
    string[] strary= ip.Split('srk');

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #765312
    simple you can use string.split method see below snippet

    using System;

    class Program
    {
    static void Main()
    {
    string szCode = "srkwersrktyusrkvbnsrkvfgsrkvbn"
    // Split string on spaces.
    // ... This will separate all the words.
    string[] words = szCode .Split("srk");
    foreach (string word in words)
    {
    Console.WriteLine("srk" + word);
    }
    }
    }

    Hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765317
    Hello,

    Try the below code to do the split:


    string str = "srkwersrktyusrkvbnsrkvfgsrkvbn";
    string[] strs=str.Split(new string[] {"srk"},StringSplitOptions.None);
    for(int i=0; i < strs.Length; i++)
    {
    Response.Write("srk"+strs[i] + "<br>" );
    }


    Hope this will help you.

    Thank you.

    If it helps you then Rate this.

    Best Regards,
    Manoranjan Sahoo
    http://www.dotnetsquare.com


Sign In to post your comments