How to detect a special char in string and modify this special word?

user3821183

I have a string, it may looks like this:

asjlfkajs alsdkfja s asldfkjas @abc dfasldkfj asldkfj las @nveweb dfasdlf asldfj.

I want to change any word started with @, to %@ %. for example,

@abc    change to    %@abc%

Can anyone show me an exmaple? I really have no idea about it, how to detect any word starting with @ and add % in the beginning and end of every such word?

Tim Schmelter

Perhaps simply:

str = string.Join(" ", str.Split()
                          .Select(w => w.StartsWith("@") ? "%" + w + "%" : w));

You need to add using system.Linq; at the top since the argument to String.Join is a LINQ query. String.Split without argument splits by white-spaces which includes spaces, new-lines and tabs. At the end the Join will concat all with a space which means that you don't retain possible new-line or tab-characters.

However, the query itself does following, str.Split() returns all "words". Enumerable.Select projects a string which is either the orginal string (if the word doesn't start with @) or a new string which is wrapped by %.

Update if you want to retain the separator if they are at the beginning or at the end of words and don't want them to be wrapped also as in:

@abc.  --> wrong:  %@abc.% correct: %@abc%.

The you could use following method:

static readonly HashSet<char> WordSeparators = new HashSet<char> { ',', '.', '!', '?', ';', ':', ' ', '-', '/', '\\', '[', ']', '(', ')', '<', '>', '"', '\'' };

static string WrapWithIfStartsWith(string input, string startsWith, string wrap, StringComparison comparison = StringComparison.CurrentCulture)
{
    if (string.IsNullOrEmpty(input) || !input.StartsWith(startsWith, comparison))
        return input;
    else if(input.Length == 1)
        return string.Format("{1}{0}{1}", input, wrap);

    char first = input.First();
    char last = input.Last();
    bool firstIsSeparator = WordSeparators.Contains(first);
    bool lastIsSeparator = WordSeparators.Contains(last);
    if (firstIsSeparator && lastIsSeparator)
        return string.Format("{0}{1}{2}{1}{3}",
            first, wrap, input.Substring(1, input.Length - 2), last);
    else if (firstIsSeparator && !lastIsSeparator)
        return string.Format("{0}{1}{2}{1}",
             first, wrap, input.Substring(1));
    else if (!firstIsSeparator && lastIsSeparator)
        return string.Format("{0}{1}{0}{2}",
            wrap, input.Remove(input.Length - 1), last);
    else
        return string.Format("{1}{0}{1}", input, wrap);
}

Now it's simple as:

string str = "blabalbal blabal @abc.";
str = string.Join(" ", str.Split().Select(w => WrapWithIfStartsWith(w,"@","%")));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to detect a special char in string and modify this special word?

From Dev

How add special char into string

From Dev

How to filter of those special char from a word?

From Dev

How to remove special char from spanish word

From Dev

How to escape special characters in a char* string in C

From Dev

How to escape special characters in a char* string in C

From Dev

Match string before special char

From Dev

Remove Special char from the string

From Dev

Replace character with a special char in a string

From Dev

How to detect a special symbol in the TextBox?

From Dev

Java Regex Word Extract exclude with special char

From Dev

Java Regex Word Extract exclude with special char

From Dev

PHP: how to remove special char(except some) from string

From Dev

how to pass a string to a macro sas without special char

From Dev

How to check string has number as well as two special char only

From Dev

PHP: how to remove special char(except some) from string

From Dev

String matches does not work for special char "/"

From Dev

C - is char* template a special type of string?

From Dev

Trim Special Char from SQL String

From Dev

Extract numbers separated by special char from string

From Dev

String matches does not work for special char "/"

From Dev

C - is char* template a special type of string?

From Dev

Word VBA String insertion with special characters

From Dev

Find last word of a string that has special characters

From Dev

Word frequency in a string without spaces and with special characters?

From Dev

How I can replace a underscore with an space inside a word starting with an special char in javascript?

From Dev

how to remove special char-set from string except the first char

From Dev

How to detect special characters using jquery

From Dev

JQuery - How to detect if a div includes a special position

Related Related

  1. 1

    How to detect a special char in string and modify this special word?

  2. 2

    How add special char into string

  3. 3

    How to filter of those special char from a word?

  4. 4

    How to remove special char from spanish word

  5. 5

    How to escape special characters in a char* string in C

  6. 6

    How to escape special characters in a char* string in C

  7. 7

    Match string before special char

  8. 8

    Remove Special char from the string

  9. 9

    Replace character with a special char in a string

  10. 10

    How to detect a special symbol in the TextBox?

  11. 11

    Java Regex Word Extract exclude with special char

  12. 12

    Java Regex Word Extract exclude with special char

  13. 13

    PHP: how to remove special char(except some) from string

  14. 14

    how to pass a string to a macro sas without special char

  15. 15

    How to check string has number as well as two special char only

  16. 16

    PHP: how to remove special char(except some) from string

  17. 17

    String matches does not work for special char "/"

  18. 18

    C - is char* template a special type of string?

  19. 19

    Trim Special Char from SQL String

  20. 20

    Extract numbers separated by special char from string

  21. 21

    String matches does not work for special char "/"

  22. 22

    C - is char* template a special type of string?

  23. 23

    Word VBA String insertion with special characters

  24. 24

    Find last word of a string that has special characters

  25. 25

    Word frequency in a string without spaces and with special characters?

  26. 26

    How I can replace a underscore with an space inside a word starting with an special char in javascript?

  27. 27

    how to remove special char-set from string except the first char

  28. 28

    How to detect special characters using jquery

  29. 29

    JQuery - How to detect if a div includes a special position

HotTag

Archive