Converting the comma separated string [duplicate] - c#

This question already has answers here:
Split comma-separated values
(6 answers)
Closed 8 years ago.
I have a string which is in following format or comma separated format
s1 ,s2,s3,s4,and so on. I want to convert it into
s1
s2
s3
s4
..
..
..
I know how I can do this by using a loop but I want to do this by regex in c# and in java without using the loop can I achive this ???

Here is how you can do it in Java.
public class MainProg {
public static void main(String[] args) {
String s = "s1,s2,s3,s4";
String z = s.replaceAll(",", "\n");
System.out.println(z);
}
}

find : \s*,\s*
replace with : \n

String.Join(Environment.NewLine, myString.Split(','));

Related

Use # symbol in regex search as keyword in C# [duplicate]

This question already has answers here:
Regex to match a word with + (plus) signs
(5 answers)
Search and replace method using regular expressions
(1 answer)
Closed 3 months ago.
Created an extension method and it works for normal words but When I added an # as part of search keyword it stopped working. Can someone help?
public static class Extensions
{
public static string ReplaceWholeWord(this string inputString, string oldValue, string newValue)
{
string pattern = #"\b#test\b"; // #"\b$\b".Replace("$",oldValue);
return Regex.Replace(inputString, pattern, newValue);
}
}
static void Main(string[] args)
{
string input = "#test, and #test but not testing. But yes to #test";
input = input.ReplaceWholeWord("#test", "text");
Console.WriteLine(input);
}

c# convert string to array on char(253) deliminator [duplicate]

This question already has answers here:
How do i split a String into multiple values?
(5 answers)
Closed 3 years ago.
Not entirely sure the following code is going to help many people, but here goes
try
{
uvConnect = UniObjects.OpenSession(serverId, sUser, sPass, sAcct, "uvcs");
// Open Movie File
UniFile uvFile = uvConnect.CreateUniFile("MOVIES");
UniDynArray movieRec = uvFile.Read(txtMovieId.Text);
string sMovieData = movieRec.StringValue;
MessageBox.Show(sMovieData);
}
sMovieData contains a single string of the entire record retrieve from MOVIES file, each field is deliminated by a char(253) character in the database I am using.
Is there a function/method/etc to convert the string to an array using char(253) as a value deliminator
Something like this should work:
string[] fields = sMovieData.Split((char)253);
Try this... string[] arrayValues = "stringToConvertToArray".Split((char)253);

Finding a string within another string using C# [duplicate]

This question already has answers here:
Find text in string with C#
(17 answers)
Closed 3 years ago.
How do I find out if one of my strings occurs in the other in C#?
For example, there are 2 strings:
string 1= "The red umbrella";
string 2= "red";
How would I find out if string 2 appears in string 1 or not?
you can use String Contains I guess. pretty sure there is similar question have been asked before How can I check if a string exists in another string
example :
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
You can do it like this:
public static bool CheckString(string str1, string str2)
{
if (str1.Contains(str2))
{
return true;
}
return false;
}
Call the function:
CheckString("The red umbrella", "red") => true

c# - How can i format a string in c# [duplicate]

This question already has answers here:
Add zero-padding to a string
(6 answers)
Closed 4 years ago.
I have a very simple Question to ask.
I have a string like:
string str="89";
I want to format my string as follow :
str="000089";
How can i achieve this?
Assuming the 89 is actually coming from another variable, then simply:
int i = 89;
var str = i.ToString("000000");
Here the 0 in the ToString() is a "zero placeholder" as a custom format specifier; see https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
If you have a string (not int) as the initial value and thus you want to pad it up to length 6, try PadLeft:
string str = "89";
str = str.PadLeft(6, '0');
If you want the input to be a string you'll have to parse it before you output it
int.Parse(str).ToString("000000")

Split a string into an array c# [duplicate]

This question already has answers here:
Convert comma separated string of ints to int array
(9 answers)
Closed 8 years ago.
I have a string in C#. It's blank at the beginning, but eventually it will become something like
public string info12 = "0, 50, 120, 10";
One of you might be thinking, eh? Isn't than an integer array? Well it needs to stay a string for the time being, it must be a string.
How can I convert this string into an string array (Variable info13) so I can eventually reference it into more variables.
info 14 = info13[0];
info 15 = info13[1];
PLEASE NOTE: This is not a duplicate question. If you read the whole thing, I clearly stated that I have an array of strings not integers.
Here are a few options:
1. String.Split with char and String.Trim
Use string.Split and then trim the results to remove extra spaces.
public string[] info13 = info12.Split(',').Select(str => str.Trim()).ToArray();
Remember that Select needs using System.Linq;
2. String.Split with char array
No need for trim, although this method is not my favorite
public string[] info13 = info12.Split(new string[] { ", " }, StringSplitOptions.None);
3. Regex
public string[] info13 = Regex.Split(info12, ", ");
Which requires using System.Text.RegularExpressions;
EDIT: Because you no longer need to worry about spaces, you can simply do:
public string[] info13 = info12.Split(',');
Which will return a string array of the split items.

Categories