How can I return the index of the second word in a string that can have multiple white spaces followed by one word followed by multiple white spaces? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
String str =" vol ABCD C XYZ";
I want to return the index of "A"
Note: There are multiple white spaces before all words including the first.

This will get the index of A in your original example (String str =" vol ABCD C XYZ";):
int indexOfABCD = str.IndexOf(str.Trim()[str.Trim().IndexOf(' ')+1]);
If you have something like String str = " vol ABCD C XYZ"; where there are multiple spaces:
string secondword = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)[1];
int indexOfAinABCD = str.IndexOf(secondword.First());
If you want to get the indices of all the letters in the second word:
IEnumerable<int> fullindex = Enumerable.Range(indexOfAinABCD, secondword.Length);
EDIT:
This will fail if you have A anywhere else in the first word. You should get an exact match through Regex:
int indexOfAinABCD = Regex.Match(str, string.Format(#"\W{0}\W",secondword)).Index+1;
IEnumerable<int> fullindex = Enumerable.Range(indexOfAinABCD, secondword.Length);
So something like String str = " vABCDol ABCD C XYZ"; won't be an issue

Related

How do I split a list on a certain character? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 18 hours ago.
Improve this question
For example, if I have this string: this is item 1 (splithere) this is item 2
Then I want to split the list on character (splithere)
So then the array will be:
[ "this is item 1", "this is item 2" ]
Is there a method for this solution?
You can use string.Split()
var myString = "word1 delimiter word2";
var myList = myString.Split("delimiter");
myList will be ["word1 ", " word2"]
See more here: https://learn.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split
you can use the String.Split method to split a string into an array of substrings based on a specified delimiter. Here's an example:
string input = "this is item 1 (splithere) this is item 2";
string delimiter = "(splithere)";
string[] items = input.Split(new string[] { delimiter }, StringSplitOptions.RemoveEmptyEntries);
In this example, we're using the Split method to split the input string into an array of substrings using the (splithere) delimiter. The second parameter, StringSplitOptions.RemoveEmptyEntries, specifies that any empty entries in the resulting array should be removed.
After running this code, the items array will contain the following two strings:
["this is item 1 ", " this is item 2"]
string str = "this is item 1 (splithere) this is item 2"
var arr = str.Split(new[] {"(splithere)"}, StringSplitOptions.None)

how to split base64 text into base64 strings c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a base64 text example:
UAAAAAAAAAA=AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=KAAAAAAAAAA=AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==
I need to split it into sides like this:
UAAAAAAAAAA=
AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
KAAAAAAAAAA=
AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==
I tried:
string base64Text = "UAAAAAAAAAA=AA....."
Regex regex = new Regex(#"^[a-zA-Z0-9+/]*={0,2}$");
string[] base64Strings = regex.Split(base64Text);
You could split on a position where there is a char A-Z or 0-9 to the left and not a = char or the end of the string to the right:
(?<=[A-Z0-9]=)(?!=|$)
Regex demo
string base64Text = #"UAAAAAAAAAA=AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=KAAAAAAAAAA=AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==";
Regex regex = new Regex(#"(?<=[A-Z\d]=)(?!=|$)");
string[] base64Strings = regex.Split(base64Text);
foreach (string s in base64Strings)
{
Console.WriteLine(s);
}
Output
UAAAAAAAAAA=
AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=
KAAAAAAAAAA=
AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhAAAAAAAAAEEAAAAAAAAAAAA==

How do I remove an X number of capital letters from a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to remove an X number of capital letters from a string.
For example if I had the strings:
string Line1 = "NICEWEather";
and
string Line2 = "HAPpyhour";
How would I create a function that pulls out the 2 sets of Capital letters?
Try using regular expressions with \p{Lu} for a Unicode capital letter
using System.Text.RegularExpressions;
...
// Let's remove 2 or more consequent capital letters
int X = 2;
// English and Russian
string source = "NICEWEather - ХОРОшая ПОГОда - Keep It (HAPpyhour)";
// ather - шая да - Keep It (pyhour)
string result = Regex.Replace(source, #"\p{Lu}{" + X.ToString() + ",}", "");
Here we use \p{Lu}{2,} pattern: capital letter appeared X (2 in the code above) or more times.
To remove capital letter from string
string str = " NICEWEather";
Regex pattern = new Regex("[^a-z]");
string result = pattern.Replace(str, "");
Console.WriteLine(result );
output: ather
to remove capital letter if occurrences more than once in sequence order then try this
string str = " NICEWEather";
Regex pattern = new Regex(#"\p{Lu}{2,}");
string output = pattern.Replace(str, "");
Console.WriteLine(output);

Get values between string in C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string value like "%2%,%12%,%115%+%55%,..."
Sample inputs "(%2%+%5%)/5"
Step1:
get the vales 2 and 5
Step2:
get values from table column2 and column5
step3:
from that value (Column2+Column5)/5
How to get the values from that string
2,12,115,55
Also
commas, "+" (symbols)
Thanks in Advance.
I referred these links:
Find a string between 2 known values
How do I extract text that lies between parentheses (round brackets)?
You can replace the % and then split on the , and +:
var value = "%2%,%12%,%115%+%55%,";
value = value.Replace("%", "");
var individualValues = value.Split(new[] {',', '+'});
foreach (var val in individualValues)
Console.WriteLine(val);
If I understand you correctly...
var string = "%2%,%12%,%115%+%55%";
var values = string.replace("%", "").replace("+", "").split(',');
Edit: Actually, I think you mean you want to split on "+" so that becomes split(',', '+')
str.Replace("%", "").Replace("+",",").Split(',');
This will do it.
Another regex solution:
foreach (Match m in Regex.Matches(str, #"\d+"))
// m.Value is the values you wanted
Using regular expressions:
using System.Text.RegularExpressions;
Regex re = new Regex("\\d+");
MatchCollection matches = re.Matches("%2%,%12%,%115%+%55%,...");
List<int> lst = new List<int>();
foreach (Match m in matches)
{
lst.Add(Convert.ToInt32(m.Value));
}
It's possible parsing the string with splits and other stuff or through Regex:
[TestMethod]
public void TestGetNumberCommasAndPlusSign()
{
Regex r = new Regex(#"[\d,\+]+");
string result = "";
foreach (Match m in r.Matches("%2%,%12%,%115%+%55%,..."))
result += m.Value;
Assert.AreEqual("2,12,115+55,", result);
}

Compare regular expression [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to compare following string by Regular Expression. I have surf lot of time but unable to get it's pattern.
string str = "Full Name: Atif Mahmood"
+ "ID Number: 12345678901"
+ "Mobile Number: +921234567890";
In above string
Full Name:
ID Number:
Mobile Number:
are necessary with sequence and there should be any string after these constants.
var regex = "Full Name:(.*)ID Number:(.*)Mobile Number:(.*)";
var match = Regex.Match(string, regex);
match.Groups[1] will contain the name, [2] will contain the ID number, etc. (Groups[0] is the whole match group, so counting each match starts at 1)
This probably needs some bullet proofing, but you get the idea?
In case you want to check that the string follows the pattern you stated, this expression should do it:
const string expression = "Full Name:.*ID Number:.*Mobile Number:.*";
bool correct = Regex.IsMatch(str, expression);

Categories