Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose i have a sentence

my name is james and i am 19 years old.

how can i replace after "and" part til full stop(.) with full stop(.)

the expected output should be something like that.

my name is james.

thank you in advance
Posted
Updated 17-Apr-15 19:53pm
v2

Use a Regex:
C#
public static Regex regex = new Regex(
      @"\sand\s.+\.",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );


string result = regex.Replace(inputText,".");
 
Share this answer
 
v2
Comments
Asad_Iqbal 18-Apr-15 2:31am    
suppose instead of "and" i have list of words i.e. "بلکہ ","مگر ","یعنی "," کیونکہ", "لیکن ","گویا ","یعنی", "چنانچہ"
and instead of full stop(.) i have these "۔", "؟" how regular expression will work than??

your code works perfectly for english but i want to replace it with them
OriginalGriff 18-Apr-15 4:10am    
So look at how regexes work: you can use groups and alternatives:
(\sand\s|\sthen\s).+\.
Replace "and" and "then" with your list and give it a try.

Get a copy of Expresso
http://www.ultrapico.com/Expresso.htm
It's free; it examines, generates, and tests Regular expressions.
Asad_Iqbal 18-Apr-15 4:32am    
Thanksssss alot my 5 for this.. it worked i was stuck there from a very long time :)
OriginalGriff 18-Apr-15 4:45am    
You're welcome!
You can use Substring() to do that
C#
string sentence = "my name is james and i am 19 years old.";
string result = sentence.Substring(0, sentence.IndexOf(" and "))+".";


Hope, it helps :)

Update:
I just realized, it's better to use regular expression to handle these kind of requirements as shown by @OriginalGriff.
 
Share this answer
 
v3
Comments
Asad_Iqbal 18-Apr-15 2:33am    
Index and length must refer to a location within the string.
Parameter name: length

i am getting this probelm Suvendu....
Suvendu Shekhar Giri 18-Apr-15 2:41am    
Check the updated solution. It was mistake at my end. Sorry !
Asad_Iqbal 18-Apr-15 2:53am    
the updated solution gives an output "and i am 19 years old."
Suvendu Shekhar Giri 18-Apr-15 3:12am    
Oh :( updated the solution to have the exact result. But it uses some static appending so I believe you can try finding a solution using regular expressions.
Asad_Iqbal 18-Apr-15 4:11am    
i agree with u too because this code works fine but ignore all sentences after this particular sentence

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900