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
In this project I need to separate the Booking ID from the string below into a separate integer.
Booking ID: 27620
Booked for Paitient(ID): 003
Booked Staff ID: 001
Booked Room: b001
Booked Equipment: Adam
How do I do this in C#?
To clarify: The code above is the whole string, the first line is where the booking ID is contained. I need help finding a way to separate the ID from the string.
I have tried Regex but to no prevail. I cannot get my head around the string formatting.
The ID can be any length.
Regex can handle what you are looking for.
string text = "Booking ID: 27620 ...";
foreach (string line in new LineReader(() => new StringReader(text))
{
string result = Regex.Replace(line, #"[^\d]", "");
Console.WriteLine(result); // >> 27620
}
The regex "[^\d]" says:
[] Capture
^\d Everything that is not (^) a digit (\d)
Then the Replace replaces it all with an empty string.
This would be most valuble if you were looping through each line of your file.
If you needed to keep the field value, then running a simple split would be the better way to go.
string input = "Booking ID: 27620";
Dictionary<string, string> dict =
new Dictionary<string, string>{key = input.Split(':')[0],
value = input.Split(':')[1]};
Related
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 1 year ago.
Improve this question
I am trying to figure out regex for wrongly formated names where the user has entered in the name and the relationship as one value, for example
[Son of Joseph Joestar ] => s/o. Joseph Joestar
the problem is since there was no validation, the users entered different variations like
s/o , s/, s/Joseph... , etc
this is so far I have gotten
^(s\/o.)(S\/o.)(s\/o)(S\/o)(s\/)(S\/)\w+
the relation is at the beginning or at the start, and after that is the name
there are 3 more cases Daughter(D/o.), Wife(W/o.), Father(F/o.)
I want to know the corresponding REGEX to filter out the relation prefixes
Thank You In advance
Maybe start with something like that
string foo = "s/o. Joseph Joestar";
// Look (and capture) for SDWF followed by "/" and
// EITHER "o" and maybe "." and maybe a white space
// OR we look ahead (?= ) and see a Upper wordchar followeed by lower word chars.
// look ahead because we do not want to have anything to do with this when replacing
string bar = Regex.Replace(foo, #"^([sSdDwWfF])/(o\.?\s?|(?=[A-Z]\w+))", match =>
{
string relation = match.Groups[1].Value.ToUpper() switch
{
"S" => "Son of",
"D" => "Daughter of",
"F" => "Father of",
"W" => "Wife of",
_ => throw new Exception("Oops")
};
return $"{relation} ";
});
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have this FINAL PAYMENT $25 string on a MVC c# application.
I want to split into FINAL PAYMENT and 25
I tried doing this
string s = "FINAL PAYMENT $25";
string[] str1 = s.Split('$');
//result: 25
How can I get the rest. Can anyone help?
Split method returns a string array, if you need both elements of this array, Try:
string s = "FINAL PAYMENT $25";
string[] resArray = s.Split('$');
var FPayment = resArray[0];
var second25= resArray[1];
You can use indexOf instead of a Split
string s = "FINAL PAYMENT $25";
int index = s.IndexOf("$");
String final_pay = s.Substring(index + 1);
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 5 years ago.
Improve this question
ok I have no idea on how to do this and i have tried looking up how to do this but nothing good came up so ill ask it here. So what i am trying to do is:
string input = TextEditor.text; <-- this is in windows form application and
The "TextEditor" is the textbox for input
i want to take the string (which is input from the texct box) then split it so each word is on every other line like so:
if input = "hi my name is is"
out put should be:
hi: 1
my: 1
name: 1
is: 2 <-- if the word is said it shouldn't be repeated.
could someone please help me? I am a true newbie and i am completely lost. I don't have any code yet because I DONT KNOW HOW TO DO THIS!
Use Linq GroupBy and Count:
string inputText = "hi my name is is";
var words = inputText.Split(' ').ToList();
var wordGroups = words.GroupBy(w => w).Select(grp => new {
Word = grp.Key,
Count = grp.Count()
});
string outputText = string.Join("\n", wordGroups.Select(g => string.Format("{0}:\t{1}", g.Word, g.Count)));
/*
hi: 1
my: 1
name: 1
is: 2
*/
Split the input string into an array, then use that to build a dictionary. If the word is already in the dictionary, increment it. Otherwise add it with an initial value of 1.
string input = TextEditor.text;
string[] inputWords = input.Split(' ');
Dictionary<string, int> wordCounts = new Dictionary<string, int>();
foreach (string word in inputWords)
{
if (wordCounts.ContainsKey(word))
{
wordCounts[word]++;
}
else
{
wordCounts.Add(word, 1);
}
}
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
Hi I am facing problem to get specific string. The string as below:
string myPurseBalance = "Purse Balance: +0000004000 556080";
I want to get 4000 out only.
if your string format/pattern is fix then you can get specific value
string myPurseBalance = "Purse Balance: +0000004000 556080";
//
var newPursebal =Convert.ToDouble(myPurseBalance.Split('+')[1].Split(' ')[0]);
You can use this regular expression:
string extract = Regex.Replace(myPurseBalance, #"(.*?)\: \+[0]*(?<val>\d+) (.*)", "${val}")
It searches for the decimals after :, trims the leading 0s and removes everything after the last whitespace.
You can use string.Split to get the +0000004000 and then use string.Substring to get the last four characters by passing the Length-4 as starting index.
string str = myPurseBalance.Split(' ')[2];
str = str.Substring(str.Length-4);
Learn Regex . Here is just simple tutorial
using System;
using System.Text.RegularExpressions;
namespace regex
{
class MainClass
{
public static void Main (string[] args)
{
string input = "Purse Balance: +0000504000 556080";
// Here we call Regex.Match.
Match match = Regex.Match(input, #"\+[0]*(\d+)",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
}
}
}
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 need help to develop a logic that will extract part of the string when the user enter a "delimiter"
For example:
string data = "|cBlue|pDaisy|pRose|mTomato|mWheat|pCabbage|p100 units|d19.0";
string userInput = //User enter input
So, if the user enters "|c", it should return "Blue"
If the user enters "|m", it should return "Tomato", "Wheat" as 2 strings.
If the user enters "|p", it should return "Daisy", "Rose", "Cabbage" and "100 units" as 4 different strings.
If the user enters something that is not exist, say for example, |z, it will return nothing or an empty string "".
Note: This is just a sample data, the actual data consists of |a - |z, |A - |Z
Start with string.Split() to tokenize the string.
Then iterate over each token. Pull out its first character and construct a Dictionary<char, string> using the first character as a key and the remainder as a value.
Then simply do a dictionary lookup on the desired character to find the relevant token.
First get the input,parse it.Use Split method then filter the words by first letter,Something like this:
var userInput = Console.ReadLine();
if(userInput.Length == 2)
{
var words = data.Split(userInput[0]).Where(x => x.StartsWith(userInput[1].ToString()));
if(words.Any())
{
var result = words.Select(x => x.Substring(1)).ToList();
}
else
{
// no word found
}
}