Getting a substring from a string
string substring c++
string substring c#
java substring indexof
substring in java example programs
java substring from end
getting a substring from a string after a particular word in java
string substring python
I have some code that attributes the value in a cell adjacently to other values in the sheet. The value in the cell is "Nat Rep feasibility check for United States - Details by Region", in which the country variates - it can be "Australia";"Argentina" etc. I need to extract that country and put it next to the values in cause:
Sub RowInt() Dim rng As Range For i = 1 To Sheets.Count Sheets(i).Select Cells.Range("A1").Select ActiveCell.EntireColumn.Insert ActiveCell.EntireColumn.Insert Range("A7").Value = "Channel" Range("B7").Value = "Country" mystring = Cells(1, 3).Value NumRows = Range("C8", Range("C8").End(xlDown)).Rows.Count For j = 1 To NumRows - 1 Cells(j + 7, 2).Value = Replace(mystring, "Nat Rep feasibility check for", "") Cells(j + 7, 1).Value = Range("C2").Value Next j Next i End Sub
I managed to get "Nat Rep feasibility check for" out of the way but i still need to get the rest of the string, can you help me? Thank you in advance!
How about removing both fixed parts of the string - as you were already doing in one case:
Cells(j + 7, 2).Value = Replace(Replace(mystring, "Nat Rep feasibility check for ", ""), " - Details by Region", "")
Java - String substring() Method example, If "start" is less than 0, it will start extraction from index position 0: string text = "Retrieves a substring from this instance. The substring starts at a specified character position. Some other text"; string result = text.Substring(text.IndexOf('.') + 1,text.LastIndexOf('.')-text.IndexOf('.')) This will cut the part of string which lays between the special characters.
You can achieve it by this formula:
=MID(F8,LEN("Nat Rep feasibility check for ")+1,LEN(F8)-LEN("Nat Rep feasibility check for ")-LEN("Details by Region")-2)
Java String substring() method example, Java String substring() method is used to get the substring of a given string that beginIndex is inclusive and endIndex is exclusive while getting the substring. You call the Substring(Int32, Int32) method to extract a substring from a string that begins at a specified character position and ends before the end of the string. The starting character position is a zero-based; in other words, the first character in the string is at index 0, not index 1.
Example test sub
Public Sub test() Debug.Print Split(Split("Nat Rep feasibility check for United States - Details by Region", "for" & Chr(32))(1), Chr(32) & Chr(45))(0) End Sub
As an UDF in the sheet
Public Function GetCountry(ByVal rng As Range) As String GetCountry = Split(Split(rng.Text, "for" & Chr$(32))(1), Chr$(32) & Chr$(45))(0) End Function
Use in sheet:
Substring in Java, Java String substring() example. Learn to get substring of a string from given start index to end index. A valid index is between 0 and length of string. In this quick tutorial, we'll focus on the substring functionality of Strings in Java. We'll mostly use the methods from the String class and few from Apache Commons' StringUtils class. In all of the following examples, we're going to using this simple String: String text = "Julia Evans was born on 25-09-1984.
How to check if String contains another SubString in , String substring() : This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified For example, in Java, the substring method is used to get the substring from the source string. In Python, you may use a couple of ways for getting a substring from the source string. While there is no formal method like “substring” or “substr” etc. the techniques are called slicing and extended slicing .
The substring() Method in JDK 6 and JDK 7, How do you create a substring from a string in C++? The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first). Parameters pos Position of the first character to be copied as a substring. If this is equal to the string length, the function returns an empty string.
- Is the
vbanecessity here? Seems to be something that you can do with formula - if you provide example of data.
- If there is always a
-after the country name, that could be used as a marker.
- There are multiple sheets and at the end i have to get them congregated in a single sheet, this is just preparing them for later use.
- Yes there's always a "-" after the country name, I tried with Left after the first string removal but it returns to the original string...
MyString= "Nat Rep feasibility check for USA - Details by Region" then the above would output "USA"
- What produces "FALSE"? A replace can't produce FALSE unless it started with FALSE in it. Step through (
F8) the procedure to monitor what's happening line-by-line.
- @CLR im encountering an interesting error... when i copy yours it does not do the first replace until i copy and paste the "Nat Rep..." phrase from the original question...
- manually typing the phrase "Nat Rep feasibility check for " will not work - I have to copy and paste the exact string from the initial question even though the strings are (as far as i can tell) identical
- Clear and simple.
- Sometimes I just can't understand why going for crazy solutions, keep it simple :)
- Because this doesn't remove the "- Details By Region" from the end. You're only deleting the part OP has already successfully removed with
- Sorry @MoondogsMaDawg, Haven't seen that. just use this one instead: =MID(F8,LEN("Nat Rep feasibility check for ")+1,LEN(F8)-LEN("Nat Rep feasibility check for ")-LEN("Details by Region")-2)
- A different version is mid(f8,find(("for ",f8,1)+4,len(f8)-find("- D",f8,1)+2)