美文网首页
JS,Java,Python语法对比

JS,Java,Python语法对比

作者: 好心态 | 来源:发表于2018-09-21 12:33 被阅读32次

    字符串

    Java Javascript Python 说明
    charAt charAt
    compareTo ><
    compareToIgnoreCase
    concat concat
    contains includes
    endsWith endsWith endswith
    getBytes
    getChars
    indexOf(str,from) indexOf find 找不到返回-1
    str.search() 可使用正则
    isEmpty length() is 0.
    lastIndexOf lastIndexOf rfind
    "s".repeat(2) 's' * 2
    subSequence(start,end)
    toLowerCase toLowerCase lower()
    trim trim strip
    trimStart lstrip
    trimEnd rstrip
    length() length len(str) 字符串
    length length len(arr) 数组
    slice(-12, -6) [:]
    subString(start,end) subString(start,end) 参数不能为负
    substr(start,length) 指定数目的字符
    replace replace replace(old, new, max) js非全部替换
    charCodeAt
    split split split(str,num)

    正则

    Javascript Java Python
    p=/[az][xc]/g p=Pattern.compile("r"); p= re.compile(r'([a-z]+)'
    新建Pattern对象 首先import re
    p.exec(string) p.matcher(str) p.search(str)<==>re.search(reg,str)
    数组或null,返回第一个匹配的分组 获取Matcher对象 任意位置匹配,返回match对象
    p.match(str)<==>re.match('www',str).
    从首字母开始,只匹配一个Match或None
    m.group(0) m.group(0)
    <==>m.group() <==>m.group()
    m.group(1) m.group(1)
    获取第一个分组
    m.groupCount;m.start m.span;m.start
    获取匹配信息
    'ixaapzzpaa'.match(/az/g) p.findall
    数组或null 数组或空数组
    re.search(pattern, string)
    返回match对象,无需从头匹配
    m.replaceAll() re.sub(pattern, repl, string)
    m.matches()
    是否全局匹配
    m.lookAt()
    开头匹配
    /e/.test("best") m.find()
    布尔值 非开头匹配
    str.matches(reg)
    全局匹配,返回布尔型
    /^be[sn]t$/.test("best") Pattern.matches(reg,str)
    方法
    StringObj.split([separator) re.split

    数组

    Java Javascript Python
    数组新建
    int a[]=new int[]{1,2,3} var myCars=["E","SV","B"]; [1,2,3,]
    int a[]= {2,3,4,5};
    splice(start,len)
    slice(start,end) [:]
    [].map(foo,init) list(map(mul, blist))
    [].filter(foo,init) filter(fun,list)
    every() all([i > 5 for i in nums])
    some() any([i % 2 == 0 for i in nums])

    [1, 2, 3, 4].forEach(function (elem, index, array) {
    this.push(elem * elem);
    }, out);

    语法

    Java Javascript Python
    不定参数
    String... args ... args * args
    默认参数
    function say(name='dude') def print(age=35 ):
    foreach
    for(int : ) for(v of list) for in :
    下标遍历
    for(v in list) for in:
    new Cat() new Cat() Cat()
    (x,y) ->x>0 const foo = bar => bar + 1 lambda x,y:y*x
    obj instanceof class) x instanceof Array isinstance(object, classinfo)
    typeof() type()

    for of 可对字符串进行遍历

    Java:
    ArrayList<Integer> a = new ArrayList<Integer>();
    a.add(44);
    a.add(66);
    a.add(366);
    for(int b : a) {
    q(String.valueOf(b));
    }
    for (Integer in : map.keySet()) {
    String str = map.get(in);//得到每个key多对用value的值
    }
    2、通过Map.entrySet使用iterator遍历key和value
    Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry<Integer, String> entry = it.next();
    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    }

    3、通过Map.entrySet遍历key和value,推荐,尤其是容量大时
    for (Map.Entry<Integer, String> entry : map.entrySet()) {
    //Map.entry<Integer,String> 映射项(键-值对) 有几个方法:用上面的名字entry
    //entry.getKey() ;entry.getValue(); entry.setValue();
    //map.entrySet() 返回此映射中包含的映射关系的 Set视图。
    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    }

    4、通过Map.values()遍历所有的value,但不能遍历key
    for (String v : map.values()) {
    System.out.println("value= " + v);
    }

    jquery:$.each

    players.forEach((player) -> System.out.print(player + "; "));

    Js#
    try {
    throw "值为空";
    } catch(err) {
    txt="本页有一个错误。\n\n";
    txt+="错误描述:" + err.message + "\n\n";
    txt+="点击确定继续。\n\n";
    alert(txt);
    }
    Python#
    try:
    raise ValueError
    print c
    except (IOError ,ZeroDivisionError),x:
    print x
    else:
    print "no error"
    print "done"

    相关文章

      网友评论

          本文标题:JS,Java,Python语法对比

          本文链接:https://www.haomeiwen.com/subject/sbfonftx.html