0

I am looking to find a string with in a string.

Lets say I have 2 strings:

String1 = "1.1)The Element is"

String2 = "1.1)The Element is:(-) for the sub"

If I compare String1 with String2, I can get "1.1)The Element is" which is ok.

 int Length_Str1 = string1.Length;

 string2 = string2.Remove(Length_Str1);

But I also want to get the non-alphabetical characters ":(-)". I am thinking to keep extracting the character until a space character is found. But I don't know how I can do it in C#.

3
  • 2
    So basically, you want to get 1.1)The Element is:(-)? Commented Mar 4, 2014 at 13:44
  • You probably want to look at string.Substring Commented Mar 4, 2014 at 13:47
  • @Tijesunimi yes. But this is only 1 Example I have more strings. Commented Mar 4, 2014 at 13:47

7 Answers 7

2

You could take chars so long as Char.IsLetter and Char.IsWhiteSpace return false:

int index = String2.IndexOf(String1);
if(index >= 0)
{
    string result = String1;
    if (String1.Length < String2.Length)
    {
        string rest = String2.Substring(index + String1.Length);
        var chars = rest.TakeWhile(c => !Char.IsLetter(c) && !Char.IsWhiteSpace(c));
        result = result + string.Join("", chars);
    }
}

Note that you have to add using System.Linq; at the top of the file.

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

3 Comments

I am having an error on this line, var takeChars = rest.Where(c => !Char.IsLetter(c) && !Char.IsWhiteSpace(c)); The Error is: Cannot convert lambda expression to type 'System.Func<char,int,bool>' because it is not a delegate type
@Kami: i have tested it with your sample strings. What framework version are you using, have you added using System.Linq;?
Yes I added the library. But let me check again.
1
var String1 = "1.1)The Element is";
var String2 = "1.1)The Element is:(-) for the sub";
var result = string.Empty;
if(String2.Contains(String1))
{
    result = String1 + Regex.Match(String2.Replace(String1, string.Empty), "[^\\sa-zA-Z0-9]+").ToString();
}

//result will contain String1 + ":(-)" from String2 IF there is a match

1 Comment

+1 for having the same idea as mine, at least your answer is quicker lol. I spent way too much time to explain everything... :(
1

How about this:

string s = "1.1)The Element is:(-) for the sub";
s = s.Substring(0, s.IndexOf("(-) ") + "(-) ".Length);

This gives 1.1)The Element is:(-)

string s = "1.1)The Element is:(-) for the sub";
s = s.Substring(s.IndexOf("(-) ") + "(-) ".Length);

This gives for the sub. Going by your comment:

 string String1 = "1.1)The Element is";

 string String2 = "1.1)The Element is:(-) for the sub";
 if(String2.Contains(String1))
 {
      string s = String2.Substring(String2.IndexOf(String1)+ String1.Length);
      s = s.Substring(s.IndexOf(" ")+1);  // +1 to leave space
 }

6 Comments

Its not always the case, some strings contains "(-)". Some contains ")" Some contains ":" and some contains "-". I need some how to keep getting the characters until a space is found.
Your logic is flawed - the space is even after the, element, etc - how do you plan to differentiate them?
it should starting finding the space once the matching is found.
Matching for what is found?
match string1 with string2, matching is found now find the first space after the matching string.
|
1

This could be solved with a regular expression quite easily:

//Note the special escape character here for the regex engine not to fail on a found ')'
string string1 = @"1.1\)The Element is:";

List<string> testStrings = new List<string>();
testStrings.Add(@"1.1)The Element is:(-) for the sub 1");
testStrings.Add(@"1.1)The Element is:) for the sub 2");
testStrings.Add(@"1.1)The Element is:[-] for the sub 3");

//Create a regular expression string based upon the 'string1' provided above.
string regularExpression = string.Format(@"(?<base>{0})+(?:[^\\sa-zA-Z0-9]+)", string1);
Regex regex = new Regex(regularExpression, RegexOptions.Multiline);
//Will contain the found results
List<string> subStrings = new List<string>();

foreach (string str in testStrings)
{
  foreach (Match match in regex.Matches(str))
  {
    if (match.Success)
    {
      subStrings.Add(str.Replace(match.Groups[0].ToString(), string.Empty));
    }
  }
}
//Display the found results
foreach (string str in subStrings)
{
  Console.WriteLine(str);
}

After executing this code, it will yield the following results:

for the sub 1 
for the sub 2 
for the sub 3

I am not sure if this is what you are looking for, but this code makes it able to have different patterns after 'is:'.

Edit: Hash Sling Slasher just provided the same answer, albeit a bit smaller :)

Comments

0

How about using Split and string.Join like this ?

var str1 = "1.1)The Element is";
var str2 = "1.1)The Element is:(-) for the sub";

str2 = string.Join(" ",str2.Split().Take(str1.Count(x => x == ' ')+1));

Comments

0

The following code should find the index of the first space after the length of string1.

int Length_Str1 = string1.Length;

string2 = string2.Remove(string2.IndexOf(' ', Length_Str1));

Please note that you should also add some checks to make sure that string2 is longer than string1 and that .IndexOf actually finds a space.

Comments

0

You can do something like:

At least if your string is always with the same format ;)

string result = string.Join(" ", string1.Split(' ').Take(3).ToArray<string>());

I hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.