简体   繁体   中英

Insert string randomly into another string

Does anyone have a good method for inserting all items from a list randomly into another string? For example if I have paragraph of text and I want to insert urls randomly after sentences. The Urls are stored in a list object. How would I go about doing this? I was thinking I could count the number of periods and other punctuation, but I would somehow have to store the location of each period as well. If anyone could point me in the right direction i would appreciate it.

I think the logic is as follows:

count periods, store period indexes in list, get random index from list, insert text after that index by adding 1 to the index

Does this work for you?

var rnd = new Random();
var urls = new [] { "www.a.com", "www.b.com" };
var text = "I like apples. I like bananas.";

var query =
    from t in text.Split(new [] { '.' }, StringSplitOptions.RemoveEmptyEntries)
    from x in new [] { t, ". ", urls[rnd.Next(urls.Length)] }
    select x;

var result = String.Join("", query);

I like apples. www.b.com I like bananas. www.a.com

Here you go.

Module Module1
    ' String to insert into
    Const target As String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse vel posuere neque, quis suscipit diam. Proin congue dapibus elit, ut tempus leo. Suspendisse suscipit mi quis purus laoreet facilisis. Sed eget arcu eu risus sodales posuere. Nulla ut nulla urna. Quisque eu eleifend metus. Mauris suscipit rhoncus eros at ultrices. Cras sodales facilisis mauris, vitae condimentum mi tempus a. Suspendisse hendrerit, arcu sit amet viverra placerat, eros quam venenatis enim, id tincidunt felis mauris id augue. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur ac ullamcorper sem, sed porta orci. Praesent euismod, ipsum et sagittis gravida, purus ipsum venenatis ipsum, quis congue risus erat in magna. Donec nec elit ut velit faucibus efficitur. Donec facilisis, tellus et volutpat hendrerit, dui sem pulvinar lectus, sit amet fringilla sem mi eget elit."
    ' Urls to insert
    Private injections As String() = New String() {"http://lorempixel.com/output/cats-q-c-640-480-1.jpg", "http://lorempixel.com/output/cats-q-c-640-480-2.jpg", "http://lorempixel.com/output/cats-q-c-640-480-3.jpg", "http://lorempixel.com/output/cats-q-c-640-480-4.jpg"}

    Sub Main()
        Dim sentence As New System.Text.RegularExpressions.Regex("[^.!?]+[.!?]")
        Dim result As New System.Text.StringBuilder()
        Dim n As Integer = injections.Count()
        ' Break sentence at punctuation.
        For Each m As System.Text.RegularExpressions.Match In sentence.Matches(target)
            ' Get index of one of our injection strings at random.
            Dim i As Integer = CInt(Math.Ceiling(Rnd() * n)) - 1
            result.Append(m).Append(injections(i))
        Next
        Console.WriteLine(result.ToString())
        Console.ReadKey()
    End Sub

End Module

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