Trimming and Removing text from string? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to trim a string and remove all the words that occur after a certain word.
For example - If the string contains 'very' text
string mySentence=" Today is very nice day! ";
if (mysentence.Contains(very))
{
//remove everything that starts with 'very' until rest of the line..
}
result should be:
Today is

First you split using the required word
string[] splits = mysentence.Split("very");
Since you've already made certain that "very" is inside the string, this will get you two strings. You want the first one (the split before the "very"). You need to trim the extra space from that one so:
string result = splits[0].Trim();

Try this
string mySentence = " Today is very nice day! ";
if (mySentence.Contains("very"))
{
mySentence = mySentence.Remove(mySentence.IndexOf("very")).Trim();
}

Related

C# subtraction of strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am sorry for that bad Title, but basicaly my problem is really simple. I got 1 string which is basic alphabet and the second string which is gonna be part of the alphabet (8 characters) which user will fill up by himself. If 2 characters are the same, they will get removed and then rest of characters will be in the TextBox3. could someone pls help me ?
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = TextBox2.Text;
I assume you want to check the existence of substring and remove from the parent string, Try this
string alphabet = "abcdefghijklmnopqrstuvwxyz_*";
string special = textBox2.Text;
if (alphabet.ToLowerInvariant().Contains(special))
{
textBox3.Text = alphabet.Replace(special, "");
}

Replace substring which comes in middle of string only [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want regx pattern in C# which find substring in any string which comes in middle only. Let say ,
Input : "toprohitpop rohittoppop toppoprohit"
find substring : "rohit"
Replace with : "$$$$"
Output : "top$$$$pop rohittoppop toppoprohit"
if substring "rohit" comes in left or right of the string then it should not be replaced.Substring "rohit" will only be replaced when it comes in middle of string .
Thanks in advance.
Use non-word-break anchors:
\Brohit\B
The \B will only match if it is in the middle of a word.
Read about it.
var input = "toprohitpop rohittoppop toppoprohit";
var regex = new Regex(#"\Brohit\B");
var output = regex.Replace(input, "$$$$$$$$");
See "Anchors" in Regular Expression Language.
Also, be careful with the '$' in the substitution string (see comments)
Use the following regex: .+rohit.+
Basically it enforeces at least one char before rohit and one after

C# User input a word eg( Hello ), how to i put hello into an array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
User input a word eg( Hello ), how do I put hello into an array? such that
array[1]=h
array[2]=e
array[3]=l
array[4]=l
array[5]=o
Use the string method ToCharArray like this :
char[] input = "hello".ToCharArray();
If you have a lot of logic to do after, I'd recommend using a List instead, which you can get with :
List<char> input = "hello".ToList();
And as a side note, h will be in yourArray[0] (or .ElementAt(0)) not [1], since C# is 0-based; all indexes start at 0 instead of 1.
Actually you don't need to do anything special to accomplish this. You can already access the characters in the string by using an indexer, like this:
"Hello"[0] will return "H", "Hello"[1] will return "e" and so on.
Try this:
char myArray[] = "Hello".ToCharArray();
Try below instead
char [] array = "Hello".ToArray();
use this
string[] a=Console.ReadLine() // Get String From user
char[] myCharArray= a.ToCharArray();

Replace all occurrences of string only if they don't start by '#' [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to replace all the occurrences of a string, as long as those don't start by '#', so for example in the following query:
(surname = #surname and surname = #surname1)
if I want to replace surname, it will only replace the two of them on the left side of the equal sign. Thus, leaving #surname and #surname1 unreplaced.
You can use Regex.Replace for this:
Regex.Replace(yourString, "([^#])surname", "$1Diaz");
The [^#] basically tells Regex that any character except the # symbol can come in front of the "surname" text that you are looking for. The $1 is necessary because otherwise, whatever character that is will also get stripped out.
Note that this Regex, without some additional modification, will not match "surname" if it is at the beginning of the string. In the example you provided, it starts with an open-parenthesis, so as long as that condition holds, the solution above will work.

Splitting a string into parts [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to split a string in C# for example:
120530
so it will be like this:
Day: 12
Time: 0530
But it will be without spaces, just as it is 120530
How can I do that?
I'm assuming that you know that it will always be 6 digits with the first 2 being day and last 4 being time.
Utilize the Substring() method of your string object...
string allTogether = "120530";
string day = allTogether.Substring(0, 2);
string time = allTogether.Substring(2);
var dayAndTime = "120530";
var day = dayAndTime.Substring(0, 2);
var time = dayAndTime.Substring(2, 4);

Categories