Hi all,
I need some help from you expert. I need to extract numeric value of a string.
for ext: string str = "1234 absc st" or it can be "123 hello world"
or "1234567 asp.net"
How can i only get the numeric part of the string?
Please help and give me some suggestions.
Thank you,
13 4095
One potential way would be to go through each char in the string, and check if it's numeric. If it is, add it on to a temporary string, which is then returned. I'm not sure how efficient this is however.
Regular expressions would probably be useful for this.
I'm not good with RegEx myself, so I can't give you a sample, but maybe someone else can help with that.
hi
you always find a space after your numeric value on your string. so its simple, first you find the position of the space , then you able to copy your numeric value by using substring.
regards
chel-1
If the number is at the first, you might like to use Split method using space
as separator, the Parse (or better yet, TryParse) to get the value.
split your string into an string array by space position. then convert your array element into integer. exception will throws when you cast the string. only one array element never throws exception thats element holds your answer.
chel-1
- string MyAddress = "123 N. Main Street";
-
int HouseNumber = Convert.ToInt32(MyAddress.Substring(0, MyAddress.IndexOf(' ')));
-
MessageBox.Show(HouseNumber.ToString(), "House number");
-
Give this a try.
Its up to you to beef it up, give it some value qualification and error handling. But should point you in a workable direction.
- string MyAddress = "123 N. Main Street";
-
int HouseNumber = Convert.ToInt32(MyAddress.Substring(0, MyAddress.IndexOf(' ')));
-
MessageBox.Show(HouseNumber.ToString(), "House number");
-
Give this a try.
Its up to you to beef it up, give it some value qualification and error handling. But should point you in a workable direction.
hi its right.
but my question is whats your idea
when the string value turn to like this "abc 14526 kio mk";
chel-1
Have a look at - string s = "56ttt777";
-
foreach(string strItem in Regex.Split(s,"\\D")){
-
Console.WriteLine(strItem);
-
}
You'd need use System.Text.RegularExpressions of course.
Thank you all for all your suggetion.
like chel-1 said i need to make sure it works both way,
123 abc and abnd 345 edf.
I will give a try to your suggetion and let you all know what happens.
Thank You,
Hi all,
Back to my problem.
I will to save numeric value in separate string as well as the characters.
like,,,,,12345 abcdefh
I need string1 = "12345" and string2 = "abcdefh"
Any idea how can i do this?
Hi all,
Back to my problem.
I will to save numeric value in separate string as well as the characters.
like,,,,,12345 abcdefh
I need string1 = "12345" and string2 = "abcdefh"
Any idea how can i do this?
-
//asuming always divided by a space
-
string startstring="12345 abcdefh";
-
string[] pieces startstring.Split(' ');
-
pieces[0]="12345";//can be parsed by int.Parse() now
-
pieces[1]= "abcdefh";
-
//note there could be a pieces[3]and etc if there are more spaces
-
Hi all,
Back to my problem.
I will to save numeric value in separate string as well as the characters.
like,,,,,12345 abcdefh
I need string1 = "12345" and string2 = "abcdefh"
Any idea how can i do this?
Did you read/try my post above? Regex is the proper way of doing these things.
Thank you all for help. My problem is solved.
Thank you,
Sign in to post your reply or Sign up for a free account.
Similar topics
by: cpp_weenie |
last post by:
Given a std::string of the form "default(N)" where N is an integer number of
any length (e.g. the literal string might be "default(243)"), what is the
quickest way to extract the characters...
|
by: Darren Anderson |
last post by:
I have a function that I've tried using in an if then statement and I've
found that no matter how much reworking I do with the code, the expected
result is incorrect.
the code:
If Not...
|
by: David Filion |
last post by:
Hi,
I have a question about substring(), when I run the following query:
prepaid=# select substring('15148300', 0, 5);
substring
-----------
1514
(1 row)
|
by: Jean-François Michaud |
last post by:
Hello,
I've been looking at this for a bit now and I don't see what's wrong
with the code. Can anybody see a problem with this?
Here is an XSLT snippet I use.
<xsl:template match="graphic">...
|
by: kellygreer1 |
last post by:
What is a good one line method for doing a "length safe"
String.Substring?
The VB classes offer up the old Left function so that
string s = Microsoft.VisualBasic.Left("kelly",200)
// s will =...
|
by: shapper |
last post by:
Hello
I have the following string:
myName = "John Smith Curtis"
I want to get the first word, i.e. "John", which means I want to get
everything from the start to the first space.
I know...
|
by: dyc |
last post by:
how do i make use of substring method in order to extract the specified data from a a long string?
I also need to do some checking b4 extracting the data, for instance:
it only will extract the...
|
by: Serman D. |
last post by:
I would like to extract the first 6 digits of a numeric value (e.g.
the string '123456' out of the numeric 1234567890123456789). I tried
a combination of CAST and SUBSTR, but it seems I am not...
|
by: =?Utf-8?B?anAybXNmdA==?= |
last post by:
Two part question:
1. Is Regex more efficient than manually comparing values using Substring?
2. I've never created a Regex expression. How would I use regex to do the
equivalent of what I...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |