0

Supposed I have the following strings:

string str1 = "<tag tsdfg> some other random text";
string str2 = "<tag sdfgsdfgfsdg> some other random text";
string str3 = "<tag 1564> some other random text";

i would like to change those strings to

"<tag> some other random text"
3
  • 11
    str1 = str2 = str3 = "<tag>" ? :) Commented Jan 7, 2015 at 12:03
  • 1
    hmm i gave a bad example :p Commented Jan 7, 2015 at 12:06
  • 1
    @SonerGönül actually str1 = str2 = str3 = "<tag> some other random text" for correctness! :-D Commented Jan 7, 2015 at 12:23

7 Answers 7

3

Use a regular expression:

str1 = Regex.Replace(str1, @"\<tag.*?\>", "<tag>");

Fiddle:

https://dotnetfiddle.net/LdokRn

Sign up to request clarification or add additional context in comments.

2 Comments

I think you need "\<tag.*?\>" to stop regex being greeedy. Otherwise this is fine. +1
You are right, in the OPs case it would not be needed, but as a general solution, yes. Will edit
1

try

str1.Replace((str1.Substring(str1.IndexOf("<"), str1.IndexOf(">"))), "<tag>");

Comments

1

If after <tag there will always be a white-space ,then you can use Split() this way:

string str1 = "<tag tsdfg> some other random text";
string values = str1.Split(' ')[0]+">";

Comments

1

You could use a regular expression to replace all the arbitrary text inside the tag

Regex.Replace("<tag tsdfg> some random text", @"<(.*)?\s+(.*)?>", "<$1>");

This will effectively replace any text, after whitespace, at the end of any tag.

Try it out

8 Comments

@SriramSakthivel yeah I realised the regex was invalid after I posted the answer, I have updated with one that will replace everything inside the tag after whitespace.
Still that's not correct. please test yourself. Op asks for <tag> some other random text but your answer gives some other random text
The OP question is not edited (you can see this in Stack Overflow). A regex would be the way to go IMO, but your sample code (both the regex, the input, and the replacement) are just wrong as an answer to the question
@Jcl yeah I get that and I am fixing, but the question has changed when it was originally posted, there was no mention of "some other random text" in the original post (remember there is a grace period so you can edit questions without it been deemed "edited").
@James I have removed my downvote. I think what you'd want would be more in the line of: Regex.Replace(str1, @"<(.*)?\s+(.*)?>", "<$1>"); as a general solution (probably not bug-free, haven't really checked).
|
0

You can also do it this way:

string str = "<tag tsdfg> some other random text";

string newStr = new string(str.TakeWhile(c => !Char.IsWhiteSpace(c))
                              .Concat(str.SkipWhile(c => c != '>'))
                              .ToArray());

Console.WriteLine(newStr);  //  "<tag> some other random text" 

Comments

0

you can do this:

str1="<tag>" + str1.Remove(0, str1.IndexOf(">") + 1);

it removes from index 0 to >+1 and then add it to "tag"

4 Comments

That does not achieve the requirement
He is trying to change it to remove the "tsdfg" inside the tag.
@JustinHarvey added tsdfg accidently, you could comment it before downvote, don't you think it would be better? is it ok now?
there is no need to get funny, I was just trying to point out the problem in your answer. I think that edit is really for improving answers rather than fixing the fact they are wrong.
0

Let's make it at least once unlike the other solutions not about removing or replacing but extracting ;-]

string str1 = "<tag tsdfg> some other random text";

Match match = Regex.Match(str1, @"\<(tag).+?\>(.+)");
string result = String.Format("<{0}>{1}", match.Groups[1].Value, match.Groups[2].Value);
Console.WriteLine(result );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.