0

I have string as= var str = "9\10\16" . i want to convert it into array of strings as= ["9","10","16"]. So how to get such output

user1722857
  • 877
  • 2
  • 19
  • 33

5 Answers5

1

Depends on your language. Most of them have a method which directly splits the string to an array. Some examples:

Java:

String s = "9/10/11";
String[] foo = s.split("/");

JavaScript:

var s = "9/10/11";
var foo = s.split("/"); 

.NET:

string s = "9/10/11";
string foo = s.Split("/");

Python:

s = "9/10/11";
foo = s.split('/');

Some characters, like '\' which you're using as a delimeter have to be escaped, so they will be counted as a basic text character, not as an escape character. This means that your delimiter will be defined as "\\" for example in Java:

String foo = s.split("\\");
Dropout
  • 13,653
  • 10
  • 56
  • 109
0

if using java you can use split function java. that will itself provide you the array of elements split-ed on "/".

Harsh
  • 1
  • 1
0

The backslash (\) is an escape character in Javascript.
See this post: https://stackoverflow.com/a/3903661/2408648.

I suggest you to escape all backslashes then you can do:

var str = "9\\10\\16";
console.log(str.split("\\"));
Community
  • 1
  • 1
Florent Hemmi
  • 105
  • 1
  • 2
  • 8
  • How to escape all the backslashes? means how to convert op's string to what you have written? – Sachin Oct 15 '13 at 10:10
0
String str = "9\\10\\16" ;
  StringTokenizer st = new StringTokenizer(str,"\\");
  String[] array = new String[st.countTokens()];
  for(int i=0;i<st.countTokens();i++)
  {
      array[i] = st.nextToken();
  }

Use \\ in string variable to escape single \ .

Dax Joshi
  • 143
  • 11
0
<script type="text/javascript">
 var str="09\\10\\11";
 var n=str.split("\\");
 alert(n);
</script>

for javascript

Dax Joshi
  • 143
  • 11
  • I would suggest you to not to post multiple answer. You can always improve your existing answer by edit that. – Sachin Oct 15 '13 at 10:16