-1

I have a string:

string str = "abc = def; \r\n header \r\n { \r\n def"

In this string i want to get the string just before the character { i.e, "header".

I can get the index of { by

str.indexOf('{')

But how can we get the string just before '{' ?

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
Ani_1317
  • 151
  • 1
  • 3
  • 12
  • Search the web or this site for "C# get string before character" and you'll find plenty of hints. See duplicate. – CodeCaster Nov 27 '14 at 11:12
  • `string result = str.SubString(0, str.indexOf('{'));` – Rohit Nov 27 '14 at 11:17
  • The given duplicate link gives all the characters till the the specified index. I want just the previous string from the specified {. – Ani_1317 Nov 27 '14 at 11:18
  • Then you'll have to define "just before" or "previous string". If you can explain that in words, by editing your question, you're already halfway to writing the appropriate code. Is it something like _"The line (where 'line' is a string separated by `\r\n`) before the line starting with `{`, ignoring all whitespace"_? You may also want to write or use a parser instead of relying on substrings, especially when multiple newlines, other whitespace, comments and nesting come into play. – CodeCaster Nov 27 '14 at 11:27
  • @CodeCaster: Yes. What you meant is correct. I want the string before the character '{' ignoring all whitespace characters. – Ani_1317 Nov 27 '14 at 11:39
  • No, "the string before" is what the duplicate gives you. Do you want the _line_ before? – CodeCaster Nov 27 '14 at 11:40
  • Umm.. yes... line would be enough... – Ani_1317 Nov 27 '14 at 11:50
  • And what if str is "abc = def; \r\n header1 header2 \r\n { \r\n def"? Would you expect "header2" or "header1 header2" as your result? I.e. are you looking for the line or for the word preceding the curly bracket? – Francesco Baruchelli Nov 27 '14 at 12:01
  • You can use: string str = "abc = def; \r\n header \r\n { \r\n def"; string result=str; if(str.Contains('{')) { string[] separators = new string[] { "\r\n" };string[] strOut = str.Split(separators, StringSplitOptions.None); int index = Array.FindIndex(strOut, a => a.Trim() == "{"); result = strOut[index-1]; } – Gleb Nov 27 '14 at 12:02
  • @FrancescoBaruchelli: Yes. I would need "header2" only. I am looking for the word preceding the curly bracket. In this case, there will be only one word in the line before {. – Ani_1317 Nov 27 '14 at 12:11

1 Answers1

0

Edited after CodeCaster comment

string str = "abc = def; \r\n header \r\n { \r\n def"

int length = str.IndexOf('{'); // if there is no match - the resulted value is -1

string result = (length < 0) ? string.Empty : str.SubString(0, length);
ymz
  • 6,602
  • 1
  • 20
  • 39
  • 1
    Can you try to explain what your code does, and what can happen when the input string doesn't contain the character you're looking for? – CodeCaster Nov 27 '14 at 11:16
  • str.SubString(0, length) would give entire string till { right. I want the string just before {. Only one string i., "header" in this case. – Ani_1317 Nov 27 '14 at 11:17
  • An alternative to `str.Substring(0, length)` is `str.Remove(length)` (because we know the `str` value has a length that is at least `1+length`). – Jeppe Stig Nielsen Nov 27 '14 at 12:41