super9
super9

Reputation: 30111

String manipulation in C#. Inserting "+" between words

This is as far as I have got. My thinking was to stuff the string into an array, format it and recast it into a string.

    //input search term
    Console.WriteLine("What is your search query?:");
    string searchTerm = Console.ReadLine();

    //stuff the search term into an array to split it out
    string separator = " "; //assumes search terms are separated by spaces
    string[] searchTermArray = searchTerm.Split(separator.ToCharArray());

    //construct the search term
    string searchTermFormat = "";

    for (int i = 0; i < searchTermArray.Length; i++)
    {
        searchTermFormat += searchTermArray[i] + "+";
        //Console.WriteLine(searchTermFormat);
    }

Desired output

word1+word2+word3

where the number of words are not fixed.

Upvotes: 0

Views: 310

Answers (5)

SLaks
SLaks

Reputation: 887375

  • You're looking for String.Join("+", searchTermArray)
  • You're trying to write searchTerm.Replace(' ', '+')
  • You probably should be writing Uri.EscapeDataString(searchTerm)

Upvotes: 7

juharr
juharr

Reputation: 32266

Use String.Join to concatenate the strings together.

Upvotes: 1

Jaime
Jaime

Reputation: 6814

What about myString.Replace(' ', '+');

Upvotes: 0

VdesmedT
VdesmedT

Reputation: 9113

That a typical String.Replace job or RegEx

Upvotes: 0

Sam B
Sam B

Reputation: 2481

Try string.Replace(" ", "+")

Upvotes: 0

Related Questions